/
eventController.go
186 lines (156 loc) · 5.75 KB
/
eventController.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package insapp
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2/bson"
)
// GetEventController will answer a JSON of the event
// from the given "id" in the URL. (cf Routes in routes.go)
func GetEventController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
associationID := vars["id"]
var res = GetEvent(bson.ObjectIdHex(associationID))
_ = json.NewEncoder(w).Encode(res)
}
// GetFutureEventsController will answer a JSON
// containing all future events from "NOW"
func GetFutureEventsController(w http.ResponseWriter, r *http.Request) {
id, err := GetUserFromRequest(r)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
json.NewEncoder(w).Encode(bson.M{"error": "could not get user ID"})
return
}
user := GetUser(id)
os := GetNotificationUserForUser(id).Os
events := GetFutureEvents()
res := Events{}
if user.ID != "" {
for _, event := range events {
if contains(strings.ToUpper(user.Promotion), event.Promotions) && (contains(os, event.Plateforms) || os == "") || len(event.Promotions) == 0 || len(event.Plateforms) == 0 {
res = append(res, event)
}
}
} else {
res = events
}
json.NewEncoder(w).Encode(res)
}
func GetEventsForAssociationController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
associationID := vars["id"]
events := GetEventsForAssociation(bson.ObjectIdHex(associationID))
_ = json.NewEncoder(w).Encode(events)
}
// AddEventController will answer the JSON
// of the brand new created Event from the JSON body
// Should be protected
func AddEventController(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var event Event
_ = decoder.Decode(&event)
res := AddEvent(event)
association := GetAssociation(event.Association)
_ = json.NewEncoder(w).Encode(res)
go TriggerNotificationForEvent(event, association.ID, res.ID, "@"+strings.ToLower(association.Name)+" t'invite à "+res.Name+" 📅")
}
// UpdateEventController will answer the JSON
// of the modified Event from the JSON body
// Should be protected
func UpdateEventController(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var event Event
_ = decoder.Decode(&event)
vars := mux.Vars(r)
eventID := vars["id"]
res := UpdateEvent(bson.ObjectIdHex(eventID), event)
_ = json.NewEncoder(w).Encode(res)
}
// DeleteEventController will answer an empty JSON
// if the deletion has succeed
// Should be protected
func DeleteEventController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
event := GetEvent(bson.ObjectIdHex(vars["id"]))
res := DeleteEvent(event)
_ = json.NewEncoder(w).Encode(res)
}
func AddAttendeeController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventID := bson.ObjectIdHex(vars["id"])
userID := bson.ObjectIdHex(vars["userID"])
event, user := AddAttendeeToGoingList(eventID, userID)
_ = json.NewEncoder(w).Encode(bson.M{"event": event, "user": user})
}
// AddAttendeeController will answer the JSON
// of the event with the given attendee added
func ChangeAttendeeStatusController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventID := bson.ObjectIdHex(vars["id"])
userID := bson.ObjectIdHex(vars["userID"])
status := vars["status"]
if status == "going" {
event, user := AddAttendeeToGoingList(eventID, userID)
_ = json.NewEncoder(w).Encode(bson.M{"event": event, "user": user})
} else if status == "maybe" {
event, user := AddAttendeeToMaybeList(eventID, userID)
_ = json.NewEncoder(w).Encode(bson.M{"event": event, "user": user})
} else if status == "notgoing" {
event, user := AddAttendeeToNotGoingList(eventID, userID)
_ = json.NewEncoder(w).Encode(bson.M{"event": event, "user": user})
} else {
w.WriteHeader(http.StatusNotAcceptable)
_ = json.NewEncoder(w).Encode(bson.M{"error": "bad status"})
}
}
// RemoveAttendeeController will answer the JSON
// of the event without the given attendee added
func RemoveAttendeeController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventID := bson.ObjectIdHex(vars["id"])
userID := bson.ObjectIdHex(vars["userID"])
RemoveAttendee(eventID, userID, "participants")
RemoveAttendee(eventID, userID, "notgoing")
event, user := RemoveAttendee(eventID, userID, "maybe")
_ = json.NewEncoder(w).Encode(bson.M{"event": event, "user": user})
}
// CommentEventController will answer a JSON of the event
func CommentEventController(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(bson.M{"error": "Unable to read the request body"})
}
var comment Comment
if err := json.Unmarshal([]byte(string(body)), &comment); err != nil {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(bson.M{"error": "wrong format"})
return
}
comment.ID = bson.NewObjectId()
comment.Date = time.Now()
vars := mux.Vars(r)
eventID := vars["id"]
event := CommentEvent(bson.ObjectIdHex(eventID), comment)
association := GetAssociation(event.Association)
user := GetUser(comment.User)
_ = json.NewEncoder(w).Encode(event)
if !event.NoNotification {
_ = SendAssociationEmailForCommentOnEvent(association.Email, event, comment, user)
}
for _, tag := range comment.Tags {
go TriggerNotificationForUserFromEvent(comment.User, bson.ObjectIdHex(tag.User), event.ID, "@"+GetUser(comment.User).Username+" t'a taggé sur '"+event.Name+"'", comment, "eventTag")
}
}
// UncommentEventController will answer a JSON of the event
func UncommentEventController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventID := vars["id"]
commentID := vars["commentID"]
res := UncommentEvent(bson.ObjectIdHex(eventID), bson.ObjectIdHex(commentID))
_ = json.NewEncoder(w).Encode(res)
}