Commit acdb00aa authored by Max Richter's avatar Max Richter
Browse files

Merge branch 'feat/rest-api-tests' of git.coco.study:fvitt/good2go into dev

parents 50c01caa 66e75374
package controllers
/*
// CreateAppointment creates an appointment.
func CreateAppointment() {
}
// GetAppointment finds a single appointment by startTime
func GetAppointment(startTime time.Time) {
}
// GetAllAppointments returns all the appointments
func GetAllAppointments() {
}
// UpdateAppointment updates a single appointment
func UpdateAppointment() {
}
// DeleteAppointment deletes a single appointment
func DeleteAppointment() {
}
*/
package controllers
/*
// CreateBuilding Creates a single new building
func CreateBuilding() {
// Reserved for future use
}
// GetBuilding gets a single building
func GetBuilding() {
// Reserved for future use
}
// GetAllBuildings Returns all the buildings
func GetAllBuildings() {
// Reserved for future use
}
// UpdateBuilding Updates a single building
func UpdateBuilding() {
// Reserved for future use
}
// DeleteBuilding Deletes a single building
func DeleteBuilding(id int) {
// Reserved for future use
}
*/
package controllers
import (
m "git.coco.study/fvitt/good2go/internal/model"
s "git.coco.study/fvitt/good2go/internal/services"
"log"
)
// RoomController Controller to handle all types of controller stuff
type RoomController struct{}
// CreateRoom *drumroll* creates a room
func CreateRoom(buildingId int, roomNo int, capacity int) *m.Room {
room, err := s.BuildingService.AddRoom(buildingId, roomNo, capacity)
if err != nil {
log.Fatal(err)
}
return room
}
// GetRoom gets a single room by id
func GetRoom(roomId int) (room *m.Room, err error) {
room, err = s.BuildingService.GetRoom(roomId)
return room, err
}
// GetAllRooms returns all the rooms
func GetAllRooms() map[int][]*m.Room {
return s.BuildingService.GetRooms()
}
// UpdateRoom updates a single room
func UpdateRoom(buildingId int, roomNo int, capacity int) (room *m.Room, err error) {
room, err = s.BuildingService.UpdateRoomCapacity(buildingId, roomNo, capacity)
return room, err
}
// DeleteRoom deletes a room by id
func DeleteRoom(buildingId, roomNo int) (err error) {
err = s.BuildingService.DeleteRoom(buildingId, roomNo)
if err != nil {
log.Fatal(err)
}
return err
}
package controllers
import (
"reflect"
"testing"
"git.coco.study/fvitt/good2go/internal/model"
"git.coco.study/fvitt/good2go/internal/services"
)
var initialRoomAmount = 0
func isRoom(r *model.Room) bool {
if reflect.TypeOf(r.Capacity).Name() != "int" {
return false
}
if reflect.TypeOf(r.Number).Name() != "int" {
return false
}
return true
}
func TestCreateRooms(t *testing.T) {
services.BuildingService.AddBuilding("10:00", "10h", []string{"mon"})
room := CreateRoom(0, 420, 69)
if room.Number != 420 {
t.Errorf("Returned room didnt have the correct room number.")
}
if room.Capacity != 69 {
t.Errorf("Returned room didnt have the correct capacity.")
}
}
func TestGetRoom(t *testing.T) {
CreateRoom(0, 666, 66)
room, err := GetRoom(0, 666)
if err != nil {
t.Error(err)
}
if room.Capacity != 66 {
t.Errorf("first element in getAllRooms[] doesnt seem to be a room")
}
if room.Number != 666 {
t.Errorf("Created room didnt have the correct room number")
}
}
func TestGetAllRooms(t *testing.T) {
rooms := GetAllRooms()[0]
initialRoomAmount = len(rooms)
if initialRoomAmount > 0 && !isRoom(rooms[0]) {
t.Errorf("first element in getAllRooms[] doesnt seem to be a room")
}
}
func TestUpdateSingleRoom(t *testing.T) {
// GIVEN
CreateRoom(0, 34, 34)
// WHEN
_, updateErr := UpdateRoom(0, 34, 90)
if updateErr != nil {
t.Error(updateErr)
}
// THEN
room, err := GetRoom(0, 34)
if err != nil {
t.Errorf("Could not find created room")
}
if room != nil {
if room.Capacity != 90 {
t.Error("Updated room didnt have the correct capacity")
}
}
}
func TestDeleteRoom(t *testing.T) {
// GIVEN
CreateRoom(0, 100, 100)
_, err := GetRoom(0, 100)
if err != nil {
t.Error(err.Error())
}
// WHEN
err = DeleteRoom(0, 100)
// THEN
_, err = GetRoom(0, 100)
if err == nil {
t.Error("Deleted room was not deleted")
}
}
......@@ -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