mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-16 09:38:49 +02:00
208 lines
6.3 KiB
Go
208 lines
6.3 KiB
Go
//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 events
|
|
|
|
import (
|
|
"github.com/pocketbase/pocketbase"
|
|
"htwkalender/model"
|
|
"htwkalender/service/db"
|
|
"htwkalender/service/fetch/v1"
|
|
"htwkalender/service/functions"
|
|
)
|
|
|
|
func GetModulesForCourseDistinct(app *pocketbase.PocketBase, course string, semester string) (model.Events, error) {
|
|
|
|
modules, err := db.GetAllModulesForCourse(app, course, semester)
|
|
|
|
// Convert the []model.Module to []Named
|
|
var namedEvents []Named
|
|
for _, module := range modules {
|
|
namedEvents = append(namedEvents, &module)
|
|
}
|
|
|
|
replaceEmptyEntry(namedEvents, "Sonderveranstaltungen")
|
|
return modules, err
|
|
}
|
|
|
|
type Named interface {
|
|
GetName() string
|
|
SetName(name string)
|
|
}
|
|
|
|
// replaceEmptyEntry replaces an empty entry in a module with a replacement string
|
|
// If the module is not empty, nothing happens
|
|
func replaceEmptyEntry(namedList []Named, replacement string) {
|
|
for i, namedItem := range namedList {
|
|
if functions.OnlyWhitespace(namedItem.GetName()) {
|
|
namedList[i].SetName(replacement)
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetAllModulesDistinct returns all modules distinct by name and course from the database
|
|
// That means you get all modules with duplicates if they have different courses
|
|
func GetAllModulesDistinct(app *pocketbase.PocketBase) ([]model.ModuleDTO, error) {
|
|
modules, err := db.GetAllModulesDistinctByNameAndCourse(app)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var namedModules []Named
|
|
for _, module := range modules {
|
|
namedModules = append(namedModules, &module)
|
|
}
|
|
replaceEmptyEntry(namedModules, "Sonderveranstaltungen")
|
|
return modules, nil
|
|
}
|
|
|
|
func GetModuleByUUID(app *pocketbase.PocketBase, uuid string) (model.Module, error) {
|
|
module, findModuleErr := db.FindModuleByUUID(app, uuid)
|
|
if findModuleErr != nil {
|
|
return model.Module{}, findModuleErr
|
|
}
|
|
|
|
events, findEventsError := db.FindAllEventsByModule(app, module)
|
|
if findEventsError != nil || len(events) == 0 {
|
|
return model.Module{}, findEventsError
|
|
} else {
|
|
return model.Module{
|
|
UUID: events[0].UUID,
|
|
Name: events[0].Name,
|
|
Events: events,
|
|
Prof: events[0].Prof,
|
|
Course: events[0].Course,
|
|
Semester: events[0].Semester,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
// DeleteAllEventsByCourseAndSemester deletes all events for a course and a semester
|
|
// If the deletion was successful, nil is returned
|
|
// If the deletion was not successful, an error is returned
|
|
func DeleteAllEventsByCourseAndSemester(app *pocketbase.PocketBase, course string, semester string) error {
|
|
err := db.DeleteAllEventsByCourse(app, course, semester)
|
|
if err != nil {
|
|
return err
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func DeleteAllEvents(app *pocketbase.PocketBase) error {
|
|
err := db.DeleteAllEvents(app)
|
|
if err != nil {
|
|
return err
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// UpdateModulesForCourse updates all modules for a course
|
|
// Does Updates for ws and ss semester sequentially
|
|
// Update runs through the following steps:
|
|
// 1. Delete all events for the course and the semester
|
|
// 2. Fetch all events for the course and the semester
|
|
// 3. Save all events for the course and the semester
|
|
// If the update was successful, nil is returned
|
|
// If the update was not successful, an error is returned
|
|
func UpdateModulesForCourse(app *pocketbase.PocketBase, course string) (model.Events, error) {
|
|
|
|
//new string array with one element (course)
|
|
var courses []string
|
|
courses = append(courses, course)
|
|
|
|
seminarGroups := v1.GetSeminarGroupsEventsFromHTML(courses)
|
|
|
|
seminarGroups = v1.ClearEmptySeminarGroups(seminarGroups)
|
|
|
|
seminarGroups = v1.ReplaceEmptyEventNames(seminarGroups)
|
|
|
|
//check if events in the seminarGroups Events are already in the database
|
|
//if yes, keep the database as it is
|
|
//if no, delete all events for the course and the semester and save the new events
|
|
//if there are no events in the database, save the new events
|
|
|
|
//get all events for the course and the semester
|
|
events, err := db.GetAllModulesForCourse(app, course, "ws")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// append all events for the course and the semester to the events array for ss
|
|
summerEvents, err := db.GetAllModulesForCourse(app, course, "ss")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
events = append(events, summerEvents...)
|
|
|
|
//if there are no events in the database, save the new events
|
|
if len(events) == 0 {
|
|
events, dbError := db.SaveSeminarGroupEvents(seminarGroups, app)
|
|
if dbError != nil {
|
|
return nil, dbError
|
|
}
|
|
return events, nil
|
|
}
|
|
|
|
//check if events in the seminarGroups Events are already in the database
|
|
//if yes, keep the database as it is
|
|
//if no, delete all events for the course and the semester and save the new events
|
|
|
|
var savedEvents model.Events
|
|
|
|
for _, seminarGroup := range seminarGroups {
|
|
for _, event := range seminarGroup.Events {
|
|
// if the event is not in the database, delete all events for the course and the semester and save the new events
|
|
if !ContainsEvent(events, event) {
|
|
|
|
err = DeleteAllEventsByCourseAndSemester(app, course, "ws")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = DeleteAllEventsByCourseAndSemester(app, course, "ss")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
//save the new events
|
|
savedEvent, dbError := db.SaveSeminarGroupEvents(seminarGroups, app)
|
|
if dbError != nil {
|
|
return nil, dbError
|
|
}
|
|
savedEvents = append(savedEvents, savedEvent...)
|
|
}
|
|
}
|
|
}
|
|
return savedEvents, nil
|
|
}
|
|
|
|
func ContainsEvent(events model.Events, event model.Event) bool {
|
|
for _, e := range events {
|
|
if e.Name == event.Name &&
|
|
e.Prof == event.Prof &&
|
|
e.Rooms == event.Rooms &&
|
|
e.Semester == event.Semester &&
|
|
e.Start == event.Start &&
|
|
e.End == event.End &&
|
|
e.Course == event.Course {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|