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

@ -9,6 +9,7 @@ import (
"net/http"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
@ -51,7 +52,6 @@ func (h apikeyHandler) list(w http.ResponseWriter, r *http.Request) {
func (h apikeyHandler) store(w http.ResponseWriter, r *http.Request) {
var data domain.APIKey
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
@ -66,7 +66,14 @@ func (h apikeyHandler) store(w http.ResponseWriter, r *http.Request) {
}
func (h apikeyHandler) delete(w http.ResponseWriter, r *http.Request) {
if err := h.service.Delete(r.Context(), chi.URLParam(r, "apikey")); err != nil {
apiKey := chi.URLParam(r, "apikey")
if err := h.service.Delete(r.Context(), apiKey); err != nil {
if errors.Is(err, domain.ErrRecordNotFound) {
h.encoder.NotFoundErr(w, errors.New("api key %s not found", apiKey))
return
}
h.encoder.Error(w, err)
return
}