fixed schedule and update process

This commit is contained in:
masterelmar
2023-10-08 18:22:34 +02:00
parent 3225a9040e
commit 9a1342b954
5 changed files with 82 additions and 29 deletions

View File

@@ -82,17 +82,6 @@ func DeleteAllEventsByCourseAndSemester(app *pocketbase.PocketBase, course strin
// If the update was not successful, an error is returned
func UpdateModulesForCourse(app *pocketbase.PocketBase, course string) error {
var err error
err = DeleteAllEventsByCourseAndSemester(app, course, "ws")
if err != nil {
return err
}
err = DeleteAllEventsByCourseAndSemester(app, course, "ss")
if err != nil {
return err
}
//new string array with one element (course)
var courses []string
courses = append(courses, course)
@@ -108,11 +97,68 @@ func UpdateModulesForCourse(app *pocketbase.PocketBase, course string) error {
seminarGroups = fetch.ReplaceEmptyEventNames(seminarGroups)
_, dbError = db.SaveEvents(seminarGroups, collection, app)
//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
if dbError != nil {
return apis.NewNotFoundError("Events could not be saved", dbError)
//get all events for the course and the semester
events, err := db.GetAllModulesForCourse(app, course, "ws")
if err != nil {
return apis.NewNotFoundError("Events could not be found", err)
}
//if there are no events in the database, save the new events
if len(events) == 0 {
_, dbError = db.SaveEvents(seminarGroups, collection, 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, collection, 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
}