This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
/
room_controller.go
137 lines (116 loc) · 4.42 KB
/
room_controller.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
package controller
import (
"encoding/json"
"errors"
"net/http"
"github.com/badmuts/hsleiden-ipsenh-api/model"
"github.com/gorilla/mux"
"github.com/unrolled/render"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type RoomController struct {
router *mux.Router
r *render.Render
db *mgo.Database
}
func NewRoomController(router *mux.Router, r *render.Render, db *mgo.Database) *RoomController {
ctrl := &RoomController{
router: router,
r: r,
db: db,
}
ctrl.Register()
return ctrl
}
func (ctrl *RoomController) Register() {
ctrl.router.HandleFunc("/rooms", ctrl.Find).Name("rooms.find").Methods("GET")
ctrl.router.HandleFunc("/rooms", ctrl.Create).Name("rooms.create").Methods("POST")
ctrl.router.HandleFunc("/rooms/{roomID}", ctrl.FindRoomID).Name("rooms.findId").Methods("GET")
ctrl.router.HandleFunc("/rooms/{roomID}", ctrl.Update).Name("rooms.update").Methods("PUT", "PATCH")
ctrl.router.HandleFunc("/rooms/{roomID}/roster", ctrl.CreateRoster).Name("rooms.createRoster").Methods("POST")
}
func (ctrl *RoomController) FindRoomID(res http.ResponseWriter, req *http.Request) {
roomID := bson.ObjectIdHex(mux.Vars(req)["roomID"])
Room := model.NewRoomModel(ctrl.db)
room, err := Room.FindId(roomID)
if err == mgo.ErrNotFound {
ctrl.r.JSON(res, http.StatusNotFound, NewControllerError("404", "Can't find room due to db error (enable mgo logging)", "Could not find room", "more info"))
return
} else if err != nil {
ctrl.r.JSON(res, http.StatusInternalServerError, NewControllerError("500", "Can't find room due to db error (enable mgo logging)", "Can't find room", "more info"))
return
}
ctrl.r.JSON(res, http.StatusOK, room)
}
func (ctrl *RoomController) Update(res http.ResponseWriter, req *http.Request) {
roomID := bson.ObjectIdHex(mux.Vars(req)["roomID"])
Room := model.NewRoomModel(ctrl.db)
Room, err := Room.FindId(roomID)
if err == mgo.ErrNotFound {
ctrl.r.JSON(res, http.StatusNotFound, Room)
return
} else if err != nil {
ctrl.r.JSON(res, http.StatusInternalServerError, NewControllerError("500", "Can't update due to db error (enable mgo logging)", "Can't update room", "more info"))
return
}
dec := json.NewDecoder(req.Body)
err = dec.Decode(&Room)
if err != nil {
ctrl.r.JSON(res, http.StatusBadRequest, NewControllerError("400", "Can't decode due to invalid json", "Invalid json", "more info"))
return
}
Room, err = Room.Save()
if err != nil {
ctrl.r.JSON(res, http.StatusInternalServerError, NewControllerError("500", "Can't UpSert due to db error (enable mgo logging)", "Could not save room", "more info"))
return
}
ctrl.r.JSON(res, http.StatusOK, Room)
}
func (ctrl *RoomController) CreateRoster(res http.ResponseWriter, req *http.Request) {
ID := bson.ObjectIdHex(mux.Vars(req)["roomID"])
RoomRosters := []model.RoomRoster{}
dec := json.NewDecoder(req.Body)
err := dec.Decode(&RoomRosters)
if err != nil {
ctrl.r.JSON(res, http.StatusBadRequest, NewControllerError("400", "Can't decode due to invalid json", "Invalid json", "more info"))
return
}
for index := range RoomRosters {
if RoomRosters[index].ID == "" {
RoomRosters[index].ID = bson.NewObjectId()
}
RoomRosters[index].RoomID = ID
err = ctrl.db.C("room_reservation").Insert(RoomRosters[index])
if err != nil {
ctrl.r.JSON(res, http.StatusInternalServerError, NewControllerError("500", "Can't insert due to db error (enable mgo logging)", "Could not save reservation", "more info"))
return
}
}
ctrl.r.JSON(res, http.StatusCreated, RoomRosters)
}
func (ctrl *RoomController) Find(res http.ResponseWriter, req *http.Request) {
building_id := req.URL.Query().Get("buildingId")
room := model.NewRoomModel(ctrl.db)
rooms, err := room.Find(building_id)
if err != nil {
ctrl.r.JSON(res, http.StatusInternalServerError, NewControllerError("500", "Can't find due to db error (enable mgo logging)", "Can't find room", "more info"))
return
}
ctrl.r.JSON(res, http.StatusOK, rooms)
}
func (ctrl *RoomController) Create(res http.ResponseWriter, req *http.Request) {
Room := model.NewRoomModel(ctrl.db)
dec := json.NewDecoder(req.Body)
err := dec.Decode(&Room)
if err != nil {
ctrl.r.JSON(res, http.StatusBadRequest, NewControllerError("400", "Can't decode due to invalid json", "Invalid json", "more info"))
return
}
Room, err = Room.Save()
if err != nil {
ctrl.r.JSON(res, http.StatusInternalServerError, errors.New("Could not save room"))
return
}
ctrl.r.JSON(res, http.StatusCreated, Room)
}