package services import ( "net/http" "netina/commands" cm "netina/models/commands" "netina/queries" license_repository "netina/repositories/license" "netina/validation" "strconv" "github.com/labstack/echo/v4" ) type LicenseService struct { CommandRepo license_repository.LicenseCommandRepository QueryRepo license_repository.LicenseQueryRepository } 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()}) } handler := commands.CreateLicenseHandler{Repository: s.CommandRepo} if err := handler.Handle(*license); err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } return c.JSON(http.StatusCreated, license) } 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"}) } handler := queries.GetLicenseHandler{Repository: s.QueryRepo} 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) } 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()}) } handler := commands.UpdateLicenseHandler{Repository: s.CommandRepo} 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) } 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"}) } handler := commands.RemoveLicenseHandler{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) }