mirror of
https://github.com/idanoo/autobrr
synced 2025-07-22 16:29:12 +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,17 +6,18 @@ package http
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type downloadClientService interface {
|
||||
List(ctx context.Context) ([]domain.DownloadClient, error)
|
||||
FindByID(ctx context.Context, id int32) (*domain.DownloadClient, error)
|
||||
Store(ctx context.Context, client *domain.DownloadClient) error
|
||||
Update(ctx context.Context, client *domain.DownloadClient) error
|
||||
Delete(ctx context.Context, clientID int32) error
|
||||
|
@ -40,13 +41,15 @@ func (h downloadClientHandler) Routes(r chi.Router) {
|
|||
r.Post("/", h.store)
|
||||
r.Put("/", h.update)
|
||||
r.Post("/test", h.test)
|
||||
r.Delete("/{clientID}", h.delete)
|
||||
|
||||
r.Route("/{clientID}", func(r chi.Router) {
|
||||
r.Get("/", h.findByID)
|
||||
r.Delete("/", h.delete)
|
||||
})
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) listDownloadClients(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
clients, err := h.service.List(ctx)
|
||||
clients, err := h.service.List(r.Context())
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
|
@ -55,9 +58,28 @@ func (h downloadClientHandler) listDownloadClients(w http.ResponseWriter, r *htt
|
|||
h.encoder.StatusResponse(w, http.StatusOK, clients)
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) findByID(w http.ResponseWriter, r *http.Request) {
|
||||
clientID, err := strconv.ParseInt(chi.URLParam(r, "clientID"), 10, 32)
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
client, err := h.service.FindByID(r.Context(), int32(clientID))
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrRecordNotFound) {
|
||||
h.encoder.NotFoundErr(w, errors.New("download client with id %d not found", clientID))
|
||||
}
|
||||
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(w, http.StatusOK, client)
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) store(w http.ResponseWriter, r *http.Request) {
|
||||
var data *domain.DownloadClient
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
|
@ -74,7 +96,6 @@ func (h downloadClientHandler) store(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func (h downloadClientHandler) test(w http.ResponseWriter, r *http.Request) {
|
||||
var data domain.DownloadClient
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
|
@ -90,7 +111,6 @@ func (h downloadClientHandler) test(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func (h downloadClientHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
var data *domain.DownloadClient
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
|
@ -106,20 +126,13 @@ func (h downloadClientHandler) update(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h downloadClientHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
var clientID = chi.URLParam(r, "clientID")
|
||||
|
||||
if clientID == "" {
|
||||
h.encoder.Error(w, errors.New("no clientID given"))
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(clientID, 10, 32)
|
||||
clientID, err := strconv.ParseInt(chi.URLParam(r, "clientID"), 10, 32)
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = h.service.Delete(r.Context(), int32(id)); err != nil {
|
||||
if err = h.service.Delete(r.Context(), int32(clientID)); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue