feat(http): implement missing findByID methods (#1635)

* feat(http): implement missing methods

* general cleanup
* unify param handling
* handle not found errors
* unify err handlers

* fix(http): fmt type
This commit is contained in:
ze0s 2024-08-29 12:22:03 +02:00 committed by GitHub
parent accc875960
commit acb91e8709
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 379 additions and 422 deletions

View file

@ -38,7 +38,7 @@ func (h actionHandler) Routes(r chi.Router) {
r.Get("/", h.getActions)
r.Post("/", h.storeAction)
r.Route("/{id}", func(r chi.Router) {
r.Route("/{actionID}", func(r chi.Router) {
r.Delete("/", h.deleteAction)
r.Put("/", h.updateAction)
r.Patch("/toggleEnabled", h.toggleActionEnabled)
@ -56,17 +56,13 @@ func (h actionHandler) getActions(w http.ResponseWriter, r *http.Request) {
}
func (h actionHandler) storeAction(w http.ResponseWriter, r *http.Request) {
var (
data domain.Action
ctx = r.Context()
)
var data domain.Action
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
action, err := h.service.Store(ctx, data)
action, err := h.service.Store(r.Context(), data)
if err != nil {
h.encoder.Error(w, err)
return
@ -76,17 +72,13 @@ func (h actionHandler) storeAction(w http.ResponseWriter, r *http.Request) {
}
func (h actionHandler) updateAction(w http.ResponseWriter, r *http.Request) {
var (
data domain.Action
ctx = r.Context()
)
var data domain.Action
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
action, err := h.service.Store(ctx, data)
action, err := h.service.Store(r.Context(), data)
if err != nil {
h.encoder.Error(w, err)
return
@ -96,7 +88,7 @@ func (h actionHandler) updateAction(w http.ResponseWriter, r *http.Request) {
}
func (h actionHandler) deleteAction(w http.ResponseWriter, r *http.Request) {
actionID, err := parseInt(chi.URLParam(r, "id"))
actionID, err := parseInt(chi.URLParam(r, "actionID"))
if err != nil {
h.encoder.StatusError(w, http.StatusBadRequest, errors.New("bad param id"))
return
@ -111,7 +103,7 @@ func (h actionHandler) deleteAction(w http.ResponseWriter, r *http.Request) {
}
func (h actionHandler) toggleActionEnabled(w http.ResponseWriter, r *http.Request) {
actionID, err := parseInt(chi.URLParam(r, "id"))
actionID, err := parseInt(chi.URLParam(r, "actionID"))
if err != nil {
h.encoder.StatusError(w, http.StatusBadRequest, errors.New("bad param id"))
return