mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-08-07 04:09:17 +02:00
3 add occupancy bson endpoint in backend
This commit is contained in:
@@ -31,8 +31,11 @@ import (
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/apis"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
const RoomOccupancyGranularity = 5
|
||||
|
||||
func AddRoutes(app *pocketbase.PocketBase) {
|
||||
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
@@ -172,6 +175,40 @@ func AddRoutes(app *pocketbase.PocketBase) {
|
||||
return nil
|
||||
})
|
||||
|
||||
// API Endpoint to get room occupancy for a time period for all rooms, when requested as BSON
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
_, err := e.Router.AddRoute(echo.Route{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/schedule/rooms",
|
||||
Handler: func(c echo.Context) error {
|
||||
from := c.QueryParam("from")
|
||||
to := c.QueryParam("to")
|
||||
rooms, err := room.GetRoomOccupancyList(app, from, to, RoomOccupancyGranularity)
|
||||
|
||||
if err != nil {
|
||||
slog.Error("Failed to get room occupancy: %v", err)
|
||||
return c.JSON(http.StatusBadRequest, "Failed to get room occupancy")
|
||||
}
|
||||
|
||||
bson_coded, err := bson.Marshal(rooms)
|
||||
|
||||
if err != nil {
|
||||
slog.Error("Failed to encode room occupancy to BSON: %v", err)
|
||||
return c.JSON(http.StatusBadRequest, "Failed to encode room occupancy to BSON")
|
||||
}
|
||||
|
||||
return c.Blob(http.StatusOK, "application/bson", bson_coded)
|
||||
},
|
||||
Middlewares: []echo.MiddlewareFunc{
|
||||
apis.ActivityLogger(app),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// API Endpoint to create a new iCal feed
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
_, err := e.Router.AddRoute(echo.Route{
|
||||
|
Reference in New Issue
Block a user