From d70d2b1621e72e3dd98d79fe366e7e676e35203e Mon Sep 17 00:00:00 2001 From: nima Date: Mon, 17 Jun 2024 19:04:55 +0330 Subject: [PATCH] role service --- services/role.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 services/role.go diff --git a/services/role.go b/services/role.go new file mode 100644 index 0000000..3ce6c30 --- /dev/null +++ b/services/role.go @@ -0,0 +1,87 @@ +package services + +import ( + "net/http" + "netina/commands" + 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 { + 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); 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 { + 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) + 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) +}