addhandler.go 575 B

1234567891011121314151617181920212223242526272829
  1. package handler
  2. import (
  3. "net/http"
  4. "bookstore/api/internal/logic"
  5. "bookstore/api/internal/svc"
  6. "bookstore/api/internal/types"
  7. "github.com/tal-tech/go-zero/rest/httpx"
  8. )
  9. func addHandler(ctx *svc.ServiceContext) http.HandlerFunc {
  10. return func(w http.ResponseWriter, r *http.Request) {
  11. var req types.AddReq
  12. if err := httpx.Parse(r, &req); err != nil {
  13. httpx.Error(w, err)
  14. return
  15. }
  16. l := logic.NewAddLogic(r.Context(), ctx)
  17. resp, err := l.Add(req)
  18. if err != nil {
  19. httpx.Error(w, err)
  20. } else {
  21. httpx.WriteJson(w, http.StatusOK, resp)
  22. }
  23. }
  24. }