feat:#150 extracted functions rewrote error pipe for feed routes

This commit is contained in:
Elmar Kresse
2024-01-22 15:04:50 +01:00
parent 70170054e8
commit 1582154d5f
5 changed files with 234 additions and 139 deletions

View File

@@ -1,45 +1,36 @@
package room
import (
"github.com/pocketbase/pocketbase"
"htwkalender/model"
"htwkalender/service/db"
"log/slog"
"net/http"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
)
func GetRooms(c echo.Context, app *pocketbase.PocketBase) error {
func GetRooms(app *pocketbase.PocketBase) ([]string, error) {
rooms, err := db.GetRooms(app)
if err != nil {
slog.Error("Error getting rooms: ", err)
return c.JSON(http.StatusNotFound, "No rooms found")
return nil, err
} else {
return c.JSON(http.StatusOK, rooms)
return rooms, nil
}
}
func GetRoomScheduleForDay(c echo.Context, app *pocketbase.PocketBase, room string, date string) error {
events, err := db.GetRoomScheduleForDay(app, room, date)
func GetRoomScheduleForDay(app *pocketbase.PocketBase, room string, date string) ([]model.AnonymizedEventDTO, error) {
roomSchedule, err := db.GetRoomScheduleForDay(app, room, date)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
} else {
return c.JSON(http.StatusOK, anonymizeRooms(events))
return nil, err
}
anonymizedRoomSchedule := anonymizeRooms(roomSchedule)
return anonymizedRoomSchedule, nil
}
func GetRoomSchedule(c echo.Context, app *pocketbase.PocketBase, room string, from string, to string) error {
events, err := db.GetRoomSchedule(app, room, from, to)
func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to string) ([]model.AnonymizedEventDTO, error) {
roomSchedule, err := db.GetRoomSchedule(app, room, from, to)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
} else {
return c.JSON(http.StatusOK, anonymizeRooms(events))
return nil, err
}
anonymizedRoomSchedule := anonymizeRooms(roomSchedule)
return anonymizedRoomSchedule, nil
}
// Transform the events to anonymized events throwing away all unnecessary information