-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_handler.go
65 lines (58 loc) · 1.84 KB
/
api_handler.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
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
type apiHandler struct {
handlerCom *handlerCommon
trelloManager *trelloDataManager
dCache dataStore
}
type trialInfo struct {
SelectedBoardID string `json:"boardId"`
SelectedListID string `json:"listId"`
}
func newAPIHandler(cache dataStore) *apiHandler {
ah := new(apiHandler)
ah.handlerCom = newHandlerCommon()
ah.trelloManager = newTrelloDataManager()
ah.dCache = cache
return ah
}
func (ah *apiHandler) getBordLists(w http.ResponseWriter, r *http.Request) {
userID := ah.handlerCom.GetUserIDFromSession(r)
info, _ := ah.dCache.getUserInfo(userID)
params := mux.Vars(r)
boardID := params["boardId"]
if boardID == "" {
ah.handlerCom.ProcessErrorMessage(messageInvalidBoardID, w)
return
}
boardLists, _ := ah.trelloManager.getBoardLists(info, boardID)
ah.handlerCom.ProcessResponse(boardLists, w)
}
func (ah *apiHandler) saveBoardAndList(w http.ResponseWriter, r *http.Request) {
userID := ah.handlerCom.GetUserIDFromSession(r)
info, _ := ah.dCache.getUserInfo(userID)
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&info.TrelloDetails)
if err != nil {
ah.handlerCom.ProcessErrorMessage(messageInvalidBoardID, w)
return
}
if info.TrelloDetails.SelectedBoardID == "" || info.TrelloDetails.SelectedListID == "" {
ah.handlerCom.ProcessErrorMessage(messageInvalidBoardList, w)
return
}
// Mark the selected board in the list of boards as selected and mark others as unselected
for index, board := range info.TrelloDetails.TrelloBoards {
if board.ID == info.TrelloDetails.SelectedBoardID {
info.TrelloDetails.TrelloBoards[index].IsSelected = true
} else {
info.TrelloDetails.TrelloBoards[index].IsSelected = false
}
}
ah.dCache.saveDetailsToCache(userID, *info)
ah.handlerCom.ProcessSuccessMessage(messageSavedBoardList, w)
}