Files
htwkalender-pwa/backend/service/events/eventService.go
2023-10-25 16:54:48 +02:00

171 lines
5.2 KiB
Go

package events
import (
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"htwkalender/model"
"htwkalender/service/db"
"htwkalender/service/fetch"
"htwkalender/service/functions"
)
func GetModulesForCourseDistinct(app *pocketbase.PocketBase, course string, semester string) (model.Events, error) {
modules, err := db.GetAllModulesForCourse(app, course, semester)
replaceEmptyEntry(modules, "Sonderveranstaltungen")
return modules, err
}
// replaceEmptyEntry replaces an empty entry in a module with a replacement string
// If the module is not empty, nothing happens
func replaceEmptyEntry(modules model.Events, replacement string) {
for i, module := range modules {
if functions.OnlyWhitespace(module.Name) {
modules[i].Name = 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, c echo.Context) error {
modules, err := db.GetAllModulesDistinctByNameAndCourse(app)
replaceEmptyEntry(modules, "Sonderveranstaltungen")
if err != nil {
return c.JSON(400, err)
} else {
return c.JSON(200, modules)
}
}
// GetModuleByName returns a module by its name
// If the module does not exist, an error is returned
// If the module exists, the module is returned
// Module is a struct that exists in database as events
func GetModuleByName(app *pocketbase.PocketBase, name string) (model.Module, error) {
events, err := db.FindAllEventsByModule(app, name)
if err != nil || len(events) == 0 {
return model.Module{}, err
} else {
return model.Module{
UUID: events[0].UUID,
Name: 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.DeleteAllEventsForCourse(app, course, semester)
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) error {
//new string array with one element (course)
var courses []string
courses = append(courses, course)
seminarGroups := fetch.GetSeminarGroupsEventsFromHTML(courses)
seminarGroups = fetch.ClearEmptySeminarGroups(seminarGroups)
seminarGroups = fetch.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 apis.NewNotFoundError("Events for winter semester could not be found", 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 apis.NewNotFoundError("Events for summer semester could not be found", err)
}
events = append(events, summerEvents...)
//if there are no events in the database, save the new events
if len(events) == 0 {
_, dbError := db.SaveEvents(seminarGroups, app)
if dbError != nil {
return apis.NewNotFoundError("Events could not be saved", dbError)
}
return 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
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 err
}
err = DeleteAllEventsByCourseAndSemester(app, course, "ss")
if err != nil {
return err
}
//save the new events
_, dbError := db.SaveEvents(seminarGroups, app)
if dbError != nil {
return apis.NewNotFoundError("Events could not be saved", dbError)
}
return nil
}
}
}
return 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
}