mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
fix: store filter actions (#54)
This commit is contained in:
parent
e496027798
commit
e1ef47e09a
10 changed files with 213 additions and 73 deletions
|
@ -1,6 +1,7 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
@ -12,7 +13,7 @@ import (
|
|||
|
||||
type actionService interface {
|
||||
Fetch() ([]domain.Action, error)
|
||||
Store(action domain.Action) (*domain.Action, error)
|
||||
Store(ctx context.Context, action domain.Action) (*domain.Action, error)
|
||||
Delete(actionID int) error
|
||||
ToggleEnabled(actionID int) error
|
||||
}
|
||||
|
@ -47,61 +48,71 @@ func (h actionHandler) getActions(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h actionHandler) storeAction(w http.ResponseWriter, r *http.Request) {
|
||||
var data domain.Action
|
||||
var (
|
||||
data domain.Action
|
||||
ctx = r.Context()
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
action, err := h.service.Store(data)
|
||||
action, err := h.service.Store(ctx, data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(r.Context(), w, action, http.StatusCreated)
|
||||
h.encoder.StatusResponse(ctx, w, action, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h actionHandler) updateAction(w http.ResponseWriter, r *http.Request) {
|
||||
var data domain.Action
|
||||
var (
|
||||
data domain.Action
|
||||
ctx = r.Context()
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
action, err := h.service.Store(data)
|
||||
action, err := h.service.Store(ctx, data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(r.Context(), w, action, http.StatusCreated)
|
||||
h.encoder.StatusResponse(ctx, w, action, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h actionHandler) deleteAction(w http.ResponseWriter, r *http.Request) {
|
||||
var ctx = r.Context()
|
||||
|
||||
actionID, err := parseInt(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(r.Context(), w, errors.New("bad param id"), http.StatusBadRequest)
|
||||
h.encoder.StatusResponse(ctx, w, errors.New("bad param id"), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if err := h.service.Delete(actionID); err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(r.Context(), w, nil, http.StatusNoContent)
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h actionHandler) toggleActionEnabled(w http.ResponseWriter, r *http.Request) {
|
||||
var ctx = r.Context()
|
||||
|
||||
actionID, err := parseInt(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(r.Context(), w, errors.New("bad param id"), http.StatusBadRequest)
|
||||
h.encoder.StatusResponse(ctx, w, errors.New("bad param id"), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if err := h.service.ToggleEnabled(actionID); err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(r.Context(), w, nil, http.StatusCreated)
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
}
|
||||
|
||||
func parseInt(s string) (int, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue