Esempio n. 1
0
// PostNewRoom creates a new room using the name submitted
//
// Request {"name":"Bedroom","type":"bedroom"}
// Response {"name":"Bedroom","type":"bedroom","id":"16c63268-c0e5-48a2-b312-c74c64837802"}
//
func (lr *RoomRouter) PostNewRoom(r *http.Request, w http.ResponseWriter, roomModel *models.RoomModel, conn redis.Conn) {

	// get the request body
	body, err := GetJsonPayload(r)

	if err != nil {
		WriteServerErrorResponse("Unable to parse body", http.StatusInternalServerError, w)
		return
	}

	roomName := body["name"].(string)
	roomType := body["type"].(string)

	room := &model.Room{Name: roomName, Type: roomType}

	err = roomModel.Create(room, conn)

	if err != nil {
		WriteServerErrorResponse("Unable to create room", http.StatusInternalServerError, w)
		return
	}

	WriteServerResponse(room, http.StatusOK, w)

}
Esempio n. 2
0
// GetRoom updates a room using it's identifier
func (lr *RoomRouter) UpdateRoom(params martini.Params, r *http.Request, w http.ResponseWriter, roomModel *models.RoomModel, conn redis.Conn) {

	room, err := roomModel.Fetch(params["id"], conn)

	log.Infof(spew.Sprintf("room: %v", room))

	if err == models.RecordNotFound {
		WriteServerErrorResponse(fmt.Sprintf("Unknown room id: %s", params["id"]), http.StatusNotFound, w)
		return
	}

	if err != nil {
		WriteServerErrorResponse("Unable to retrieve room", http.StatusInternalServerError, w)
		return
	}

	// get the request body
	body, err := GetJsonPayload(r)

	if err != nil {
		WriteServerErrorResponse("Unable to read body to update room", http.StatusInternalServerError, w)
		return
	}

	room.Name = body["name"].(string)
	room.Type = body["type"].(string)

	err = roomModel.Create(room, conn)

	if err != nil {
		WriteServerErrorResponse("Unable to update room", http.StatusInternalServerError, w)
		return
	}

	WriteServerResponse(room, http.StatusOK, w)
}