Commit 85216b04 authored by Enrico Bollen's avatar Enrico Bollen 💬
Browse files

Merge branch 'feat/check-building-opening-times' into 'dev'

Feat/check building opening times

See merge request !29
parents c3aa0ad3 fa02e692
Pipeline #1821 failed with stages
in 59 seconds
......@@ -3,6 +3,7 @@ package model
import (
"errors"
"sort"
"strings"
"time"
"git.coco.study/fvitt/good2go/internal/utils"
......@@ -33,7 +34,7 @@ func (b *Building) New(openFrom string, openDuration string, workdays []string)
return b
}
// CreateRoom adds rooms to building.
// AddRoom adds rooms to building.
// takes number and capacity
func (b *Building) CreateRoom(roomNumber int, capacity int) (room *Room, err error) {
......@@ -87,6 +88,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
}
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