mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat: add notifications (#216)
* feat: initial notifications support * chore: update deps
This commit is contained in:
parent
3185832708
commit
431742fd94
20 changed files with 1632 additions and 36 deletions
106
internal/http/notification.go
Normal file
106
internal/http/notification.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
type notificationService interface {
|
||||
Find(context.Context, domain.NotificationQueryParams) ([]domain.Notification, int, error)
|
||||
FindByID(ctx context.Context, id int) (*domain.Notification, error)
|
||||
Store(ctx context.Context, n domain.Notification) (*domain.Notification, error)
|
||||
Update(ctx context.Context, n domain.Notification) (*domain.Notification, error)
|
||||
Delete(ctx context.Context, id int) error
|
||||
}
|
||||
|
||||
type notificationHandler struct {
|
||||
encoder encoder
|
||||
service notificationService
|
||||
}
|
||||
|
||||
func newNotificationHandler(encoder encoder, service notificationService) *notificationHandler {
|
||||
return ¬ificationHandler{
|
||||
encoder: encoder,
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
func (h notificationHandler) Routes(r chi.Router) {
|
||||
r.Get("/", h.list)
|
||||
r.Post("/", h.store)
|
||||
r.Put("/{notificationID}", h.update)
|
||||
r.Delete("/{notificationID}", h.delete)
|
||||
}
|
||||
|
||||
func (h notificationHandler) list(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
list, _, err := h.service.Find(ctx, domain.NotificationQueryParams{})
|
||||
if err != nil {
|
||||
h.encoder.StatusNotFound(ctx, w)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, list, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h notificationHandler) store(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Notification
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
filter, err := h.service.Store(ctx, data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, filter, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h notificationHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Notification
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
filter, err := h.service.Update(ctx, data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, filter, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h notificationHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
notificationID = chi.URLParam(r, "notificationID")
|
||||
)
|
||||
|
||||
id, _ := strconv.Atoi(notificationID)
|
||||
|
||||
if err := h.service.Delete(ctx, id); err != nil {
|
||||
// return err
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
|
@ -31,10 +31,11 @@ type Server struct {
|
|||
filterService filterService
|
||||
indexerService indexerService
|
||||
ircService ircService
|
||||
notificationService notificationService
|
||||
releaseService releaseService
|
||||
}
|
||||
|
||||
func NewServer(config domain.Config, sse *sse.Server, version string, commit string, date string, actionService actionService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService, releaseSvc releaseService) Server {
|
||||
func NewServer(config domain.Config, sse *sse.Server, version string, commit string, date string, actionService actionService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService, notificationSvc notificationService, releaseSvc releaseService) Server {
|
||||
return Server{
|
||||
config: config,
|
||||
sse: sse,
|
||||
|
@ -50,6 +51,7 @@ func NewServer(config domain.Config, sse *sse.Server, version string, commit str
|
|||
filterService: filterSvc,
|
||||
indexerService: indexerSvc,
|
||||
ircService: ircSvc,
|
||||
notificationService: notificationSvc,
|
||||
releaseService: releaseSvc,
|
||||
}
|
||||
}
|
||||
|
@ -107,6 +109,7 @@ func (s Server) Handler() http.Handler {
|
|||
r.Route("/filters", newFilterHandler(encoder, s.filterService).Routes)
|
||||
r.Route("/irc", newIrcHandler(encoder, s.ircService).Routes)
|
||||
r.Route("/indexer", newIndexerHandler(encoder, s.indexerService, s.ircService).Routes)
|
||||
r.Route("/notification", newNotificationHandler(encoder, s.notificationService).Routes)
|
||||
r.Route("/release", newReleaseHandler(encoder, s.releaseService).Routes)
|
||||
|
||||
r.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue