package services import ( "net/http" "netina/commands" "netina/models" cm "netina/models/commands" "netina/queries" license_repository "netina/repositories/license" "netina/validation" "strconv" "time" "github.com/labstack/echo/v4" ) type LicenseService struct { CommandRepo license_repository.LicenseCommandRepository QueryRepo license_repository.LicenseQueryRepository } func (s *LicenseService) CreateLicense(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 } 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()}) } plan , err := FindPlan(license.Plan_id) if err != nil { return err } period := plan.Period // convert nano seconds to days expireDate := time.Now().Add((time.Duration(period)*1000000000*3600*24)) license.ExpireDate = expireDate handler := commands.CreateLicenseHandler{Repository: s.CommandRepo} if err := handler.Handle(*license , u.Name); 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 { 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 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"}) } handle := queries.GetLicenseHandler{Repository: s.QueryRepo} ls, err := handle.Handle(uint(id)) if err != nil { return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()}) } numberOfRenewals := ls.NumberOfRenewals + 1 license.NumberOfRenewals = numberOfRenewals plan , err := FindPlan(license.Plan_id) if err != nil { return err } period := plan.Period // convert nano seconds to days expireDate := ls.ExpireDate.Add((time.Duration(period)*1000000000*3600*24)) license.ExpireDate = expireDate 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 , u.Name) 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) }