-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
notification.go
127 lines (103 loc) · 2.79 KB
/
notification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package http
import (
"context"
"encoding/json"
"github.com/SyncYomi/SyncYomi/internal/domain"
"github.com/go-chi/chi/v5"
"net/http"
"strconv"
)
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
Test(ctx context.Context, notification domain.Notification) 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.Post("/test", h.test)
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)
}
func (h notificationHandler) test(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
h.encoder.Error(w, err)
return
}
err := h.service.Test(ctx, data)
if err != nil {
h.encoder.Error(w, err)
return
}
h.encoder.NoContent(w)
}