forked from ebhomengo/niki
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"git.gocasts.ir/ebhomengo/niki/domain/authorization/service"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
)
|
|
|
|
type RoleHandler struct {
|
|
service service.Authorization
|
|
}
|
|
|
|
func NewRoleHandler(service service.Authorization) RoleHandler {
|
|
return RoleHandler{service: service}
|
|
}
|
|
|
|
func (r RoleHandler) Store(c echo.Context) error {
|
|
var request service.StoreRoleRequest
|
|
if err := c.Bind(&request); err != nil {
|
|
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
|
|
}
|
|
|
|
role, err := r.service.Store(c.Request().Context(), request)
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, echo.Map{"message": fmt.Sprintf("role %s created successfully", role.Title_fa)})
|
|
}
|
|
|
|
func (r RoleHandler) Update(c echo.Context) error {
|
|
var request service.UpdateRoleRequest
|
|
if err := c.Bind(&request); err != nil {
|
|
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
|
|
}
|
|
|
|
role, err := r.service.Update(c.Request().Context(), request)
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, echo.Map{"message": fmt.Sprintf("role %s updated successfully", role.Title_fa)})
|
|
}
|
|
|
|
func (r RoleHandler) AssignRoleToStaff(c echo.Context) error {
|
|
var request service.AssignRoleToStaffRequest
|
|
|
|
if err := c.Bind(&request); err != nil {
|
|
return c.JSON(http.StatusBadRequest, echo.Map{"error": "Invalid request body", "details": err.Error()})
|
|
}
|
|
|
|
err := r.service.AssignRoleToStaff(c.Request().Context(), request)
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, echo.Map{"error": "Failed to assign roles to staff", "details": err.Error()})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, echo.Map{"message": fmt.Sprintf("Roles assigned to staff ID %d successfully", request.StaffID)})
|
|
} |