First checkpoint
This commit is contained in:
38
server/categories/create.go
Normal file
38
server/categories/create.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package categories
|
||||
|
||||
import (
|
||||
"gis/app"
|
||||
"gis/server/httputil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CreateCategoryRequest struct {
|
||||
Name string `json:"name" validate:"required,max=255"`
|
||||
Description string `json:"description" validate:"required"`
|
||||
}
|
||||
|
||||
func createCategoryRoute(application *app.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := httputil.DecodeJSON[CreateCategoryRequest](w, r)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := application.Validator.Struct(req); err != nil {
|
||||
httputil.WriteValidationErrors(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = application.Db.Exec(application.Ctx,
|
||||
"INSERT INTO categories (name, description) VALUES ($1, $2)",
|
||||
req.Name, req.Description,
|
||||
)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
}
|
||||
27
server/categories/delete.go
Normal file
27
server/categories/delete.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package categories
|
||||
|
||||
import (
|
||||
"gis/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func deleteCategoryRoute(application *app.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
|
||||
tag, err := application.Db.Exec(application.Ctx,
|
||||
"DELETE FROM categories WHERE id=$1",
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
39
server/categories/index.go
Normal file
39
server/categories/index.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package categories
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gis/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func listCategoriesRoute(application *app.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
rows, err := application.Db.Query(application.Ctx, "SELECT id, name FROM categories")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
cats := make([]Category, 0)
|
||||
for rows.Next() {
|
||||
var c Category
|
||||
if err := rows.Scan(&c.ID, &c.Name); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
cats = append(cats, c)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(map[string][]Category{"data": cats}); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
13
server/categories/routes.go
Normal file
13
server/categories/routes.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package categories
|
||||
|
||||
import (
|
||||
"gis/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func AddCategoriesRoutes(application *app.App, mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /categories", listCategoriesRoute(application))
|
||||
mux.HandleFunc("POST /categories", createCategoryRoute(application))
|
||||
mux.HandleFunc("PUT /categories/{id}", updateCategoryRoute(application))
|
||||
mux.HandleFunc("DELETE /categories/{id}", deleteCategoryRoute(application))
|
||||
}
|
||||
6
server/categories/types.go
Normal file
6
server/categories/types.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package categories
|
||||
|
||||
type Category struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
44
server/categories/update.go
Normal file
44
server/categories/update.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package categories
|
||||
|
||||
import (
|
||||
"gis/app"
|
||||
"gis/server/httputil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UpdateCategoryRequest struct {
|
||||
Name string `json:"name" validate:"required,max=255"`
|
||||
Description string `json:"description" validate:"required"`
|
||||
}
|
||||
|
||||
func updateCategoryRoute(application *app.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
|
||||
req, err := httputil.DecodeJSON[UpdateCategoryRequest](w, r)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := application.Validator.Struct(req); err != nil {
|
||||
httputil.WriteValidationErrors(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
tag, err := application.Db.Exec(application.Ctx,
|
||||
"UPDATE categories SET name=$1, description=$2, updated_at=now() WHERE id=$3",
|
||||
req.Name, req.Description, id,
|
||||
)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user