mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
fix: no irc network created when adding a new indexer (#177)
This commit is contained in:
parent
aa196d8104
commit
8bf43dc1e0
6 changed files with 56 additions and 27 deletions
|
@ -6,7 +6,11 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
type encoder struct {
|
||||
type encoder struct{}
|
||||
|
||||
type errorResponse struct {
|
||||
Message string `json:"message"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
func (e encoder) StatusResponse(ctx context.Context, w http.ResponseWriter, response interface{}, status int) {
|
||||
|
@ -14,14 +18,19 @@ func (e encoder) StatusResponse(ctx context.Context, w http.ResponseWriter, resp
|
|||
w.Header().Set("Content-Type", "application/json; charset=utf=8")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
// log err
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(status)
|
||||
}
|
||||
}
|
||||
|
||||
func (e encoder) StatusNoContent(w http.ResponseWriter) {
|
||||
func (e encoder) StatusCreated(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
func (e encoder) NoContent(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
|
@ -32,3 +41,13 @@ func (e encoder) StatusNotFound(ctx context.Context, w http.ResponseWriter) {
|
|||
func (e encoder) StatusInternalError(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func (e encoder) Error(w http.ResponseWriter, err error) {
|
||||
res := errorResponse{
|
||||
Message: err.Error(),
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf=8")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(res)
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ func (h ircHandler) listNetworks(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
networks, err := h.service.GetNetworksWithHealth(ctx)
|
||||
if err != nil {
|
||||
//
|
||||
h.encoder.Error(w, err)
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, networks, http.StatusOK)
|
||||
|
@ -63,30 +63,27 @@ func (h ircHandler) getNetworkByID(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
network, err := h.service.GetNetworkByID(int64(id))
|
||||
if err != nil {
|
||||
//
|
||||
h.encoder.Error(w, err)
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, network, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h ircHandler) storeNetwork(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
|
||||
}
|
||||
|
||||
err := h.service.StoreNetwork(ctx, &data)
|
||||
err := h.service.StoreNetwork(r.Context(), &data)
|
||||
if err != nil {
|
||||
//
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusBadRequest)
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
h.encoder.NoContent(w)
|
||||
}
|
||||
|
||||
func (h ircHandler) updateNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -96,22 +93,21 @@ func (h ircHandler) updateNetwork(w http.ResponseWriter, r *http.Request) {
|
|||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.service.UpdateNetwork(ctx, &data)
|
||||
if err != nil {
|
||||
//
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusBadRequest)
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
h.encoder.NoContent(w)
|
||||
}
|
||||
|
||||
func (h ircHandler) storeChannel(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.IrcChannel
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
)
|
||||
|
@ -119,15 +115,17 @@ func (h ircHandler) storeChannel(w http.ResponseWriter, r *http.Request) {
|
|||
id, _ := strconv.Atoi(networkID)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.service.StoreChannel(int64(id), &data)
|
||||
if err != nil {
|
||||
//
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
h.encoder.NoContent(w)
|
||||
}
|
||||
|
||||
func (h ircHandler) deleteNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -140,8 +138,9 @@ func (h ircHandler) deleteNetwork(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
err := h.service.DeleteNetwork(ctx, int64(id))
|
||||
if err != nil {
|
||||
//
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
h.encoder.NoContent(w)
|
||||
}
|
||||
|
|
|
@ -145,5 +145,5 @@ func (h releaseHandler) deleteReleases(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusNoContent(w)
|
||||
h.encoder.NoContent(w)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue