feat: add notifications (#216)

* feat: initial notifications support

* chore: update deps
This commit is contained in:
Ludvig Lundgren 2022-04-04 19:13:09 +02:00 committed by GitHub
parent 3185832708
commit 431742fd94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 1632 additions and 36 deletions

View 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 &notificationHandler{
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)
}