mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-08 04:39:13 +02:00
feat:#34 refactored function to intended service, fixed docker files
This commit is contained in:
106
services/data-manager/service/room/roomService.go
Normal file
106
services/data-manager/service/room/roomService.go
Normal file
@@ -0,0 +1,106 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package room
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"htwkalender/model"
|
||||
"htwkalender/service/db"
|
||||
"htwkalender/service/functions"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetRooms(app *pocketbase.PocketBase) ([]string, error) {
|
||||
rooms, err := db.GetRooms(app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return rooms, nil
|
||||
}
|
||||
}
|
||||
|
||||
func GetRoomScheduleForDay(app *pocketbase.PocketBase, room string, date string) ([]model.AnonymizedEventDTO, error) {
|
||||
roomSchedule, err := db.GetRoomScheduleForDay(app, room, date)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
anonymizedRoomSchedule := anonymizeRooms(roomSchedule)
|
||||
return anonymizedRoomSchedule, nil
|
||||
}
|
||||
|
||||
func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to string) ([]model.AnonymizedEventDTO, error) {
|
||||
roomSchedule, err := db.GetRoomSchedule(app, room, from, to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
anonymizedRoomSchedule := anonymizeRooms(roomSchedule)
|
||||
return anonymizedRoomSchedule, nil
|
||||
}
|
||||
|
||||
// Transform the events to anonymized events throwing away all unnecessary information
|
||||
func anonymizeRooms(events []model.Event) []model.AnonymizedEventDTO {
|
||||
var anonymizedEvents []model.AnonymizedEventDTO
|
||||
for _, event := range events {
|
||||
anonymizedEvents = append(anonymizedEvents, event.AnonymizeEvent())
|
||||
}
|
||||
return anonymizedEvents
|
||||
}
|
||||
|
||||
func GetFreeRooms(app *pocketbase.PocketBase, from time.Time, to time.Time) ([]string, error) {
|
||||
rooms, err := db.GetRooms(app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var events model.Events
|
||||
events, err = db.GetEventsThatCollideWithTimeRange(app, from, to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
freeRooms := removeRoomsThatHaveEvents(rooms, events)
|
||||
return freeRooms, nil
|
||||
}
|
||||
|
||||
// Remove all rooms from the list that have events in the given time range
|
||||
func removeRoomsThatHaveEvents(rooms []string, schedule []model.Event) []string {
|
||||
var freeRooms []string
|
||||
for _, room := range rooms {
|
||||
if !isRoomInSchedule(room, schedule) {
|
||||
freeRooms = append(freeRooms, room)
|
||||
}
|
||||
}
|
||||
return freeRooms
|
||||
}
|
||||
|
||||
// Check if a room is in the schedule
|
||||
func isRoomInSchedule(room string, schedule []model.Event) bool {
|
||||
for _, event := range schedule {
|
||||
if event.Course != "Sport" {
|
||||
rooms := functions.SeperateRoomString(event.Rooms)
|
||||
// check if room is in rooms
|
||||
for _, r := range rooms {
|
||||
if r == room {
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if event.Rooms == room {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user