Skip to main content

Employee Handler Documentation

This code defines an EmployeeHandler struct that handles various employee-related HTTP requests. It uses the Echo framework for routing and handling HTTP requests and responses.

EmployeeHandler struct

type EmployeeHandler struct {
medahandler.MedaHandler
}
  • medahandler.MedaHandler: Embeds the MedaHandler struct from the medahandler package.

Functions

NewEmployeeHandler

func NewEmployeeHandler() medahandler.MedaHandlerContract

Creates a new EmployeeHandler instance.

BindRouter

func (h *EmployeeHandler) BindRouter(pbx *core.ServeEvent) error

Binds the handler's routes to the Echo router.

GetDetailEmployee

func (h *EmployeeHandler) GetDetailEmployee(c echo.Context) error

Gets the details of an employee by their ID and returns the result as a JSON response.

CreateEmployee

func (h *EmployeeHandler) CreateEmployee(c echo.Context) error

Creates a new employee and returns the result as a JSON response.

UpdateEmployee

func (h *EmployeeHandler) UpdateEmployee(c echo.Context) error

Updates an existing employee and returns the result as a JSON response.

GetExportTemplate

func (h *EmployeeHandler) GetExportTemplate(c echo.Context) error

Generates an Excel template for employee data and returns it as a downloadable file.

autoFitCol

func (h *EmployeeHandler) autoFitCol(f *excelize.File, sheetName string) error

Autofits the columns of an Excel sheet based on their content.

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
employeeHandler := handler.NewEmployeeHandler()

// Bind routes
pb.OnBeforeServe().Add(func(e *core.ServeEvent) error {
return employeeHandler.BindRouter(e)
})

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

Making Requests

Get Detail Employee

curl -X GET http://localhost:8080/api/v1/employee/employee123

Create Employee

curl -X POST http://localhost:8080/api/v1/employee -d '{
"company_id": "company123",
"employee_id": "emp123",
"name": "John Doe",
"position": "Developer",
"department": "IT"
}'

Update Employee

curl -X PUT http://localhost:8080/api/v1/employee/employee123 -d '{
"company_id": "company123",
"employee_id": "emp123",
"name": "John Doe Updated",
"position": "Senior Developer",
"department": "IT"
}'

Get Export Template

curl -X GET http://localhost:8080/api/v1/employee/export/template

This documentation provides an overview of the EmployeeHandler struct, its functions, and example usage. By following the example, you can initialize the handler, bind routes, and make requests to the various endpoints.