Skip to main content

General Handler Utilities Documentation

This code provides utility functions for handling common tasks such as binding and validating request payloads, finalizing calendar data, and retrieving user events by date range. These utilities are designed to be used within the context of an Echo web server and PocketBase DAO.

Functions

ValidatableWithDao Interface

type ValidatableWithDao interface {
Validate(dao *daos.Dao) error
}

An interface that requires a Validate method which takes a *daos.Dao as a parameter. This is used for payloads that need to be validated with access to the DAO.

bindAndValidate

func bindAndValidate(c echo.Context, payload interface{}) error

Binds and validates a request payload.

  • c: The Echo context.
  • payload: The payload to bind and validate.

bindAndValidateDao

func bindAndValidateDao(c echo.Context, dao *daos.Dao, payload interface{}) error

Binds and validates a request payload with access to the DAO.

  • c: The Echo context.
  • dao: The PocketBase DAO.
  • payload: The payload to bind and validate.

finalizeGetCalendar

func finalizeGetCalendar(dao *daos.Dao, ref *entity.GetCalendarDepartmentAndEmployeeResponse, schedules []*entity.ScheduleTable, month, year int) error

Finalizes the calendar data for a given month and year.

  • dao: The PocketBase DAO.
  • ref: The reference to the calendar response.
  • schedules: The list of schedules.
  • month: The month for which to finalize the calendar.
  • year: The year for which to finalize the calendar.

getUserEventsByDateRange

func getUserEventsByDateRange(dao *daos.Dao, u *entity.UserProfileTable, startDate time.Time, endDate time.Time, merge ...entity.CalendarEventTable) (map[string][]entity.CalendarEventTable, error)

Retrieves user events by date range and merges them with additional events.

  • dao: The PocketBase DAO.
  • u: The user profile.
  • startDate: The start date of the range.
  • endDate: The end date of the range.
  • merge: Additional events to merge with the user events.

Example Usage

Binding and Validating a Payload

package main

import (
"fio-backend/handler"
"fio-backend/entity"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
)

func main() {
// Initialize Echo
e := echo.New()

// Initialize PocketBase
pb := pocketbase.New()

// Example route
e.POST("/example", func(c echo.Context) error {
payload := &entity.ExampleRequest{}
dao := daos.New(pb.DB())

if err := handler.bindAndValidateDao(c, dao, payload); err != nil {
return c.JSON(400, map[string]string{"error": err.Error()})
}

// Process the payload
return c.JSON(200, payload)
})

// Start the server
e.Logger.Fatal(e.Start(":8080"))
}

Finalizing Calendar Data

package main

import (
"fio-backend/handler"
"fio-backend/entity"
"fio-backend/queries"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"time"
)

func main() {
// Initialize Echo
e := echo.New()

// Initialize PocketBase
pb := pocketbase.New()

// Example route
e.GET("/calendar", func(c echo.Context) error {
dao := daos.New(pb.DB())
schedules, _ := queries.GetAllSchedules(dao)
ref := &entity.GetCalendarDepartmentAndEmployeeResponse{
Calendar: make(map[string]*entity.GetCalendarResponse),
}

month := 1
year := 2023

if err := handler.finalizeGetCalendar(dao, ref, schedules, month, year); err != nil {
return c.JSON(500, map[string]string{"error": err.Error()})
}

return c.JSON(200, ref)
})

// Start the server
e.Logger.Fatal(e.Start(":8080"))
}

Retrieving User Events by Date Range

package main

import (
"fio-backend/handler"
"fio-backend/entity"
"fio-backend/queries"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"time"
)

func main() {
// Initialize Echo
e := echo.New()

// Initialize PocketBase
pb := pocketbase.New()

// Example route
e.GET("/user-events", func(c echo.Context) error {
dao := daos.New(pb.DB())
user, _ := queries.GetUserById(dao, "user123")
startDate := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
endDate := time.Date(2023, 1, 31, 23, 59, 59, 0, time.UTC)

events, err := handler.getUserEventsByDateRange(dao, user, startDate, endDate)
if err != nil {
return c.JSON(500, map[string]string{"error": err.Error()})
}

return c.JSON(200, events)
})

// Start the server
e.Logger.Fatal(e.Start(":8080"))
}

This documentation provides an overview of the general handler utilities, their functions, and example usage. By following the examples, you can bind and validate request payloads, finalize calendar data, and retrieve user events by date range.