Merge branch 'main' into 10-roomfinder

This commit is contained in:
Elmar Kresse
2023-10-20 19:46:06 +02:00
45 changed files with 2374 additions and 481 deletions

View File

@@ -9,7 +9,9 @@ import (
"htwkalender/service/fetch"
"htwkalender/service/ical"
"htwkalender/service/room"
"io"
"net/http"
"net/url"
"os"
)
@@ -71,6 +73,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
return nil
})
// API Endpoint to get all events for a specific room on a specific day
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
@@ -90,6 +93,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
return nil
})
// API Endpoint to create a new iCal feed
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
@@ -114,7 +118,13 @@ func AddRoutes(app *pocketbase.PocketBase) {
Method: http.MethodPost,
Path: "/api/createFeed",
Handler: func(c echo.Context) error {
return ical.CreateIndividualFeed(c, app)
requestBody, _ := io.ReadAll(c.Request().Body)
result, err := ical.CreateIndividualFeed(requestBody, app)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusOK, result)
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
@@ -151,7 +161,13 @@ func AddRoutes(app *pocketbase.PocketBase) {
Handler: func(c echo.Context) error {
course := c.QueryParam("course")
semester := c.QueryParam("semester")
return events.GetModulesForCourseDistinct(app, c, course, semester)
modules, err := events.GetModulesForCourseDistinct(app, course, semester)
if err != nil {
return c.JSON(400, err)
} else {
return c.JSON(200, modules)
}
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
@@ -180,12 +196,62 @@ func AddRoutes(app *pocketbase.PocketBase) {
return nil
})
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
Path: "/api/module",
Handler: func(c echo.Context) error {
name := c.Request().Header.Get("Name")
name, err := url.QueryUnescape(name)
module, err := events.GetModuleByName(app, name)
if err != nil {
return c.JSON(400, err)
} else {
return c.JSON(200, module)
}
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
},
})
if err != nil {
return err
}
return nil
})
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
Path: "/api/courses",
Handler: func(c echo.Context) error {
return events.GetAllCourses(app, c)
courses := events.GetAllCourses(app)
return c.JSON(200, courses)
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
},
})
if err != nil {
return err
}
return nil
})
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
Method: http.MethodDelete,
Path: "/api/events",
Handler: func(c echo.Context) error {
course := c.QueryParam("course")
semester := c.QueryParam("semester")
err := events.DeleteAllEventsByCourseAndSemester(app, course, semester)
if err != nil {
return c.JSON(400, err)
} else {
return c.JSON(200, "Events deleted")
}
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),

View File

@@ -0,0 +1,40 @@
package service
import (
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/cron"
"htwkalender/service/events"
"log"
)
func AddSchedules(app *pocketbase.PocketBase) {
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
scheduler := cron.New()
// Every hour update all courses (5 segments - minute, hour, day, month, weekday) "0 * * * *"
// Every three hours update all courses (5 segments - minute, hour, day, month, weekday) "0 */3 * * *"
// Every 10 minutes update all courses (5 segments - minute, hour, day, month, weekday) "*/10 * * * *"
scheduler.MustAdd("updateCourse", "0 */3 * * *", func() {
courses := events.GetAllCourses(app)
for _, course := range courses {
err := events.UpdateModulesForCourse(app, course)
if err != nil {
log.Println("Update Course: " + course + " failed")
log.Println(err)
} else {
log.Println("Update Course: " + course + " successful")
}
}
})
scheduler.Start()
return nil
})
}

View File

