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

@@ -9,6 +9,7 @@ import (
"htwkalender/service/fetch" "htwkalender/service/fetch"
"htwkalender/service/ical" "htwkalender/service/ical"
"htwkalender/service/room" "htwkalender/service/room"
"io"
"net/http" "net/http"
"os" "os"
) )
@@ -71,6 +72,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
return nil return nil
}) })
// API Endpoint to get all events for a specific room on a specific day
app.OnBeforeServe().Add(func(e *core.ServeEvent) error { app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{ _, err := e.Router.AddRoute(echo.Route{
Method: http.MethodGet, Method: http.MethodGet,
@@ -90,12 +92,19 @@ func AddRoutes(app *pocketbase.PocketBase) {
return nil return nil
}) })
// API Endpoint to create a new iCal feed
app.OnBeforeServe().Add(func(e *core.ServeEvent) error { app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{ _, err := e.Router.AddRoute(echo.Route{
Method: http.MethodPost, Method: http.MethodPost,
Path: "/api/createFeed", Path: "/api/createFeed",
Handler: func(c echo.Context) error { 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{ Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app), apis.ActivityLogger(app),

View File

@@ -13,7 +13,10 @@ func AddSchedules(app *pocketbase.PocketBase) {
app.OnBeforeServe().Add(func(e *core.ServeEvent) error { app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
scheduler := cron.New() scheduler := cron.New()
scheduler.MustAdd("updateCourse", "*/60 * * * *", func() { // 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", "*/10 * * * *", func() {
courses := events.GetAllCourses(app) courses := events.GetAllCourses(app)

View File

@@ -77,6 +77,8 @@ func findEventByDayWeekStartEndNameCourse(event model.Event, course string, app
return &event, err return &event, err
} }
// 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 { func GetPlanForModules(app *pocketbase.PocketBase, modules []model.FeedCollection) model.Events {
// build query functions with name equals elements in modules for dbx query // build query functions with name equals elements in modules for dbx query

View File

@@ -82,17 +82,6 @@ func DeleteAllEventsByCourseAndSemester(app *pocketbase.PocketBase, course strin
// If the update was not successful, an error is returned // If the update was not successful, an error is returned
func UpdateModulesForCourse(app *pocketbase.PocketBase, course string) error { 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) //new string array with one element (course)
var courses []string var courses []string
courses = append(courses, course) courses = append(courses, course)
@@ -108,11 +97,68 @@ func UpdateModulesForCourse(app *pocketbase.PocketBase, course string) error {
seminarGroups = fetch.ReplaceEmptyEventNames(seminarGroups) 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 { //get all events for the course and the semester
return apis.NewNotFoundError("Events could not be saved", dbError) 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 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

@@ -9,7 +9,6 @@ import (
"github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/apis"
"htwkalender/model" "htwkalender/model"
"htwkalender/service/db" "htwkalender/service/db"
"io"
"net/http" "net/http"
"time" "time"
) )
@@ -63,18 +62,12 @@ func writeSuccess(message string, w http.ResponseWriter) {
} }
} }
func CreateIndividualFeed(c echo.Context, app *pocketbase.PocketBase) error { func CreateIndividualFeed(requestBody []byte, app *pocketbase.PocketBase) (string, error) {
// read json from request body
var modules []model.FeedCollection 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 { 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 var feed model.Feed
@@ -83,13 +76,13 @@ func CreateIndividualFeed(c echo.Context, app *pocketbase.PocketBase) error {
collection, dbError := db.FindCollection(app, "feeds") collection, dbError := db.FindCollection(app, "feeds")
if dbError != nil { 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) record, err := db.SaveFeed(feed, collection, app)
if err != nil { 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
} }