Shift Handler Documentation
This code defines a ShiftHandler struct that handles various shift-related HTTP requests. It uses the Echo framework for routing and handling HTTP requests and responses.
ShiftHandler struct
type ShiftHandler struct {
medahandler.MedaHandler
}
medahandler.MedaHandler: Embeds theMedaHandlerstruct from themedahandlerpackage.
Functions
NewShiftHandler
func NewShiftHandler() medahandler.MedaHandlerContract
Creates a new ShiftHandler instance.
BindRouter
func (h *ShiftHandler) BindRouter(pbx *core.ServeEvent) error
Binds the handler's routes to the Echo router.
CreateShift
func (h *ShiftHandler) CreateShift(c echo.Context) error
Creates a new shift and returns the result as a JSON response.
UpdateShift
func (h *ShiftHandler) UpdateShift(c echo.Context) error
Updates an existing shift and returns the result as a JSON response.
Example Usage
Initializing the Handler
package main
import (
"fio-backend/handler"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
)
func main() {
// Initialize Echo
e := echo.New()
// Initialize PocketBase
pb := pocketbase.New()
// Initialize the handler
shiftHandler := handler.NewShiftHandler()
// Bind routes
pb.OnBeforeServe().Add(func(e *core.ServeEvent) error {
return shiftHandler.BindRouter(e)
})
// Start the server
e.Logger.Fatal(e.Start(":8080"))
}
Making Requests
Create Shift
curl -X POST http://localhost:8080/api/v1/shift -d '{
"name": "Morning Shift",
"start_time": "08:00",
"end_time": "16:00",
"breaks": [
{
"start_time": "12:00",
"end_time": "12:30"
}
],
"is_flexible": false
}'
Update Shift
curl -X PUT http://localhost:8080/api/v1/shift/{id} -d '{
"name": "Updated Morning Shift",
"start_time": "08:00",
"end_time": "16:00",
"breaks": [
{
"start_time": "12:00",
"end_time": "12:30"
}
],
"is_flexible": false
}'
This documentation provides an overview of the ShiftHandler struct, its functions, and example usage. By following the example, you can initialize the handler, bind routes, and make requests to the various endpoints.