/
announcement.go
168 lines (142 loc) · 3.73 KB
/
announcement.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package web
import (
"bytes"
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/BoilerMake/bm-app/internal/models"
"github.com/BoilerMake/bm-app/pkg/flash"
"github.com/go-chi/chi"
)
type idMessage struct {
ID int `json:"id"`
}
type slackMessage struct {
Text string `json:"text"`
}
func (h *Handler) postAnnouncement() http.HandlerFunc {
webhookURL := mustGetEnv("SLACK_ANNOUNCEMENTS_WEBHOOK")
mode := mustGetEnv("ENV_MODE")
return func(w http.ResponseWriter, r *http.Request) {
var message string
message = r.FormValue("message")
// Check if message is empty or not
if message == "" {
w.WriteHeader(http.StatusBadRequest)
h.Error(w, r, models.ErrAnnouncementMessageEmpty, "")
return
}
err := h.AnnouncementService.Create(message)
if err != nil {
h.Error(w, r, err, "")
return
}
if mode != "development" {
slackMsg, err := json.Marshal(slackMessage{Text: message})
req, err := http.NewRequest(http.MethodPost, webhookURL, bytes.NewBuffer(slackMsg))
if err != nil {
h.Error(w, r, err, "")
return
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
h.Error(w, r, err, "")
return
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if buf.String() != "ok" {
h.Error(w, r, err, "")
return
}
}
session := h.getSession(r)
// Show flash that everything went well
session.AddFlash(flash.Flash{
Type: flash.Success,
Message: "Announcement posted, you're doing great!",
})
session.Save(r, w)
// Redirect to exec page
http.Redirect(w, r, "/exec", http.StatusSeeOther)
}
}
func (h *Handler) getAnnouncement() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
currentAnnouncement, err := h.AnnouncementService.GetCurrent()
if err == models.ErrNoAnnouncements {
w.WriteHeader(http.StatusNoContent)
h.Error(w, r, err, "")
return
}
if err != nil {
h.Error(w, r, err, "")
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(currentAnnouncement)
}
}
func (h *Handler) getAnnouncements() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
announcements, err := h.AnnouncementService.GetAllAnnouncements() // returns an array of pointers to announcement structs
if err != nil {
if err == models.ErrNoAnnouncements {
w.WriteHeader(http.StatusNoContent)
h.Error(w, r, models.ErrNoAnnouncements, "/")
return
}
h.Error(w, r, err, "")
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(announcements)
}
}
func (h *Handler) getAnnouncementWithID() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
w.WriteHeader(http.StatusNoContent)
h.Error(w, r, err, "")
return
}
ann, err := h.AnnouncementService.GetByID(id)
if err == models.ErrNoAnnouncements {
w.WriteHeader(http.StatusNoContent)
h.Error(w, r, err, "")
return
}
if err != nil {
h.Error(w, r, err, "")
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(ann)
}
}
func (h *Handler) deleteAnnouncement() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var m idMessage
err := json.NewDecoder(r.Body).Decode(&m)
if err != nil {
h.Error(w, r, err, "")
return
}
// Check if id is empty
if m.ID == 0 {
w.WriteHeader(http.StatusBadRequest)
h.Error(w, r, models.ErrAnnouncementIDEmpty, "")
return
}
err = h.AnnouncementService.DeleteByID(m.ID)
if err != nil {
h.Error(w, r, err, "")
return
}
w.WriteHeader(http.StatusOK)
}
}