Commit 888a14ac authored by Max Richter's avatar Max Richter
Browse files

refactor(rest-api): 🗑 remove controllers as duplicated code

parent 1601a469
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")
}
}
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