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

rename services

parent 4e072092
Pipeline #1797 passed with stages
in 2 minutes and 37 seconds
......@@ -11,7 +11,7 @@ type RoomController struct{}
// CreateRoom *drumroll* creates a room
func CreateRoom(buildingId int, roomNo int, capacity int) *m.Room {
room, err := s.BuildingManager.AddRoom(buildingId, roomNo, capacity)
room, err := s.BuildingService.AddRoom(buildingId, roomNo, capacity)
if err != nil {
log.Fatal(err)
}
......@@ -20,24 +20,24 @@ func CreateRoom(buildingId int, roomNo int, capacity int) *m.Room {
// GetRoom gets a single room by id
func GetRoom(buildingId int, roomNo int) (room *m.Room, err error) {
room, err = s.BuildingManager.GetRoom(buildingId, roomNo)
room, err = s.BuildingService.GetRoom(buildingId, roomNo)
return room, err
}
// GetAllRooms returns all the rooms
func GetAllRooms() map[int][]*m.Room {
return s.BuildingManager.GetRooms()
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.BuildingManager.UpdateRoomCapacity(buildingId, roomNo, capacity)
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.BuildingManager.DeleteRoom(buildingId, roomNo)
err = s.BuildingService.DeleteRoom(buildingId, roomNo)
if err != nil {
log.Fatal(err)
}
......
......@@ -24,7 +24,7 @@ func isRoom(r *model.Room) bool {
}
func TestCreateRooms(t *testing.T) {
services.BuildingManager.AddBuilding("10:00", "10h", []string{"mon"})
services.BuildingService.AddBuilding("10:00", "10h", []string{"mon"})
room := CreateRoom(0, 420, 69)
if room.Number != 420 {
......
......@@ -55,7 +55,7 @@ func getRoom(res http.ResponseWriter, req *http.Request) {
return
}
room, err := s.BuildingManager.GetRoom(r.Building, r.Number)
room, err := s.BuildingService.GetRoom(r.Building, r.Number)
if err == nil {
err := u.SendJSON(res).Encode(room)
......@@ -69,7 +69,7 @@ func getRoom(res http.ResponseWriter, req *http.Request) {
func getAllRooms(res http.ResponseWriter, req *http.Request) {
rooms := s.BuildingManager.GetRooms()
rooms := s.BuildingService.GetRooms()
err := u.SendJSON(res).Encode(rooms)
if err != nil {
......@@ -91,7 +91,7 @@ func updateRoom(res http.ResponseWriter, req *http.Request) {
return
}
room, err := s.BuildingManager.UpdateRoomCapacity(r.Building, r.Number, r.Capacity)
room, err := s.BuildingService.UpdateRoomCapacity(r.Building, r.Number, r.Capacity)
if err == nil {
err := u.SendJSON(res).Encode(room)
if err != nil {
......@@ -115,7 +115,7 @@ func deleteRoom(res http.ResponseWriter, req *http.Request) {
return
}
err := s.BuildingManager.DeleteRoom(r.Building, r.Number)
err := s.BuildingService.DeleteRoom(r.Building, r.Number)
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
log.Fatal(err)
......
......@@ -30,9 +30,9 @@ func main() {
// Create sample objects in memory for playing around
func initSampleData() {
// Add Building
services.BuildingManager.AddBuilding("08:00", "10h", []string{"mon", "tue", "wed", "thu", "fri", "sat"})
services.BuildingService.AddBuilding("08:00", "10h", []string{"mon", "tue", "wed", "thu", "fri", "sat"})
// Add Rooms
_, err := services.BuildingManager.AddRoom(0, 310, 20)
_, err := services.BuildingService.AddRoom(0, 310, 20)
if err != nil {
log.Fatal(err)
}
......@@ -40,12 +40,12 @@ func initSampleData() {
// Add sample Appointments
a1, _ := model.Appointment{}.New("27-11-2016 10:00", "2h")
_, err = services.AppointmentPlanner.AddAppointment(a1)
_, err = services.AppointmentService.AddAppointment(a1)
if err != nil {
log.Fatal(err)
}
a2, _ := model.Appointment{}.New("27-11-2016 13:00", "2h")
_, err = services.AppointmentPlanner.AddAppointment(a2)
_, err = services.AppointmentService.AddAppointment(a2)
if err != nil {
log.Fatal(err)
}
......
......@@ -5,17 +5,17 @@ import (
"log"
)
type appointmentPlanner struct {
type appointmentService struct {
}
var (
AppointmentPlanner = &appointmentPlanner{}
AppointmentService = &appointmentService{}
)
// AddAppointment books a room on a date.
// returns the assigned room or an error if no room is free
func (a *appointmentPlanner) AddAppointment(reqAppoint model.Appointment) (room *model.Room, err error) {
selectedRooms, err := BuildingManager.FindAvailableRooms(reqAppoint)
func (a *appointmentService) AddAppointment(reqAppoint model.Appointment) (room *model.Room, err error) {
selectedRooms, err := BuildingService.FindAvailableRooms(reqAppoint)
// Add Appointment to room with lowest capacity
if len(selectedRooms) > 0 {
selectedRooms[0].AddAppointment(reqAppoint)
......@@ -26,8 +26,8 @@ func (a *appointmentPlanner) AddAppointment(reqAppoint model.Appointment) (room
// DeleteAppointment deletes an appointment on higher level.
// Takes room number and start date, finds room and deletes appointment.
func (a *appointmentPlanner) DeleteAppointment(buildingId int, roomNumber int, startDate string) (err error) {
building, err := BuildingManager.GetBuilding(buildingId)
func (a *appointmentService) DeleteAppointment(buildingId int, roomNumber int, startDate string) (err error) {
building, err := BuildingService.GetBuilding(buildingId)
if err != nil {
log.Print(err)
......@@ -43,11 +43,11 @@ func (a *appointmentPlanner) DeleteAppointment(buildingId int, roomNumber int, s
// GetAppointments returns all appointments in a map, where roomNumber is the key
func (a *appointmentPlanner) GetAppointments() (appointmentsMap map[int]map[int][]model.Appointment) {
func (a *appointmentService) GetAppointments() (appointmentsMap map[int]map[int][]model.Appointment) {
appointmentsMap = make(map[int]map[int][]model.Appointment)
for _, building := range BuildingManager.Buildings {
for _, building := range BuildingService.Buildings {
if _, ok := appointmentsMap[building.Id]; !ok {
appointmentsMap[building.Id] = make(map[int][]model.Appointment)
}
......@@ -59,9 +59,9 @@ func (a *appointmentPlanner) GetAppointments() (appointmentsMap map[int]map[int]
}
// GetAppointment gets a specific appointment in a room, returns map where roomNumber is key
func (a *appointmentPlanner) GetAppointment(buildingId int, roomNumber int, startDate string) (appointmentMap map[int]map[int]*model.Appointment, err error) {
func (a *appointmentService) GetAppointment(buildingId int, roomNumber int, startDate string) (appointmentMap map[int]map[int]*model.Appointment, err error) {
appointmentMap = make(map[int]map[int]*model.Appointment)
building, err := BuildingManager.GetBuilding(buildingId)
building, err := BuildingService.GetBuilding(buildingId)
if building != nil {
......
......@@ -7,10 +7,10 @@ import (
"git.coco.study/fvitt/good2go/internal/model"
)
func TestAppointmentPlanner_AddAppointment(t *testing.T) {
func TestAppointmentService_AddAppointment(t *testing.T) {
BuildDefBuilding()
appointment, _ := model.Appointment{}.New("20-05-2020 10:00", "8h")
room, err := AppointmentPlanner.AddAppointment(appointment)
room, err := AppointmentService.AddAppointment(appointment)
if err != nil {
t.Error(err)
}
......@@ -19,16 +19,16 @@ func TestAppointmentPlanner_AddAppointment(t *testing.T) {
}
}
func TestAppointmentPlanner_DeleteAppointment(t *testing.T) {
func TestAppointmentService_DeleteAppointment(t *testing.T) {
BuildDefBuilding()
appointment, _ := model.Appointment{}.New("01-06-2020 10:00", "2h")
_, err := AppointmentPlanner.AddAppointment(appointment)
_, err := AppointmentService.AddAppointment(appointment)
if err != nil {
t.Error(err)
}
err = AppointmentPlanner.DeleteAppointment(0, 200, "01-06-2020 10:00")
building, err := BuildingManager.GetBuilding(0)
err = AppointmentService.DeleteAppointment(0, 200, "01-06-2020 10:00")
building, err := BuildingService.GetBuilding(0)
if err != nil {
log.Fatal(err)
} else {
......@@ -42,30 +42,30 @@ func TestAppointmentPlanner_DeleteAppointment(t *testing.T) {
}
// Test non existent building
err = AppointmentPlanner.DeleteAppointment(1, 200, "01-06-2020 10:00")
err = AppointmentService.DeleteAppointment(1, 200, "01-06-2020 10:00")
if err == nil {
t.Error("error expected, got: ", err)
}
}
func TestAppointmentPlanner_GetAppointments(t *testing.T) {
func TestAppointmentService_GetAppointments(t *testing.T) {
BuildDefBuilding()
ap1, _ := model.Appointment{}.New("01-06-2020 10:00", "2h")
ap2, _ := model.Appointment{}.New("01-06-2020 12:30", "2h")
_, err := AppointmentPlanner.AddAppointment(ap1)
_, err = AppointmentPlanner.AddAppointment(ap2)
_, err := AppointmentService.AddAppointment(ap1)
_, err = AppointmentService.AddAppointment(ap2)
if err != nil {
t.Error(err)
}
_, err = BuildingManager.AddRoom(0, 250, 20)
_, err = BuildingService.AddRoom(0, 250, 20)
ap3, _ := model.Appointment{}.New("01-06-2020 10:00", "2h")
ap4, _ := model.Appointment{}.New("01-06-2020 12:30", "2h")
_, err = AppointmentPlanner.AddAppointment(ap3)
_, err = AppointmentPlanner.AddAppointment(ap4)
_, err = AppointmentService.AddAppointment(ap3)
_, err = AppointmentService.AddAppointment(ap4)
appointments := AppointmentPlanner.GetAppointments()
appointments := AppointmentService.GetAppointments()
if len(appointments[0]) != 2 {
t.Error("Not all appointments found, expected 2 rooms, 4 appointments, got: ", appointments)
......@@ -76,17 +76,17 @@ func TestAppointmentPlanner_GetAppointments(t *testing.T) {
}
}
func TestAppointmentPlanner_GetAppointment(t *testing.T) {
func TestAppointmentService_GetAppointment(t *testing.T) {
BuildDefBuilding()
requestedAppointment, _ := model.Appointment{}.New("01-06-2020 10:00", "2h")
requestedAppointment2, _ := model.Appointment{}.New("01-06-2020 14:00", "2h")
_, err := AppointmentPlanner.AddAppointment(requestedAppointment)
_, err = AppointmentPlanner.AddAppointment(requestedAppointment2)
_, err := AppointmentService.AddAppointment(requestedAppointment)
_, err = AppointmentService.AddAppointment(requestedAppointment2)
if err != nil {
t.Error(err)
}
appointment, err := AppointmentPlanner.GetAppointment(0, 200, "01-06-2020 10:00")
appointment, err := AppointmentService.GetAppointment(0, 200, "01-06-2020 10:00")
if err != nil {
t.Error(err)
}
......@@ -98,14 +98,14 @@ func TestAppointmentPlanner_GetAppointment(t *testing.T) {
// Resets building
func ResetBuildingManager() {
BuildingManager.New()
BuildingManager.AddBuilding("08:00", "10h", []string{"mon"})
BuildingService.New()
BuildingService.AddBuilding("08:00", "10h", []string{"mon"})
}
// Resets building(0) and adds room 200 with capacity 40
func BuildDefBuilding() {
ResetBuildingManager()
_, err := BuildingManager.AddRoom(0, 200, 40)
_, err := BuildingService.AddRoom(0, 200, 40)
if err != nil {
log.Fatal("BuildDefBuilding: ", err)
}
......
......@@ -7,21 +7,21 @@ import (
"log"
)
type buildingManager struct {
type buildingService struct {
Buildings []*model.Building
}
var (
BuildingManager = &buildingManager{}
BuildingService = &buildingService{}
)
func (b *buildingManager) New() *buildingManager {
func (b *buildingService) New() *buildingService {
b.Buildings = []*model.Building{}
return b
}
func (b *buildingManager) AddBuilding(openFrom string, openDuration string, workdays []string) *model.Building {
func (b *buildingService) AddBuilding(openFrom string, openDuration string, workdays []string) *model.Building {
b2 := model.Building{}
building := *b2.New(openFrom, openDuration, workdays)
building.Id = len(b.Buildings)
......@@ -29,7 +29,7 @@ func (b *buildingManager) AddBuilding(openFrom string, openDuration string, work
return &building
}
func (b *buildingManager) GetBuilding(id int) (building *model.Building, err error) {
func (b *buildingService) GetBuilding(id int) (building *model.Building, err error) {
if len(b.Buildings)-1 >= id {
building = b.Buildings[id]
return building, err
......@@ -39,7 +39,7 @@ func (b *buildingManager) GetBuilding(id int) (building *model.Building, err err
return building, err
}
func (b *buildingManager) AddRoom(buildingId int, roomNumber int, capacity int) (room *model.Room, err error) {
func (b *buildingService) AddRoom(buildingId int, roomNumber int, capacity int) (room *model.Room, err error) {
building, err := b.GetBuilding(buildingId)
if err != nil {
log.Print(err)
......@@ -49,7 +49,7 @@ func (b *buildingManager) AddRoom(buildingId int, roomNumber int, capacity int)
return room, err
}
func (b *buildingManager) GetRoom(buildingId int, roomNumber int) (room *model.Room, err error) {
func (b *buildingService) GetRoom(buildingId int, roomNumber int) (room *model.Room, err error) {
building, err := b.GetBuilding(buildingId)
if err != nil {
fmt.Println(err)
......@@ -59,7 +59,7 @@ func (b *buildingManager) GetRoom(buildingId int, roomNumber int) (room *model.R
return room, err
}
func (b *buildingManager) GetRooms() (roomsMap map[int][]*model.Room) {
func (b *buildingService) GetRooms() (roomsMap map[int][]*model.Room) {
roomsMap = make(map[int][]*model.Room)
for _, building := range b.Buildings {
for _, room := range building.Rooms {
......@@ -69,7 +69,7 @@ func (b *buildingManager) GetRooms() (roomsMap map[int][]*model.Room) {
return roomsMap
}
func (b *buildingManager) DeleteRoom(buildingId int, roomNumber int) (err error) {
func (b *buildingService) DeleteRoom(buildingId int, roomNumber int) (err error) {
building, err := b.GetBuilding(buildingId)
if err != nil {
fmt.Println(err)
......@@ -79,7 +79,7 @@ func (b *buildingManager) DeleteRoom(buildingId int, roomNumber int) (err error)
return err
}
func (b *buildingManager) UpdateRoomCapacity(buildingId int, roomNumber int, newCapacity int) (room *model.Room, err error) {
func (b *buildingService) UpdateRoomCapacity(buildingId int, roomNumber int, newCapacity int) (room *model.Room, err error) {
building, err := b.GetBuilding(buildingId)
if err != nil {
fmt.Println(err)
......@@ -89,7 +89,7 @@ func (b *buildingManager) UpdateRoomCapacity(buildingId int, roomNumber int, new
return room, err
}
func (b *buildingManager) FindAvailableRooms(reqAppoint model.Appointment) (selectedRooms []*model.Room, err error) {
func (b *buildingService) FindAvailableRooms(reqAppoint model.Appointment) (selectedRooms []*model.Room, err error) {
if len(b.Buildings) <= 0 {
err = errors.New("no buildings.")
} else {
......
......@@ -4,8 +4,8 @@ import (
"testing"
)
func TestBuildingManager_AddRoom(t *testing.T) {
room, err := BuildingManager.AddRoom(0, 250, 50)
func TestBuildingService_AddRoom(t *testing.T) {
room, err := BuildingService.AddRoom(0, 250, 50)
if err != nil {
t.Error(err)
}
......@@ -18,18 +18,18 @@ func TestBuildingManager_AddRoom(t *testing.T) {
}
// produce error
_, err = BuildingManager.AddRoom(1, 200, 20)
_, err = BuildingService.AddRoom(1, 200, 20)
if err == nil {
t.Error("expected error, got: ", err)
}
}
func TestBuildingManager_GetRoom(t *testing.T) {
func TestBuildingService_GetRoom(t *testing.T) {
BuildDefBuilding()
// try to get an actual room
room, err := BuildingManager.GetRoom(0, 200)
room, err := BuildingService.GetRoom(0, 200)
if err != nil {
t.Error(err)
}
......@@ -40,15 +40,15 @@ func TestBuildingManager_GetRoom(t *testing.T) {
room = nil
// pass wrong building id
room, err = BuildingManager.GetRoom(1, 200)
room, err = BuildingService.GetRoom(1, 200)
if err == nil {
t.Error("error expected, got: ", err)
}
}
func TestBuildingManager_GetRooms(t *testing.T) {
func TestBuildingService_GetRooms(t *testing.T) {
BuildDefBuilding()
roomsMap := BuildingManager.GetRooms()
roomsMap := BuildingService.GetRooms()
if len(roomsMap) <= 0 {
t.Error("did not get any room, expected one")
}
......@@ -62,13 +62,13 @@ func TestBuildingManager_GetRooms(t *testing.T) {
}
}
func TestBuildingManager_DeleteRoom(t *testing.T) {
func TestBuildingService_DeleteRoom(t *testing.T) {
BuildDefBuilding()
err := BuildingManager.DeleteRoom(0, 200)
err := BuildingService.DeleteRoom(0, 200)
if err != nil {
t.Error(err)
}
building, err := BuildingManager.GetBuilding(0)
building, err := BuildingService.GetBuilding(0)
if err == nil {
if len(building.Rooms) > 0 {
t.Error("too many rooms left, expected 0, got: ", len(building.Rooms))
......@@ -76,17 +76,17 @@ func TestBuildingManager_DeleteRoom(t *testing.T) {
}
// produce error
err = BuildingManager.DeleteRoom(1, 200)
err = BuildingService.DeleteRoom(1, 200)
if err == nil {
t.Error("expected error, got: ", err)
}
}
func TestBuildingManager_UpdateRoomCapacity(t *testing.T) {
func TestBuildingService_UpdateRoomCapacity(t *testing.T) {
BuildDefBuilding()
room, err := BuildingManager.UpdateRoomCapacity(0, 200, 20)
room2, err := BuildingManager.GetRoom(0, 200)
room, err := BuildingService.UpdateRoomCapacity(0, 200, 20)
room2, err := BuildingService.GetRoom(0, 200)
if err != nil {
t.Error(err)
}
......@@ -96,7 +96,7 @@ func TestBuildingManager_UpdateRoomCapacity(t *testing.T) {
}
// produce error
room, err = BuildingManager.UpdateRoomCapacity(1, 200, 20)
room, err = BuildingService.UpdateRoomCapacity(1, 200, 20)
if err == nil {
t.Error("expected error, got: ", err)
}
......
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