-
Notifications
You must be signed in to change notification settings - Fork 0
/
boardController.go
266 lines (220 loc) · 7.37 KB
/
boardController.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
func (env *Env) AddUserToBoard(c *gin.Context) {
sessionId, err := getSessionIdFromCookie(c)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
user, err := env.getUserFromSession(sessionId)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
// Check if requesting user is member of the board
boardMember := BoardMember{}
// TODO: handle this err
boardId, err := strconv.Atoi(c.Params.ByName("boardId"))
// TODO: Check if there's a result from this if the query fails
env.db.First(&boardMember, "board_id = ? AND user_id = ?", boardId, user.ID)
if boardMember.BoardID == 0 {
err = templates.ExecuteTemplate(c.Writer, "notAuthorized.html", nil)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
return
}
// Get user id from form post
// TODO: handle this err
usernameToAdd := c.PostForm("userToAdd")
userToAdd := User{}
// TODO: If a user can't be found, user ID 0 will be added to the boardMember FIX
env.db.First(&userToAdd, "username = ?", usernameToAdd)
boardMember = BoardMember{}
env.db.First(&boardMember, "board_id = ? AND user_id = ?", boardId, userToAdd.ID)
// Check if user is already a member
if boardMember.BoardID == 0 {
// Create board member
BoardMember := BoardMember{BoardID: uint(boardId), UserID: userToAdd.ID}
// TODO: Check for errors
env.db.Create(&BoardMember)
log.Printf("Added user %d to board %d", userToAdd.ID, boardId)
c.Redirect(http.StatusFound, fmt.Sprintf("/board/%d", boardId))
return
} else {
log.Printf("User %d is already a member of board %d", userToAdd.ID, boardId)
}
}
func (env *Env) RemoveUserFromBoard(c *gin.Context) {
sessionId, err := getSessionIdFromCookie(c)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
user, err := env.getUserFromSession(sessionId)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
// Check if requesting user is member of the board
boardMember := BoardMember{}
// TODO: handle this err
boardId, err := strconv.Atoi(c.Params.ByName("boardId"))
// TODO: Check if there's a result from this if the query fails
env.db.First(&boardMember, "board_id = ? AND user_id = ?", boardId, user.ID)
if boardMember.BoardID == 0 {
err = templates.ExecuteTemplate(c.Writer, "notAuthorized.html", nil)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
return
}
// Get user id from form post
// TODO: handle this err
usernameToRemove := c.PostForm("userToRemove")
userToRemove := User{}
env.db.First(&userToRemove, "username = ?", usernameToRemove)
boardMember = BoardMember{}
env.db.First(&boardMember, "board_id = ? AND user_id = ?", boardId, userToRemove.ID)
log.Printf("Board member is %d, and %d", boardMember.BoardID, boardMember.UserID)
if boardMember.BoardID != 0 {
// TODO: Check for errors
env.db.Delete(&boardMember)
log.Printf("Removed user %d from board %d", userToRemove.ID, boardId)
c.Redirect(http.StatusFound, fmt.Sprintf("/board/%d/members", boardId))
return
} else {
log.Printf("User %d was not a member of of board %d", userToRemove.ID, boardId)
c.Redirect(http.StatusFound, fmt.Sprintf("/board/%d/members", boardId))
return
}
}
func (env *Env) PostBoard(c *gin.Context) {
sessionId, err := getSessionIdFromCookie(c)
if err != nil {
panic(err)
}
user, err := env.getUserFromSession(sessionId)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
boardName := c.PostForm("boardName")
board := Board{BoardName: boardName}
// TODO: Check for errors
env.db.Create(&board)
log.Printf("New board created %d with name %s", board.ID, board.BoardName)
BoardMember := BoardMember{BoardID: board.ID, UserID: user.ID}
// TODO: Check for errors
env.db.Create(&BoardMember)
c.Redirect(http.StatusFound, fmt.Sprintf("?boardId=%d", board.ID))
}
func (env *Env) NewBoard(c *gin.Context) {
sessionId, err := getSessionIdFromCookie(c)
if err != nil {
panic(err)
}
_, err = env.getUserFromSession(sessionId)
if err != nil {
// TODO: Doesn't redirect due to turbo
c.Redirect(http.StatusFound, "/signin")
return
}
err = templates.ExecuteTemplate(c.Writer, "newBoard.html", nil)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
}
func (env *Env) GetBoard(c *gin.Context) {
sessionId, err := getSessionIdFromCookie(c)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
user, err := env.getUserFromSession(sessionId)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
boardId := c.Params.ByName("boardId")
boardMember := BoardMember{}
// TODO: Check if there's a result from this if the query fails
env.db.First(&boardMember, "board_id = ? AND user_id = ?", boardId, user.ID)
if boardMember.BoardID == 0 {
err = templates.ExecuteTemplate(c.Writer, "notAuthorized.html", nil)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
return
}
log.Printf("BoardId: %s", boardId)
board := Board{}
db.First(&board, boardId)
log.Printf("Boardname %s id %d\n", board.BoardName, board.ID)
log.Printf("=========================")
err = templates.ExecuteTemplate(c.Writer, "boardDetails.html", board)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
}
func (env *Env) GetBoardMembers(c *gin.Context) {
sessionId, err := getSessionIdFromCookie(c)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
user, err := env.getUserFromSession(sessionId)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
// TODO: Handle this error and make convienence function for getting board id from params
boardId, _ := strconv.Atoi(c.Params.ByName("boardId"))
board := Board{}
board.ID = uint(boardId)
userIsMemberOfBoard := env.isUserMemberOfBoard(user, board)
if !userIsMemberOfBoard {
err = templates.ExecuteTemplate(c.Writer, "notAuthorized.html", nil)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
return
}
users := []User{}
env.db.Table("users").Select("username, id").Joins("JOIN board_members on users.id = board_members.user_id").Where("board_members.board_id = ?", board.ID).Find(&users)
templateVars := map[string]interface{}{"board_id": board.ID, "users": users}
err = templates.ExecuteTemplate(c.Writer, "boardMembers.html", templateVars)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
}
func (env *Env) isUserMemberOfBoard(user User, board Board) bool {
boardMember := BoardMember{}
if result := env.db.First(&boardMember, "board_id = ? AND user_id = ?", board.ID, user.ID); result.Error != nil {
return false
}
return true
}
func (env *Env) GetBoardsForUser(c *gin.Context) {
sessionId, err := getSessionIdFromCookie(c)
if err != nil {
panic(err)
}
user, err := env.getUserFromSession(sessionId)
if err != nil {
c.Redirect(http.StatusFound, "/signin")
return
}
var results []map[string]interface{}
env.db.Table("board_members").Select("boards.ID", "boards.board_name").Joins("JOIN boards on boards.id = board_members.board_id").Where("board_members.user_id = ?", user.ID).Find(&results)
err = templates.ExecuteTemplate(c.Writer, "boards.html", results)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
}