mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
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:
parent
accc875960
commit
acb91e8709
15 changed files with 379 additions and 422 deletions
|
@ -6,6 +6,7 @@ package http
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
@ -41,15 +42,14 @@ func (h notificationHandler) Routes(r chi.Router) {
|
|||
r.Post("/test", h.test)
|
||||
|
||||
r.Route("/{notificationID}", func(r chi.Router) {
|
||||
r.Get("/", h.findByID)
|
||||
r.Put("/", h.update)
|
||||
r.Delete("/", h.delete)
|
||||
})
|
||||
}
|
||||
|
||||
func (h notificationHandler) list(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
list, _, err := h.service.Find(ctx, domain.NotificationQueryParams{})
|
||||
list, _, err := h.service.Find(r.Context(), domain.NotificationQueryParams{})
|
||||
if err != nil {
|
||||
h.encoder.StatusNotFound(w)
|
||||
return
|
||||
|
@ -59,17 +59,13 @@ func (h notificationHandler) list(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h notificationHandler) store(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Notification
|
||||
)
|
||||
|
||||
var data domain.Notification
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
filter, err := h.service.Store(ctx, data)
|
||||
filter, err := h.service.Store(r.Context(), data)
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
|
@ -78,18 +74,35 @@ func (h notificationHandler) store(w http.ResponseWriter, r *http.Request) {
|
|||
h.encoder.StatusResponse(w, http.StatusCreated, filter)
|
||||
}
|
||||
|
||||
func (h notificationHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Notification
|
||||
)
|
||||
func (h notificationHandler) findByID(w http.ResponseWriter, r *http.Request) {
|
||||
notificationID, err := strconv.Atoi(chi.URLParam(r, "notificationID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
notification, err := h.service.FindByID(r.Context(), notificationID)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrRecordNotFound) {
|
||||
h.encoder.NotFoundErr(w, errors.New("notification with id %d not found", notificationID))
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(w, http.StatusNoContent, notification)
|
||||
}
|
||||
|
||||
func (h notificationHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
var data domain.Notification
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
filter, err := h.service.Update(ctx, data)
|
||||
filter, err := h.service.Update(r.Context(), data)
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
|
@ -99,14 +112,13 @@ func (h notificationHandler) update(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h notificationHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
notificationID = chi.URLParam(r, "notificationID")
|
||||
)
|
||||
notificationID, err := strconv.Atoi(chi.URLParam(r, "notificationID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
id, _ := strconv.Atoi(notificationID)
|
||||
|
||||
if err := h.service.Delete(ctx, id); err != nil {
|
||||
if err := h.service.Delete(r.Context(), notificationID); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
@ -115,17 +127,13 @@ func (h notificationHandler) delete(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h notificationHandler) test(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Notification
|
||||
)
|
||||
|
||||
var data domain.Notification
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.Test(ctx, data); err != nil {
|
||||
if err := h.service.Test(r.Context(), data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue