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
|
@ -7,6 +7,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -76,30 +77,29 @@ func (h ircHandler) Routes(r chi.Router) {
|
|||
}
|
||||
|
||||
func (h ircHandler) listNetworks(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
networks, err := h.service.GetNetworksWithHealth(ctx)
|
||||
networks, err := h.service.GetNetworksWithHealth(r.Context())
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(w, http.StatusOK, networks)
|
||||
}
|
||||
|
||||
func (h ircHandler) getNetworkByID(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
)
|
||||
|
||||
id, err := strconv.Atoi(networkID)
|
||||
networkID, err := strconv.Atoi(chi.URLParam(r, "networkID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
network, err := h.service.GetNetworkByID(ctx, int64(id))
|
||||
network, err := h.service.GetNetworkByID(r.Context(), int64(networkID))
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrRecordNotFound) {
|
||||
h.encoder.NotFoundErr(w, errors.New("network with id %d not found", networkID))
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
@ -108,18 +108,18 @@ func (h ircHandler) getNetworkByID(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h ircHandler) restartNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
)
|
||||
|
||||
id, err := strconv.Atoi(networkID)
|
||||
networkID, err := strconv.Atoi(chi.URLParam(r, "networkID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.RestartNetwork(ctx, int64(id)); err != nil {
|
||||
if err := h.service.RestartNetwork(r.Context(), int64(networkID)); err != nil {
|
||||
if errors.Is(err, domain.ErrRecordNotFound) {
|
||||
h.encoder.NotFoundErr(w, errors.New("network with id %d not found", networkID))
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
@ -129,7 +129,6 @@ func (h ircHandler) restartNetwork(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func (h ircHandler) storeNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
var data domain.IrcNetwork
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
|
@ -144,17 +143,13 @@ func (h ircHandler) storeNetwork(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h ircHandler) updateNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.IrcNetwork
|
||||
)
|
||||
|
||||
var data domain.IrcNetwork
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.UpdateNetwork(ctx, &data); err != nil {
|
||||
if err := h.service.UpdateNetwork(r.Context(), &data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
@ -163,26 +158,21 @@ func (h ircHandler) updateNetwork(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h ircHandler) sendCmd(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
data domain.SendIrcCmdRequest
|
||||
)
|
||||
|
||||
id, err := strconv.Atoi(networkID)
|
||||
networkID, err := strconv.Atoi(chi.URLParam(r, "networkID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var data domain.SendIrcCmdRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
data.NetworkId = int64(id)
|
||||
data.NetworkId = int64(networkID)
|
||||
|
||||
if err := h.service.SendCmd(ctx, &data); err != nil {
|
||||
if err := h.service.SendCmd(r.Context(), &data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
@ -192,49 +182,32 @@ func (h ircHandler) sendCmd(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// announceProcess manually trigger announce process
|
||||
func (h ircHandler) announceProcess(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.IRCManualProcessRequest
|
||||
)
|
||||
|
||||
paramNetworkID := chi.URLParam(r, "networkID")
|
||||
if paramNetworkID == "" {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "parameter networkID missing",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
networkID, err := strconv.Atoi(paramNetworkID)
|
||||
networkID, err := strconv.Atoi(chi.URLParam(r, "networkID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
paramChannel := chi.URLParam(r, "channel")
|
||||
if paramChannel == "" {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "parameter channel missing",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var data domain.IRCManualProcessRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
data.NetworkId = int64(networkID)
|
||||
data.Channel = paramChannel
|
||||
data.Channel = chi.URLParam(r, "channel")
|
||||
|
||||
// we cant pass # as an url parameter so the frontend has to strip it
|
||||
if !strings.HasPrefix("#", data.Channel) {
|
||||
if !strings.HasPrefix(data.Channel, "#") {
|
||||
data.Channel = fmt.Sprintf("#%s", data.Channel)
|
||||
}
|
||||
|
||||
if err := h.service.ManualProcessAnnounce(ctx, &data); err != nil {
|
||||
if err := h.service.ManualProcessAnnounce(r.Context(), &data); err != nil {
|
||||
if errors.Is(err, domain.ErrRecordNotFound) {
|
||||
h.encoder.NotFoundErr(w, errors.New("network with id %d not found", data.NetworkId))
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
@ -243,24 +216,19 @@ func (h ircHandler) announceProcess(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h ircHandler) storeChannel(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
data domain.IrcChannel
|
||||
)
|
||||
|
||||
id, err := strconv.Atoi(networkID)
|
||||
networkID, err := strconv.Atoi(chi.URLParam(r, "networkID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var data domain.IrcChannel
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.StoreChannel(ctx, int64(id), &data); err != nil {
|
||||
if err := h.service.StoreChannel(r.Context(), int64(networkID), &data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
@ -269,18 +237,18 @@ func (h ircHandler) storeChannel(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h ircHandler) deleteNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
)
|
||||
|
||||
id, err := strconv.Atoi(networkID)
|
||||
networkID, err := strconv.Atoi(chi.URLParam(r, "networkID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.DeleteNetwork(ctx, int64(id)); err != nil {
|
||||
if err := h.service.DeleteNetwork(r.Context(), int64(networkID)); err != nil {
|
||||
if errors.Is(err, domain.ErrRecordNotFound) {
|
||||
h.encoder.NotFoundErr(w, errors.New("network with id %d does not exist", networkID))
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue