Commit 66e75374 authored by Max Richter's avatar Max Richter
Browse files

refactor(rest-api): make use of the new room ids

parent 888a14ac
Pipeline #1807 failed with stages
in 56 seconds
......@@ -3,13 +3,12 @@ package restapi
import (
"encoding/json"
"fmt"
c "git.coco.study/fvitt/good2go/api/rest/controllers"
"log"
"net/http"
u "git.coco.study/fvitt/good2go/api/rest/utils"
s "git.coco.study/fvitt/good2go/internal/services"
"github.com/gorilla/mux"
"log"
"net/http"
"strconv"
)
type roomRouter struct{}
......@@ -19,21 +18,20 @@ var (
RoomRouter = &roomRouter{}
)
func createRoom(res http.ResponseWriter, req *http.Request) {
type roomDTO struct {
Building int `json:"building"`
Number int `json:"number"`
Capacity int `json:"capacity"`
}
// Decode the body
type incoming struct {
Building int `json:"building"`
Number int `json:"number"`
Capacity int `json:"capacity"`
}
var r incoming
err := json.NewDecoder(req.Body).Decode(&incoming{})
func createRoom(res http.ResponseWriter, req *http.Request) {
var r roomDTO
err := json.NewDecoder(req.Body).Decode(&roomDTO{})
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
room := c.CreateRoom(r.Building, r.Number, r.Capacity)
room, err := s.BuildingService.AddRoom(r.Building, r.Number, r.Capacity)
err = u.SendJSON(res).Encode(room)
if err != nil {
......@@ -42,21 +40,15 @@ func createRoom(res http.ResponseWriter, req *http.Request) {
}
func getRoom(res http.ResponseWriter, req *http.Request) {
var roomId int
roomId, parseErr := strconv.Atoi(mux.Vars(req)["roomId"])
roomID := mux.Vars(req)["roomID"]
if parseErr == nil {
room, roomErr := s.BuildingService.GetRoom(roomID)
room, roomErr := c.GetRoom(roomId)
if roomErr == nil {
u.SendJSON(res).Encode(room)
} else {
http.Error(res, roomErr.Error(), http.StatusNotFound)
}
if roomErr == nil {
u.SendJSON(res).Encode(room)
} else {
http.Error(res, parseErr.Error(), http.StatusBadRequest)
http.Error(res, roomErr.Error(), http.StatusNotFound)
}
}
......@@ -71,20 +63,18 @@ func getAllRooms(res http.ResponseWriter, req *http.Request) {
}
func updateRoom(res http.ResponseWriter, req *http.Request) {
// Decode the body
type incoming struct {
Building int
Number int
Capacity int
}
var r incoming
roomID := mux.Vars(req)["roomID"]
var r roomDTO
parseBodyErr := json.NewDecoder(req.Body).Decode(&r)
if parseBodyErr != nil {
http.Error(res, parseBodyErr.Error(), http.StatusBadRequest)
return
}
room, err := s.BuildingService.UpdateRoomCapacity(r.Building, r.Number, r.Capacity)
room, err := s.BuildingService.UpdateRoomCapacity(roomID, r.Number, r.Capacity)
if err == nil {
err := u.SendJSON(res).Encode(room)
if err != nil {
......@@ -96,19 +86,10 @@ func updateRoom(res http.ResponseWriter, req *http.Request) {
}
func deleteRoom(res http.ResponseWriter, req *http.Request) {
type incoming struct {
Building int
Number int
Capacity int
}
var r incoming
parseBodyErr := json.NewDecoder(req.Body).Decode(&r)
if parseBodyErr != nil {
http.Error(res, parseBodyErr.Error(), http.StatusBadRequest)
return
}
err := s.BuildingService.DeleteRoom(r.Building, r.Number)
roomID := mux.Vars(req)["roomID"]
err := s.BuildingService.DeleteRoom(roomID)
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
log.Fatal(err)
......@@ -125,13 +106,13 @@ func (a roomRouter) AttachRouter(router *mux.Router) {
r.HandleFunc("/", createRoom).Methods("POST")
// READ
r.HandleFunc("/{roomNo}", getRoom).Methods("GET")
r.HandleFunc("/{roomID}", getRoom).Methods("GET")
r.HandleFunc("/", getAllRooms).Methods("GET")
// UPDATE
r.HandleFunc("/{roomNo}", updateRoom).Methods("PUT")
r.HandleFunc("/{roomID}", updateRoom).Methods("PUT")
// DELETE
r.HandleFunc("/{roomNo}", deleteRoom).Methods("DELETE")
r.HandleFunc("/{roomID}", deleteRoom).Methods("DELETE")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment