From 8716ac3e650075525cab7fb5caf1aa62b3efe55b Mon Sep 17 00:00:00 2001 From: NeonXP Date: Wed, 4 Jan 2023 18:44:58 +0300 Subject: rewrite --- internal/handler/crud.go | 88 +++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 46 deletions(-) (limited to 'internal/handler/crud.go') diff --git a/internal/handler/crud.go b/internal/handler/crud.go index 4ab0c7f..b12f68a 100644 --- a/internal/handler/crud.go +++ b/internal/handler/crud.go @@ -1,37 +1,35 @@ package handler import ( + "fmt" "io" "net/http" - "time" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" - "go.neonxp.dev/djson/internal/model" - "go.neonxp.dev/json" + "go.neonxp.dev/djson/internal/command" + "go.neonxp.dev/objectid" ) -func (h *handler) HandleCRUD(r chi.Router) { - r.Use(middleware.CleanPath) - r.Use(middleware.StripSlashes) +func (h *handler) HandleCRUD(router chi.Router) { + router.Use(middleware.CleanPath) + router.Use(middleware.StripSlashes) - r.Get("/*", func(w http.ResponseWriter, r *http.Request) { + router.Get("/*", func(w http.ResponseWriter, r *http.Request) { rctx := chi.RouteContext(r.Context()) - res, err := h.core.Get(parsePath(rctx.RoutePath)) - if err != nil { - writeError(http.StatusNotFound, err, w) - return - } - result, err := res.MarshalJSON() + path := parsePath(rctx.RoutePath) + + node, err := h.core.Query(r.Context(), path) if err != nil { - writeError(http.StatusInternalServerError, err, w) + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error()))) return } w.WriteHeader(http.StatusOK) - _, _ = w.Write(result) + _, _ = w.Write([]byte(node.String())) }) - r.Post("/*", func(w http.ResponseWriter, r *http.Request) { + router.Post("/*", func(w http.ResponseWriter, r *http.Request) { rctx := chi.RouteContext(r.Context()) path := parsePath(rctx.RoutePath) jsonBody, err := io.ReadAll(r.Body) @@ -40,25 +38,23 @@ func (h *handler) HandleCRUD(r chi.Router) { return } r.Body.Close() - node, err := json.Unmarshal(jsonBody) - if err != nil { - writeError(http.StatusBadRequest, err, w) - return - } - mutation := &model.Mutation{ - Date: time.Now(), - Type: model.Create, + + mutation := command.Mutation{ + ID: objectid.New(), + Type: command.Create, Path: path, - Body: node, + Data: string(jsonBody), } - if err := h.core.Mutate(r.Context(), mutation); err != nil { - writeError(http.StatusInternalServerError, err, w) + if err := h.core.Apply(r.Context(), &mutation); err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error()))) return } + w.WriteHeader(http.StatusCreated) }) - r.Patch("/*", func(w http.ResponseWriter, r *http.Request) { + router.Patch("/*", func(w http.ResponseWriter, r *http.Request) { rctx := chi.RouteContext(r.Context()) path := parsePath(rctx.RoutePath) jsonBody, err := io.ReadAll(r.Body) @@ -67,37 +63,37 @@ func (h *handler) HandleCRUD(r chi.Router) { return } r.Body.Close() - node, err := json.Unmarshal(jsonBody) - if err != nil { - writeError(http.StatusBadRequest, err, w) - return - } - mutation := &model.Mutation{ - Date: time.Now(), - Type: model.Merge, + + mutation := command.Mutation{ + ID: objectid.New(), + Type: command.Merge, Path: path, - Body: node, + Data: string(jsonBody), } - if err := h.core.Mutate(r.Context(), mutation); err != nil { - writeError(http.StatusInternalServerError, err, w) + if err := h.core.Apply(r.Context(), &mutation); err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error()))) return } + w.WriteHeader(http.StatusOK) }) - r.Delete("/*", func(w http.ResponseWriter, r *http.Request) { + router.Delete("/*", func(w http.ResponseWriter, r *http.Request) { rctx := chi.RouteContext(r.Context()) path := parsePath(rctx.RoutePath) - mutation := &model.Mutation{ - Date: time.Now(), - Type: model.Remove, + + mutation := command.Mutation{ + ID: objectid.New(), + Type: command.Remove, Path: path, - Body: nil, } - if err := h.core.Mutate(r.Context(), mutation); err != nil { - writeError(http.StatusInternalServerError, err, w) + if err := h.core.Apply(r.Context(), &mutation); err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error()))) return } + w.WriteHeader(http.StatusNoContent) }) } -- cgit v1.2.3