123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
package services
|
|
|
|
import (
|
|
"net/http"
|
|
"netina/commands"
|
|
"netina/database"
|
|
"netina/models"
|
|
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 {
|
|
user := c.Get("user").(*models.JWTClaims)
|
|
if user == nil {
|
|
return echo.ErrUnauthorized
|
|
}
|
|
|
|
u , err := FindUser(user.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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 , u.Name); 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 {
|
|
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 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 , u.Name); 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)
|
|
}
|
|
|
|
|
|
|
|
func FindOwner(id uint) (*models.Owner , error) {
|
|
db := database.Db()
|
|
|
|
var owner models.Owner
|
|
|
|
if err := db.Where("is_removed = ?" , false).Where("owner_id = ?" , id).First(&owner).Error; err != nil {
|
|
return nil , err
|
|
}
|
|
|
|
return &owner , nil
|
|
}
|