Hub-Api/services/license.go

88 lines
2.7 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
license_repository "netina/repositories/license"
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 LicenseService struct {
2024-06-07 15:14:01 +03:30
CommandRepo license_repository.LicenseCommandRepository
QueryRepo license_repository.LicenseQueryRepository
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
func (s *LicenseService) CreateLicense(c echo.Context) error {
license := new(cm.CreateLicenseCommand)
if err := c.Bind(license); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
}
if err := validation.ValidateStruct(license); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}
2024-06-02 20:57:12 +03:30
handler := commands.CreateLicenseHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
if err := handler.Handle(*license); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusCreated, license)
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
func (s *LicenseService) GetLicense(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid license ID"})
}
2024-06-02 21:15:27 +03:30
handler := queries.GetLicenseHandler{Repository: s.QueryRepo}
2024-06-07 15:14:01 +03:30
license, err := handler.Handle(uint(id))
if err != nil {
return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusOK, license)
2024-06-02 21:15:27 +03:30
}
2024-06-07 15:14:01 +03:30
func (s *LicenseService) UpdateLicense(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid license ID"})
}
license := new(cm.UpdateLicenseCommand)
if err := c.Bind(license); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
}
if err := validation.ValidateStruct(license); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}
2024-06-02 20:57:12 +03:30
handler := commands.UpdateLicenseHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
updatedLicense, err := handler.Handle(uint(id), *license)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusOK, updatedLicense)
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
func (s *LicenseService) RemoveLicense(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid license ID"})
}
2024-06-02 20:57:12 +03:30
handler := commands.RemoveLicenseHandler{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)
}