mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
Refactor irc client (#19)
* refactor: update http handlers * feat: add trace log level * refactir: irc handler * refactor(definitions): add irc settings and invite cmd: * feat: add dft values to inputs * refactor: indexer irc forms * refactor(definitions): fix nickserv.password var: * feat: pre fill indexer name field * refactor: handle stopping and updates
This commit is contained in:
parent
5f69ae9380
commit
4d40d41628
48 changed files with 1380 additions and 943 deletions
|
@ -2,6 +2,7 @@ package http
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
@ -17,97 +18,96 @@ type actionService interface {
|
|||
}
|
||||
|
||||
type actionHandler struct {
|
||||
encoder encoder
|
||||
actionService actionService
|
||||
encoder encoder
|
||||
service actionService
|
||||
}
|
||||
|
||||
func newActionHandler(encoder encoder, service actionService) *actionHandler {
|
||||
return &actionHandler{
|
||||
encoder: encoder,
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
func (h actionHandler) Routes(r chi.Router) {
|
||||
r.Get("/", h.getActions)
|
||||
r.Post("/", h.storeAction)
|
||||
r.Delete("/{actionID}", h.deleteAction)
|
||||
r.Put("/{actionID}", h.updateAction)
|
||||
r.Patch("/{actionID}/toggleEnabled", h.toggleActionEnabled)
|
||||
r.Delete("/{id}", h.deleteAction)
|
||||
r.Put("/{id}", h.updateAction)
|
||||
r.Patch("/{id}/toggleEnabled", h.toggleActionEnabled)
|
||||
}
|
||||
|
||||
func (h actionHandler) getActions(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
actions, err := h.actionService.Fetch()
|
||||
actions, err := h.service.Fetch()
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, actions, http.StatusOK)
|
||||
h.encoder.StatusResponse(r.Context(), w, actions, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h actionHandler) storeAction(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Action
|
||||
)
|
||||
var data domain.Action
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
action, err := h.actionService.Store(data)
|
||||
action, err := h.service.Store(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, action, http.StatusCreated)
|
||||
h.encoder.StatusResponse(r.Context(), w, action, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h actionHandler) updateAction(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Action
|
||||
)
|
||||
var data domain.Action
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
action, err := h.actionService.Store(data)
|
||||
action, err := h.service.Store(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, action, http.StatusCreated)
|
||||
h.encoder.StatusResponse(r.Context(), w, action, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h actionHandler) deleteAction(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
actionID = chi.URLParam(r, "actionID")
|
||||
)
|
||||
actionID, err := parseInt(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(r.Context(), w, errors.New("bad param id"), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
// if !actionID return error
|
||||
|
||||
id, _ := strconv.Atoi(actionID)
|
||||
|
||||
if err := h.actionService.Delete(id); err != nil {
|
||||
if err := h.service.Delete(actionID); err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
h.encoder.StatusResponse(r.Context(), w, nil, http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h actionHandler) toggleActionEnabled(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
actionID = chi.URLParam(r, "actionID")
|
||||
)
|
||||
actionID, err := parseInt(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(r.Context(), w, errors.New("bad param id"), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
// if !actionID return error
|
||||
|
||||
id, _ := strconv.Atoi(actionID)
|
||||
|
||||
if err := h.actionService.ToggleEnabled(id); err != nil {
|
||||
if err := h.service.ToggleEnabled(actionID); err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
h.encoder.StatusResponse(r.Context(), w, nil, http.StatusCreated)
|
||||
}
|
||||
|
||||
func parseInt(s string) (int, error) {
|
||||
u, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(u), nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue