mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-07-16 09:38:51 +02:00
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package events
|
|
|
|
import (
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/pocketbase/pocketbase"
|
|
"htwk-planner/service/db"
|
|
"unicode"
|
|
)
|
|
|
|
func GetModulesForCourseDistinct(app *pocketbase.PocketBase, c echo.Context, course string, semester string) 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)
|
|
}
|
|
}
|
|
|
|
func replaceEmptyEntryInStringArray(modules []string, replacement string) {
|
|
//replace empty string with "Sonderveranstaltungen"
|
|
for i, module := range modules {
|
|
if checkIfOnlyWhitespace(module) {
|
|
modules[i] = replacement
|
|
}
|
|
}
|
|
}
|
|
|
|
func replaceEmptyEntry(modules []struct {
|
|
Name string
|
|
Course string
|
|
}, replacement string) {
|
|
//replace empty string with "Sonderveranstaltungen"
|
|
for i, module := range modules {
|
|
if checkIfOnlyWhitespace(module.Name) {
|
|
modules[i].Name = replacement
|
|
}
|
|
}
|
|
}
|
|
|
|
// check if course is empty or contains only whitespaces
|
|
func checkIfOnlyWhitespace(word string) bool {
|
|
for _, letter := range word {
|
|
if !unicode.IsSpace(letter) || !(letter == int32(160)) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func GetAllModulesDistinct(app *pocketbase.PocketBase, c echo.Context) error {
|
|
modules, err := db.GetAllModulesDistinct(app)
|
|
|
|
replaceEmptyEntry(modules, "Sonderveranstaltungen")
|
|
|
|
if err != nil {
|
|
return c.JSON(400, err)
|
|
} else {
|
|
return c.JSON(200, modules)
|
|
}
|
|
}
|