Commit fffc18e6 authored by Enrico Bollen's avatar Enrico Bollen 💬
Browse files

check building opening times when looking for free room

parent d4c5b841
Pipeline #1814 failed with stages
in 56 seconds
......@@ -78,7 +78,7 @@ func TestUpdateSingleRoom(t *testing.T) {
}
// THEN
room, err := GetRoom(0, 34)
room, err := GetRoom(0)
if err != nil {
t.Errorf("Could not find created room")
}
......
......@@ -3,6 +3,7 @@ package model
import (
"errors"
"sort"
"strings"
"time"
"git.coco.study/fvitt/good2go/internal/utils"
......@@ -35,7 +36,6 @@ func (b *Building) New(openFrom string, openDuration string, workdays []string)
// AddRoom adds rooms to building.
// takes number and capacity
func (b *Building) AddRoom(roomNumber int, capacity int) (room *Room, err error) {
room, err = b.GetRoomByNo(roomNumber)
if err != nil {
room = &Room{Number: roomNumber, Capacity: capacity}
......@@ -86,6 +86,34 @@ func (b *Building) GetRoomByNo(roomNumber int) (room *Room, err error) {
// FindAvailableRooms takes an appointment and returns all rooms which are free at this time.
// List is sorted after capacity, so lowest capacity is index 0.
func (b *Building) FindAvailableRooms(reqAppoint Appointment) (selectedRooms []*Room, err error) {
// check opening times
reqStartTime := utils.FormatDateToHour(reqAppoint.StartDate)
reqEndTime := utils.FormatDateToHour(reqAppoint.EndDate)
buildStartTime := utils.FormatDateToHour(b.OpeningHour)
buildEndTime := utils.FormatDateToHour(b.ClosingHour)
workdaysString := []string{}
if reqStartTime.Before(buildStartTime) {
err = errors.New("start time is before building is open, startTime: " + reqStartTime.String() + " buildStart: " + buildStartTime.String())
return
}
if reqStartTime.After(buildEndTime) {
err = errors.New("start time is after building is closed, startTime: " + reqStartTime.String() + " buildingEnd: " + buildEndTime.String())
return
}
if reqEndTime.After(buildEndTime) {
err = errors.New("end time is after building is closed, endTime: " + reqEndTime.String() + " buildingEnd:" + buildEndTime.String())
return
}
for _, day := range b.Workdays {
if day == reqAppoint.StartDate.Weekday() {
err = nil
break
}
workdaysString = append(workdaysString, day.String())
err = errors.New("startDate is not when building is open, startDate: " + reqAppoint.StartDate.Weekday().String() + " building open: " + strings.Join(workdaysString, ","))
}
// look for free rooms
for _, room := range b.Rooms {
added := false
......
......@@ -161,6 +161,6 @@ func TestBuilding_FindFreeRooms(t *testing.T) {
func newBuilding() Building {
building := Building{}
building.New("08:00", "10h", []string{"mon"})
building.New("08:00", "10h", []string{"mon", "tue", "wed", "thu", "fri", "sat"})
return building
}
......@@ -99,7 +99,7 @@ func TestAppointmentService_GetAppointment(t *testing.T) {
// Resets building
func ResetBuildingManager() {
BuildingService.New()
BuildingService.AddBuilding("08:00", "10h", []string{"mon"})
BuildingService.AddBuilding("08:00", "10h", []string{"mon", "tue", "wed", "thu", "fri", "sat"})
}
// Resets building(0) and adds room 200 with capacity 40
......
package services
import (
"git.coco.study/fvitt/good2go/internal/model"
"testing"
)
......@@ -102,3 +103,23 @@ func TestBuildingService_UpdateRoomCapacity(t *testing.T) {
t.Error("expected error, got: ", err)
}
}
func TestBuildingService_FindAvailableRooms(t *testing.T) {
BuildDefBuilding()
reqAppoint, err := model.Appointment{}.New("03-06-2020 10:00", "4h")
if err != nil {
t.Fatal(err)
}
rooms, err := BuildingService.FindAvailableRooms(reqAppoint)
if len(rooms) != 1 {
t.Error("no available room found, expected 1")
}
err = nil
// no room available
// add appointment to block room
_, err = AppointmentService.AddAppointment(reqAppoint)
if err != nil {
t.Fatal(err)
}
}
......@@ -19,6 +19,13 @@ func FormatHour(input string) time.Time {
return formattedTime
}
func FormatDateToHour(date time.Time) (formattedTime time.Time) {
const timeLayout string = "15:04"
dateString := date.String()
formattedTime, _ = time.Parse(timeLayout, dateString)
return formattedTime
}
func FormatDay(input string) time.Weekday {
input = strings.ToLower(input)
......
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