First checkpoint
This commit is contained in:
30
server/datasets/create.go
Normal file
30
server/datasets/create.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package datasets
|
||||
|
||||
import (
|
||||
"gis/app"
|
||||
"gis/server/httputil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CreateDatasetRequest struct {
|
||||
Name string `json:"name" validate:"required,max=255"`
|
||||
Description string `json:"description" validate:"required"`
|
||||
}
|
||||
|
||||
func createDatasetRoute(application *app.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := httputil.DecodeJSON[CreateDatasetRequest](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
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
}
|
||||
27
server/datasets/delete.go
Normal file
27
server/datasets/delete.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package datasets
|
||||
|
||||
import (
|
||||
"gis/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func deleteDatasetRoute(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 datasets 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)
|
||||
}
|
||||
}
|
||||
45
server/datasets/index.go
Normal file
45
server/datasets/index.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package datasets
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gis/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func listDatasetsRoute(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 datasets")
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
datasets := make([]Dataset, 0)
|
||||
|
||||
for rows.Next() {
|
||||
var dataset Dataset
|
||||
if err := rows.Scan(&dataset.ID, &dataset.Name); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
datasets = append(datasets, dataset)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(map[string][]Dataset{"data": datasets})
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
13
server/datasets/routes.go
Normal file
13
server/datasets/routes.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package datasets
|
||||
|
||||
import (
|
||||
"gis/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func AddDatasetsRoutes(application *app.App, mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /datasets", listDatasetsRoute(application))
|
||||
mux.HandleFunc("POST /datasets", createDatasetRoute(application))
|
||||
mux.HandleFunc("PUT /datasets/{id}", updateDatasetRoute(application))
|
||||
mux.HandleFunc("DELETE /datasets/{id}", deleteDatasetRoute(application))
|
||||
}
|
||||
6
server/datasets/types.go
Normal file
6
server/datasets/types.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package datasets
|
||||
|
||||
type Dataset struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
44
server/datasets/update.go
Normal file
44
server/datasets/update.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package datasets
|
||||
|
||||
import (
|
||||
"gis/app"
|
||||
"gis/server/httputil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UpdateDatasetRequest struct {
|
||||
Name string `json:"name" validate:"required,max=255"`
|
||||
Description string `json:"description" validate:"required"`
|
||||
}
|
||||
|
||||
func updateDatasetRoute(application *app.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
|
||||
req, err := httputil.DecodeJSON[UpdateDatasetRequest](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 datasets 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