@@ -77,74 +77,113 @@ func findEventByDayWeekStartEndNameCourse(event model.Event, course string, app
return &event, err
}
func buildIcalQueryForModules(modules []model.FeedCollection) dbx.Expression {
// build where conditions for each module
//first check if modules is empty
if len(modules) == 0 {
return dbx.HashExp{}
}
//second check if modules has only one element
if len(modules) == 1 {
return dbx.And(
dbx.HashExp{"Name": modules[0].Name},
dbx.HashExp{"course": modules[0].Course},
)
}
//third check if modules has more than one element
var wheres []dbx.Expression
for _, module := range modules {
where := dbx.And(
dbx.HashExp{"Name": module.Name},
dbx.HashExp{"course": module.Course},
)
wheres = append(wheres, where)
}
// Use dbx.And or dbx.Or to combine the where conditions as needed
where := dbx.Or(wheres...)
return where
}
// GetPlanForModules returns all events for the given modules with the given course
// used for the ical feed
func GetPlanForModules(app *pocketbase.PocketBase, modules []model.FeedCollection) model.Events {
// build query functions with name equals elements in modules for dbx query
var events model.Events
var queryString string
for i, module := range modules {
if i == 0 {
queryString = "Name = '" + module.Name + "' AND course = '" + module.Course + "'"
// iterate over modules in 100 batch sizes
for i := 0; i < len(modules); i += 100 {
var moduleBatch []model.FeedCollection
if i+100 > len(modules) {
moduleBatch = modules[i:]
} else {
queryString = queryString + " OR Name = '" + module.Name + "' AND course = '" + module.Course + "'"
moduleBatch = modules[i : i+100]
}
var selectedModulesQuery = buildIcalQueryForModules(moduleBatch)
// get all events from event records in the events collection
err := app.Dao().DB().Select("*").From("events").Where(selectedModulesQuery).All(&events)
if err != nil {
print("Error while getting events from database: ", err)
return nil
}
}
var events model.Events
// get all events from event records in the events collection
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp(queryString)).All(&events)
if err != nil {
print("Error while getting events from database: ", err)
return nil
}
return events
}
func GetAllModulesForCourse(app *pocketbase.PocketBase, course string, semester string) ([]string, error) {
var events []struct {
Name string `db:"Name" json:"Name"`
}
var eventArray []string
func GetAllModulesForCourse(app *pocketbase.PocketBase, course string, semester string) (model.Events, error) {
var events model.Events
// get all events from event records in the events collection
err := app.Dao().DB().Select("Name").From("events").Where(dbx.NewExp("course = {:course} AND semester = {:semester}", dbx.Params{"course": course, "semester": semester})).Distinct(true).All(&events)
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("course = {:course} AND semester = {:semester}", dbx.Params{"course": course, "semester": semester})).GroupBy("Name").Distinct(true).All(&events)
if err != nil {
print("Error while getting events from database: ", err)
return eventArray, err
return nil, err
}
for _, event := range events {
eventArray = append(eventArray, event.Name)
}
return eventArray, nil
return events, nil
}
func GetAllModulesDistinct(app *pocketbase.PocketBase) ([]struct {
Name string
Course string
}, error) {
var events []struct {
Name string `db:"Name" json:"Name"`
Course string `db:"course" json:"course"`
}
func GetAllModulesDistinctByNameAndCourse(app *pocketbase.PocketBase) (model.Events, error) {
var events model.Events
var eventArray []struct {
Name string
Course string
}
err := app.Dao().DB().Select("Name", "course").From("events").Distinct(true).All(&events)
err := app.Dao().DB().Select("*").From("events").GroupBy("Name", "course").Distinct(true).All(&events)
if err != nil {
print("Error while getting events from database: ", err)
return eventArray, err
return nil, err
}
for _, event := range events {
eventArray = append(eventArray, struct {
Name string
Course string
}{event.Name, event.Course})
}
return eventArray, nil
return events, nil
}
func DeleteAllEventsForCourse(app *pocketbase.PocketBase, course string, semester string) error {
_, err := app.Dao().DB().Delete("events", dbx.NewExp("course = {:course} AND semester = {:semester}", dbx.Params{"course": course, "semester": semester})).Execute()
if err != nil {
print("Error while deleting events from database: ", err)
return err
}
return nil
}
func FindAllEventsByModule(app *pocketbase.PocketBase, moduleName string) (model.Events, error) {
var events model.Events
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Name = {:moduleName}", dbx.Params{"moduleName": moduleName})).All(&events)
if err != nil {
print("Error while getting events from database: ", err)
return nil, err
}
return events, nil
}

View File

@@ -0,0 +1,42 @@
package db
import (
"github.com/pocketbase/dbx"
"htwkalender/model"
"reflect"
"testing"
)
func Test_buildIcalQueryForModules(t *testing.T) {
type args struct {
modules []model.FeedCollection
}
tests := []struct {
name string
args args
want dbx.Expression
}{
{
name: "empty modules",
args: args{modules: []model.FeedCollection{}},
want: dbx.HashExp{},
},
{
name: "one module",
args: args{modules: []model.FeedCollection{{Name: "test", Course: "test"}}},
want: dbx.And(dbx.HashExp{"Name": "test"}, dbx.HashExp{"course": "test"}),
},
{
name: "two modules",
args: args{modules: []model.FeedCollection{{Name: "test", Course: "test"}, {Name: "test2", Course: "test2"}}},
want: dbx.Or(dbx.And(dbx.HashExp{"Name": "test"}, dbx.HashExp{"course": "test"}), dbx.And(dbx.HashExp{"Name": "test2"}, dbx.HashExp{"course": "test2"})),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildIcalQueryForModules(tt.args.modules); !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildIcalQueryForModules() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -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)
}

View File

@@ -3,36 +3,24 @@ 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) {
//replace empty functions with "Sonderveranstaltungen"
for i, module := range modules {
if functions.OnlyWhitespace(module) {
modules[i] = replacement
}
}
}
// 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) {
func replaceEmptyEntry(modules []struct {
Name string
Course string
}, replacement string) {
//replace empty functions with "Sonderveranstaltungen"
for i, module := range modules {
if functions.OnlyWhitespace(module.Name) {
modules[i].Name = replacement
@@ -40,8 +28,10 @@ func replaceEmptyEntry(modules []struct {
}
}
// 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.GetAllModulesDistinct(app)
modules, err := db.GetAllModulesDistinctByNameAndCourse(app)
replaceEmptyEntry(modules, "Sonderveranstaltungen")
@@ -51,3 +41,134 @@ 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 {
//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)
//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, 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
}

View File

@@ -28,7 +28,9 @@ func GetSeminarEvents(c echo.Context, app *pocketbase.PocketBase) error {
return apis.NewNotFoundError("Collection not found", dbError)
}
seminarGroups = clearEmptySeminarGroups(seminarGroups)
seminarGroups = ClearEmptySeminarGroups(seminarGroups)
seminarGroups = ReplaceEmptyEventNames(seminarGroups)
savedRecords, dbError := db.SaveEvents(seminarGroups, collection, app)
@@ -39,7 +41,18 @@ func GetSeminarEvents(c echo.Context, app *pocketbase.PocketBase) error {
return c.JSON(http.StatusOK, savedRecords)
}
func clearEmptySeminarGroups(seminarGroups []model.SeminarGroup) []model.SeminarGroup {
func ReplaceEmptyEventNames(groups []model.SeminarGroup) []model.SeminarGroup {
for i, group := range groups {
for j, event := range group.Events {
if event.Name == "" {
groups[i].Events[j].Name = "Sonderveranstaltungen"
}
}
}
return groups
}
func ClearEmptySeminarGroups(seminarGroups []model.SeminarGroup) []model.SeminarGroup {
var newSeminarGroups []model.SeminarGroup
for _, seminarGroup := range seminarGroups {
if len(seminarGroup.Events) > 0 && seminarGroup.Course != "" {

View File

@@ -1,6 +1,10 @@
package fetch
import "testing"
import (
"htwkalender/model"
"reflect"
"testing"
)
func Test_extractSemesterAndYear(t *testing.T) {
type args struct {
@@ -12,7 +16,6 @@ func Test_extractSemesterAndYear(t *testing.T) {
want string
want1 string
}{
// TODO: Add test cases.
{
name: "Test 1",
args: args{
@@ -50,3 +53,68 @@ func Test_extractSemesterAndYear(t *testing.T) {
})
}
}
func Test_replaceEmptyEventNames(t *testing.T) {
type args struct {
groups []model.SeminarGroup
}
tests := []struct {
name string
args args
want []model.SeminarGroup
}{
{
name: "Test 1",
args: args{
groups: []model.SeminarGroup{
{
Events: []model.Event{
{
Name: "Test",
},
},
},
},
},
want: []model.SeminarGroup{
{
Events: []model.Event{
{
Name: "Test",
},
},
},
},
},
{
name: "Test 1",
args: args{
groups: []model.SeminarGroup{
{
Events: []model.Event{
{
Name: "",
},
},
},
},
},
want: []model.SeminarGroup{
{
Events: []model.Event{
{
Name: "Sonderveranstaltungen",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ReplaceEmptyEventNames(tt.args.groups); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReplaceEmptyEventNames() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -20,3 +20,10 @@ func Contains(s []string, e string) bool {
}
return false
}
func ReplaceEmptyString(word string, replacement string) string {
if OnlyWhitespace(word) {
return replacement
}
return word
}

View File

@@ -9,7 +9,6 @@ import (
"github.com/pocketbase/pocketbase/apis"
"htwkalender/model"
"htwkalender/service/db"
"io"
"net/http"
"time"
)
@@ -63,18 +62,12 @@ func writeSuccess(message string, w http.ResponseWriter) {
}
}
func CreateIndividualFeed(c echo.Context, app *pocketbase.PocketBase) error {
// read json from request body
func CreateIndividualFeed(requestBody []byte, app *pocketbase.PocketBase) (string, error) {
var modules []model.FeedCollection
requestBodyBytes, err := io.ReadAll(c.Request().Body)
if err != nil {
return apis.NewApiError(400, "Could not bind request body", err)
}
err = json.Unmarshal(requestBodyBytes, &modules)
err := json.Unmarshal(requestBody, &modules)
if err != nil {
return apis.NewApiError(400, "Could not bind request body", err)
return "", apis.NewNotFoundError("Could not parse request body", err)
}
var feed model.Feed
@@ -83,13 +76,13 @@ func CreateIndividualFeed(c echo.Context, app *pocketbase.PocketBase) error {
collection, dbError := db.FindCollection(app, "feeds")
if dbError != nil {
return apis.NewNotFoundError("Collection not found", dbError)
return "", apis.NewNotFoundError("Collection could not be found", dbError)
}
record, err := db.SaveFeed(feed, collection, app)
if err != nil {
return apis.NewNotFoundError("Feed could not be saved", dbError)
return "", apis.NewNotFoundError("Could not save feed", err)
}
return c.JSON(http.StatusOK, record.Id)
return record.Id, nil
}

View File

@@ -62,6 +62,9 @@ func generateDescription(event *model.Event) string {
if !functions.OnlyWhitespace(event.Course) {
description += "Gruppe: " + event.Course + "\n"
}
if !functions.OnlyWhitespace(event.EventType) {
description += "Typ: " + event.EventType + "\n"
}
return description
}