package services import ( "net/http" "netina/commands" cm "netina/models/commands" "netina/queries" owner_repository "netina/repositories/owner" "netina/validation" "strconv" "github.com/labstack/echo/v4" ) type OwnerService struct { CommandRepo owner_repository.OwnerCommandRepository QueryRepo owner_repository.OwnerQueryRepository } func (s *OwnerService) CreateOwner(c echo.Context) error { owner := new(cm.CreateOwnerCommand) if err := c.Bind(owner); err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) } if err := validation.ValidateStruct(owner); err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) } handler := commands.CreateOwnerHandler{Repository: s.CommandRepo} if err := handler.Handle(*owner); err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } return c.JSON(http.StatusCreated, owner) } func (s *OwnerService) GetOwner(c echo.Context) error { id, err := strconv.Atoi(c.Param("id")) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid owner ID"}) } handler := queries.GetOwnerHandler{Repository: s.QueryRepo} owner, err := handler.Handle(uint(id)) if err != nil { return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()}) } return c.JSON(http.StatusOK, owner) } func (s *OwnerService) UpdateOwner(c echo.Context) error { id, err := strconv.Atoi(c.Param("id")) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid owner ID"}) } owner := new(cm.UpdateOwnerCommand) if err := c.Bind(owner); err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) } if err := validation.ValidateStruct(owner); err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) } handler := commands.UpdateOwnerHandler{Repository: s.CommandRepo} if _, err := handler.Handle(uint(id), *owner); err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } return c.NoContent(http.StatusNoContent) } func (s *OwnerService) RemoveOwner(c echo.Context) error { id, err := strconv.Atoi(c.Param("id")) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid owner ID"}) } handler := commands.RemoveOwnerHandler{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) }