Hub-Api/services/owner.go

87 lines
2.6 KiB
Go
Raw Normal View History

2024-06-02 20:57:12 +03:30
package services
import (
2024-06-07 15:14:01 +03:30
"net/http"
2024-06-02 20:57:12 +03:30
"netina/commands"
cm "netina/models/commands"
2024-06-02 21:15:27 +03:30
"netina/queries"
2024-06-02 20:57:12 +03:30
owner_repository "netina/repositories/owner"
2024-06-07 15:14:01 +03:30
"netina/validation"
"strconv"
2024-06-02 20:57:12 +03:30
2024-06-07 15:14:01 +03:30
"github.com/labstack/echo/v4"
)
2024-06-02 20:57:12 +03:30
type OwnerService struct {
2024-06-07 15:14:01 +03:30
CommandRepo owner_repository.OwnerCommandRepository
QueryRepo owner_repository.OwnerQueryRepository
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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()})
}
2024-06-02 20:57:12 +03:30
handler := commands.CreateOwnerHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
if err := handler.Handle(*owner); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusCreated, owner)
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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"})
}
2024-06-02 20:57:12 +03:30
2024-06-02 21:15:27 +03:30
handler := queries.GetOwnerHandler{Repository: s.QueryRepo}
2024-06-07 15:14:01 +03:30
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)
2024-06-02 21:15:27 +03:30
}
2024-06-07 15:14:01 +03:30
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()})
}
2024-06-02 21:15:27 +03:30
2024-06-02 20:57:12 +03:30
handler := commands.UpdateOwnerHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
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)
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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"})
}
2024-06-02 20:57:12 +03:30
handler := commands.RemoveOwnerHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
if err := handler.Handle(uint(id)); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.NoContent(http.StatusNoContent)
}