mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-08-03 10:19:16 +02:00
wrote descriptions and refactored for new module response with information
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"htwkalender/service/db"
|
||||
)
|
||||
|
||||
func GetAllCourses(app *pocketbase.PocketBase, c echo.Context) error {
|
||||
courses := db.GetAllCourses(app)
|
||||
return c.JSON(200, courses)
|
||||
func GetAllCourses(app *pocketbase.PocketBase) []string {
|
||||
return db.GetAllCourses(app)
|
||||
}
|
||||
|
@@ -3,21 +3,18 @@ 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, c echo.Context, course string, semester string) error {
|
||||
func GetModulesForCourseDistinct(app *pocketbase.PocketBase, course string, semester string) (model.Events, error) {
|
||||
|
||||
modules, err := db.GetAllModulesForCourse(app, course, semester)
|
||||
replaceEmptyEntryInStringArray(modules, "Sonderveranstaltungen")
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(400, err)
|
||||
} else {
|
||||
return c.JSON(200, modules)
|
||||
}
|
||||
replaceEmptyEntry(modules, "Sonderveranstaltungen")
|
||||
return modules, err
|
||||
}
|
||||
|
||||
func replaceEmptyEntryInStringArray(modules []string, replacement string) {
|
||||
@@ -49,3 +46,80 @@ func GetAllModulesDistinct(app *pocketbase.PocketBase, c echo.Context) error {
|
||||
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{
|
||||
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 {
|
||||
|
||||
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)
|
||||
|
||||
seminarGroups := fetch.GetSeminarGroupsEventsFromHTML(courses)
|
||||
|
||||
collection, dbError := db.FindCollection(app, "events")
|
||||
if dbError != nil {
|
||||
return apis.NewNotFoundError("Collection not found", dbError)
|
||||
}
|
||||
|
||||
seminarGroups = fetch.ClearEmptySeminarGroups(seminarGroups)
|
||||
|
||||
seminarGroups = fetch.ReplaceEmptyEventNames(seminarGroups)
|
||||
|
||||
_, dbError = db.SaveEvents(seminarGroups, collection, app)
|
||||
|
||||
if dbError != nil {
|
||||
return apis.NewNotFoundError("Events could not be saved", dbError)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user