/
societyUsers.go
133 lines (126 loc) · 3.9 KB
/
societyUsers.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
package main
import (
"encoding/json"
"fmt"
"mime"
"net/http"
"github.com/ourrootsorg/cms-server/utils"
"github.com/ourrootsorg/cms-server/api"
)
// GetUsers returns all users for a society
// @summary returns all users
// @router /societies/{society}/users [get]
// @tags societyUsers
// @id getUsers
// @produce application/json
// @success 200 {array} api.SocietyUserEmail "OK"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) GetSocietyUserNames(w http.ResponseWriter, req *http.Request) {
enc := json.NewEncoder(w)
w.Header().Set("Content-Type", contentType)
societies, errors := app.api.GetSocietyUserNames(req.Context())
if errors != nil {
ErrorsResponse(w, errors)
return
}
err := enc.Encode(societies)
if err != nil {
serverError(w, err)
return
}
}
// GetCurrentSocietyUser returns the current SocietyUser
// @summary returns the current SocietyUser
// @router /societies/{society}/current_user [get]
// @tags societyUsers
// @id getCurrentSocietyUser
// @produce application/json
// @success 200 {array} model.SocietyUser "OK"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) GetCurrentSocietyUser(w http.ResponseWriter, req *http.Request) {
enc := json.NewEncoder(w)
w.Header().Set("Content-Type", contentType)
user, err := utils.GetUserFromContext(req.Context())
if err != nil {
ErrorsResponse(w, err)
return
}
societyUser, errors := app.api.GetSocietyUserByUser(req.Context(), user.ID)
if errors != nil {
ErrorsResponse(w, errors)
return
}
err = enc.Encode(societyUser)
if err != nil {
serverError(w, err)
return
}
}
// PutSocietyUserName updates a SocietyUser in the database
// @summary updates a SocietyUser
// @router /societies/{society}/users/{id} [put]
// @tags societyUsers
// @id updateSocietyUser
// @Param id path integer true "SocietyUser ID"
// @Param society body api.SocietyUserEmail true "Update SocietyUser"
// @accept application/json
// @produce application/json
// @success 200 {object} api.SocietyUserEmail "OK"
// @failure 415 {object} api.Error "Bad Content-Type"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) PutSocietyUserName(w http.ResponseWriter, req *http.Request) {
mt, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
if err != nil || mt != contentType {
msg := fmt.Sprintf("Bad Content-Type '%s'", mt)
ErrorResponse(w, http.StatusUnsupportedMediaType, msg)
return
}
var in api.SocietyUserEmail
err = json.NewDecoder(req.Body).Decode(&in)
if err != nil {
msg := fmt.Sprintf("Bad request: %v", err.Error())
ErrorResponse(w, http.StatusBadRequest, msg)
return
}
society, errors := app.api.UpdateSocietyUserEmail(req.Context(), in.ID, in)
if errors != nil {
ErrorsResponse(w, errors)
return
}
w.Header().Set("Content-Type", contentType)
enc := json.NewEncoder(w)
err = enc.Encode(society)
if err != nil {
serverError(w, err)
return
}
}
// DeleteSociety deletes a SocietyUser from the database
// @summary deletes a SocietyUser
// @router /societies/{society}/users/{id} [delete]
// @tags societyUsers
// @id deleteSocietyUSer
// @Param id path integer true "SocietyUser ID"
// @success 204 "OK"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) DeleteSocietyUser(w http.ResponseWriter, req *http.Request) {
id, errors := getIDFromRequest(req)
if errors != nil {
ErrorsResponse(w, errors)
return
}
errors = app.api.DeleteSocietyUser(req.Context(), id)
if errors != nil {
ErrorsResponse(w, errors)
return
}
w.WriteHeader(http.StatusNoContent)
}