/
associationController.go
102 lines (84 loc) · 2.93 KB
/
associationController.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
package insapp
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2/bson"
)
// GetMyAssociationController will answer a JSON of the associations owned by the applicant master association
func GetMyAssociationController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
associationID := vars["id"]
var res = GetMyAssociations(bson.ObjectIdHex(associationID))
_ = json.NewEncoder(w).Encode(res)
}
// GetAssociationController will answer a JSON of the association
// linked to the given id in the URL
func GetAssociationController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
associationID := vars["id"]
var res = GetAssociation(bson.ObjectIdHex(associationID))
_ = json.NewEncoder(w).Encode(res)
}
// GetAllAssociationsController will answer a JSON of all associations
func GetAllAssociationsController(w http.ResponseWriter, r *http.Request) {
var res = GetAllAssociations()
_ = json.NewEncoder(w).Encode(res)
}
// AddAssociationController will answer a JSON of the
// brand new created association (from the JSON Body)
// Should be protected
func AddAssociationController(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var association Association
decoder.Decode(&association)
isValidMail := VerifyEmail(association.Email)
if !isValidMail {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(bson.M{"error": "email already used"})
return
}
res := AddAssociation(association)
password := GeneratePassword()
userID, err := GetUserFromRequest(r)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
json.NewEncoder(w).Encode(bson.M{"error": "could not get user ID"})
return
}
var user AssociationUser
user.Association = res.ID
user.Username = res.Email
user.Master = false
user.Owner = userID
user.Password = GetMD5Hash(password)
AddAssociationUser(user)
_ = SendAssociationEmailSubscription(user.Username, password)
_ = json.NewEncoder(w).Encode(res)
}
// UpdateAssociationController will answer the JSON of the
// modified association (from the JSON Body)
// Should be protected
func UpdateAssociationController(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var association Association
_ = decoder.Decode(&association)
vars := mux.Vars(r)
associationID := vars["id"]
res := UpdateAssociation(bson.ObjectIdHex(associationID), association)
_ = json.NewEncoder(w).Encode(res)
}
// DeleteAssociationController will answer a JSON of an
// empty association if the deletion has succeed
// Should be protected
func DeleteAssociationController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
associationID := vars["id"]
res := DeleteAssociation(bson.ObjectIdHex(associationID))
_ = json.NewEncoder(w).Encode(res)
}
// VerifyEmail return true if email is not already used
func VerifyEmail(email string) bool {
association := GetAssociationFromEmail(email)
return association.Email == ""
}