109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
package services
|
|
|
|
import (
|
|
"net/http"
|
|
"netina/commands"
|
|
"netina/models"
|
|
cm "netina/models/commands"
|
|
"netina/queries"
|
|
Role_repository "netina/repositories/role"
|
|
"netina/validation"
|
|
"strconv"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type RoleService struct {
|
|
CommandRepo Role_repository.RoleCommandRepository
|
|
QueryRepo Role_repository.RoleQueryRepository
|
|
}
|
|
|
|
func (s *RoleService) CreateRole(c echo.Context) error {
|
|
user := c.Get("user").(*models.JWTClaims)
|
|
if user == nil {
|
|
return echo.ErrUnauthorized
|
|
}
|
|
|
|
u , err := FindUser(user.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
role := new(cm.CreateRoleCommand)
|
|
if err := c.Bind(role); err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
|
|
}
|
|
if err := validation.ValidateStruct(role); err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
}
|
|
|
|
handler := commands.CreateRoleHandler{Repository: s.CommandRepo}
|
|
if err := handler.Handle(*role , u.Name); err != nil {
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
}
|
|
|
|
return c.JSON(http.StatusCreated, role)
|
|
}
|
|
|
|
func (s *RoleService) GetRole(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid role ID"})
|
|
}
|
|
|
|
handler := queries.GetRoleHandler{Repository: s.QueryRepo}
|
|
role, err := handler.Handle(uint(id))
|
|
if err != nil {
|
|
return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, role)
|
|
}
|
|
|
|
func (s *RoleService) UpdateRole(c echo.Context) error {
|
|
user := c.Get("user").(*models.JWTClaims)
|
|
if user == nil {
|
|
return echo.ErrUnauthorized
|
|
}
|
|
|
|
u , err := FindUser(user.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid role ID"})
|
|
}
|
|
|
|
role := new(cm.UpdateRoleCommand)
|
|
if err := c.Bind(role); err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
|
|
}
|
|
if err := validation.ValidateStruct(role); err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
}
|
|
|
|
handler := commands.UpdateRoleHandler{Repository: s.CommandRepo}
|
|
updatedRole, err := handler.Handle(uint(id), *role , u.Name)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, updatedRole)
|
|
}
|
|
|
|
func (s *RoleService) RemoveRole(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid Role ID"})
|
|
}
|
|
|
|
handler := commands.RemoveRoleHandler{Repository: s.CommandRepo}
|
|
if err := handler.Handle(uint(id)); err != nil {
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
}
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
}
|