Hub-Api/services/owner.go

123 lines
3.2 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"
"netina/database"
"netina/models"
2024-06-02 20:57:12 +03:30
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 {
user := c.Get("user").(*models.JWTClaims)
if user == nil {
return echo.ErrUnauthorized
}
u , err := FindUser(user.ID)
if err != nil {
return err
}
2024-06-07 15:14:01 +03:30
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}
if err := handler.Handle(*owner , u.Name); err != nil {
2024-06-07 15:14:01 +03:30
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 {
user := c.Get("user").(*models.JWTClaims)
if user == nil {
return echo.ErrUnauthorized
}
u , err := FindUser(user.ID)
if err != nil {
return err
}
2024-06-07 15:14:01 +03:30
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}
if _, err := handler.Handle(uint(id), *owner , u.Name); err != nil {
2024-06-07 15:14:01 +03:30
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)
}
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
}