fix:#25 changed course updater insert and delete procedure

This commit is contained in:
Elmar Kresse
2024-04-21 11:59:20 +02:00
parent e13dad48b4
commit c8bcc3be94
4 changed files with 100 additions and 108 deletions

View File

@@ -22,6 +22,7 @@ import (
"htwkalender/service/db"
"htwkalender/service/fetch/v1"
"htwkalender/service/functions"
"log/slog"
)
func GetModulesForCourseDistinct(app *pocketbase.PocketBase, course string, semester string) (model.Events, error) {
@@ -120,15 +121,11 @@ func DeleteAllEvents(app *pocketbase.PocketBase) error {
// 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)
seminarGroup := v1.GetSeminarGroupEventsFromHTML(course)
seminarGroups := v1.GetSeminarGroupsEventsFromHTML(courses)
seminarGroup = v1.ClearEmptySeminarGroups(seminarGroup)
seminarGroups = v1.ClearEmptySeminarGroups(seminarGroups)
seminarGroups = v1.ReplaceEmptyEventNames(seminarGroups)
seminarGroup = v1.ReplaceEmptyEventNames(seminarGroup)
//check if events in the seminarGroups Events are already in the database
//if yes, keep the database as it is
@@ -151,43 +148,45 @@ func UpdateModulesForCourse(app *pocketbase.PocketBase, course string) (model.Ev
//if there are no events in the database, save the new events
if len(events) == 0 {
events, dbError := db.SaveSeminarGroupEvents(seminarGroups, app)
events, dbError := db.SaveSeminarGroupEvents(seminarGroup, 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
// Create partial update list and delete list for the events
var insertList model.Events
var deleteList model.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...)
}
// check which events are not already in the database and need to be inserted/saved
for _, event := range seminarGroup.Events {
if !ContainsEvent(events, event) {
insertList = append(insertList, event)
}
}
// save all events that are in the insertList
savedEvents, err := db.SaveEvents(insertList, app)
if err != nil {
slog.Error("Failed to save events: %v", err)
return nil, err
}
// check which events are in the database but not in the seminarGroup and need to be deleted
for _, event := range events {
if !ContainsEvent(seminarGroup.Events, event) {
deleteList = append(deleteList, event)
}
}
// delete all events that are in the deleteList
err = db.DeleteEvents(deleteList, app)
if err != nil {
slog.Error("Failed to delete events: %v", err)
return nil, err
}
return savedEvents, nil
}