diff --git a/commands/license_handler.go b/commands/license_handler.go index ba3427d..96553c6 100644 --- a/commands/license_handler.go +++ b/commands/license_handler.go @@ -45,6 +45,7 @@ func (r *UpdateLicenseHandler) Handle (id uint , command c.UpdateLicenseCommand) Plan_id: command.Plan_id, Period: command.Period, Modified_by: command.Modified_by, + Modified_at: time.Now(), } return r.Repository.UpdateLicense(id , license) diff --git a/commands/owner_handler.go b/commands/owner_handler.go index 3987247..5e0c259 100644 --- a/commands/owner_handler.go +++ b/commands/owner_handler.go @@ -47,6 +47,7 @@ func (r *UpdateOwnerHandler) Handle(id uint , command c.UpdateOwnerCommand)(*m.O LastName: command.LastName, NationalCode: command.NationalCode, Modified_by: command.Modified_by, + Modified_at: time.Now(), } return r.Repository.UpdateOwner(id , owner) diff --git a/commands/plan_handler.go b/commands/plan_handler.go index a963f90..bbfc499 100644 --- a/commands/plan_handler.go +++ b/commands/plan_handler.go @@ -49,6 +49,7 @@ func (r *UpdatePlanHandler) Handle (id uint , command c.UpdatePlanCommand)(*m.Pl Partnership: command.Partnership, PercentageOfOwner: command.PercentageOfOwner, Modified_by: command.Modified_by, + Modified_at: time.Now(), } return r.Repository.UpdatePlan(id , plan) diff --git a/commands/store_handler.go b/commands/store_handler.go index f869824..9ed57f3 100644 --- a/commands/store_handler.go +++ b/commands/store_handler.go @@ -51,6 +51,8 @@ func (r *UpdateStoreHandler) Handle (id uint ,command c.UpdateStoreCommand)(*m.S return nil,err } + + store := &m.Store{ Owner_id: command.Owner_id, Name: command.Name, @@ -62,9 +64,12 @@ func (r *UpdateStoreHandler) Handle (id uint ,command c.UpdateStoreCommand)(*m.S StorageAddress: command.StorageAddress, License_id: command.License_id, Modified_by: command.Modified_by, + Modified_at: time.Now(), } + + return r.Repository.UpdateStore(id ,store ) } diff --git a/models/commands/license_commands.go b/models/commands/license_commands.go index db9ce59..ea88ff3 100644 --- a/models/commands/license_commands.go +++ b/models/commands/license_commands.go @@ -3,15 +3,15 @@ package models import "time" type CreateLicenseCommand struct { - Plan_id uint - Period time.Time - Modified_by string + Plan_id uint `validate:"required"` + Period time.Time `validate:"required"` + Modified_by string `validate:"required"` } type UpdateLicenseCommand struct { - Plan_id uint - Period time.Time - Modified_by string + Plan_id uint `validate:"required"` + Period time.Time `validate:"required"` + Modified_by string `validate:"required"` } diff --git a/models/commands/owner_commands.go b/models/commands/owner_commands.go index fcb90fe..898f5af 100644 --- a/models/commands/owner_commands.go +++ b/models/commands/owner_commands.go @@ -1,17 +1,17 @@ package models type CreateOwnerCommand struct { - PhoneNumber string - FirstName string - LastName string - NationalCode string - Modified_by string + PhoneNumber string `validate:"required"` + FirstName string `validate:"required"` + LastName string `validate:"required"` + NationalCode string `validate:"required"` + Modified_by string `validate:"required"` } type UpdateOwnerCommand struct { - PhoneNumber string - FirstName string - LastName string - NationalCode string - Modified_by string + PhoneNumber string `validate:"required"` + FirstName string `validate:"required"` + LastName string `validate:"required"` + NationalCode string `validate:"required"` + Modified_by string `validate:"required"` } diff --git a/models/commands/plan_commands.go b/models/commands/plan_commands.go index d36b520..da81b70 100644 --- a/models/commands/plan_commands.go +++ b/models/commands/plan_commands.go @@ -3,19 +3,19 @@ package models import "time" type CreatePlanCommand struct { - Price uint - Priod time.Time - Partnership bool - PercentageOfOwner uint - Modified_by string + Price uint `validate:"required"` + Priod time.Time `validate:"required"` + Partnership bool `validate:"required"` + PercentageOfOwner uint `validate:"required"` + Modified_by string `validate:"required"` } type UpdatePlanCommand struct { - Price uint - Priod time.Time - Partnership bool - PercentageOfOwner uint - Modified_by string + Price uint `validate:"required"` + Priod time.Time `validate:"required"` + Partnership bool `validate:"required"` + PercentageOfOwner uint `validate:"required"` + Modified_by string `validate:"required"` } \ No newline at end of file diff --git a/models/commands/store_commands.go b/models/commands/store_commands.go index 562d545..ee98430 100644 --- a/models/commands/store_commands.go +++ b/models/commands/store_commands.go @@ -1,27 +1,27 @@ package models type CreateStoreCommand struct { - Owner_id uint - Name string - Address string - PhoneNumber string - WebAddress string - ApiAddress string - StorageAddress string - AdminPanelAddress string - License_id uint - Modified_by string + Owner_id uint `validate:"required"` + Name string `validate:"required"` + Address string `validate:"required"` + PhoneNumber string `validate:"required"` + WebAddress string `validate:"required"` + ApiAddress string `validate:"required"` + StorageAddress string `validate:"required"` + AdminPanelAddress string `validate:"required"` + License_id uint `validate:"required"` + Modified_by string `validate:"required"` } type UpdateStoreCommand struct { - Owner_id uint - Name string - Address string - PhoneNumber string - WebAddress string - ApiAddress string - StorageAddress string - AdminPanelAddress string - License_id uint - Modified_by string + Owner_id uint `validate:"required"` + Name string `validate:"required"` + Address string `validate:"required"` + PhoneNumber string `validate:"required"` + WebAddress string `validate:"required"` + ApiAddress string `validate:"required"` + StorageAddress string `validate:"required"` + AdminPanelAddress string `validate:"required"` + License_id uint `validate:"required"` + Modified_by string `validate:"required"` } \ No newline at end of file diff --git a/services/license.go b/services/license.go index 77ae063..80a965f 100644 --- a/services/license.go +++ b/services/license.go @@ -1,38 +1,87 @@ package services import ( + "net/http" "netina/commands" - models "netina/models" 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 + 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()}) + } -func(s *LicenseService) CreateLicnese(cmd cm.CreateLicenseCommand) error { handler := commands.CreateLicenseHandler{Repository: s.CommandRepo} - return handler.Handle(cmd) + 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(id uint)(*models.License , error){ +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} - return handler.Handle(id) + 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()}) + } -func(s *LicenseService) UpdateOwner(id uint,cmd cm.UpdateLicenseCommand)(*models.License ,error) { handler := commands.UpdateLicenseHandler{Repository: s.CommandRepo} - return handler.Handle(id,cmd) + 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) RemoveOwner(id uint) error { +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} - return handler.Handle(id) -} \ No newline at end of file + if err := handler.Handle(uint(id)); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.NoContent(http.StatusNoContent) +} diff --git a/services/owner.go b/services/owner.go index 7cdd4e1..c057952 100644 --- a/services/owner.go +++ b/services/owner.go @@ -1,39 +1,86 @@ package services import ( + "net/http" "netina/commands" - models "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 + CommandRepo owner_repository.OwnerCommandRepository + QueryRepo owner_repository.OwnerQueryRepository } +func (s *OwnerService) CreateOwner(c echo.Context) error { + 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()}) + } -func(s *OwnerService) CreateOwner(cmd cm.CreateOwnerCommand) error { handler := commands.CreateOwnerHandler{Repository: s.CommandRepo} - return handler.Handle(cmd) + if err := handler.Handle(*owner); 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"}) + } -func(s *OwnerService) GetOwner(id uint)(*models.Owner , error){ handler := queries.GetOwnerHandler{Repository: s.QueryRepo} - return handler.Handle(id) + 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 { + 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()}) + } -func(s *OwnerService) UpdateOwner(id uint,cmd cm.UpdateOwnerCommand)(*models.Owner ,error) { handler := commands.UpdateOwnerHandler{Repository: s.CommandRepo} - return handler.Handle(id,cmd) + if _, err := handler.Handle(uint(id), *owner); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.NoContent(http.StatusNoContent) } -func(s *OwnerService) RemoveOwner(id uint) error { +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} - return handler.Handle(id) -} \ No newline at end of file + if err := handler.Handle(uint(id)); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.NoContent(http.StatusNoContent) +} diff --git a/services/plan.go b/services/plan.go index ea87532..45b5b97 100644 --- a/services/plan.go +++ b/services/plan.go @@ -1,38 +1,87 @@ package services import ( + "net/http" "netina/commands" - models "netina/models" cm "netina/models/commands" "netina/queries" plan_repository "netina/repositories/plan" + "netina/validation" + "strconv" + + "github.com/labstack/echo/v4" ) - - type PlanService struct { - CommandRepo plan_repository.PlanCommandRepository - QueryRepo plan_repository.PlanQueryRepository + CommandRepo plan_repository.PlanCommandRepository + QueryRepo plan_repository.PlanQueryRepository } +func (s *PlanService) CreatePlan(c echo.Context) error { + plan := new(cm.CreatePlanCommand) + if err := c.Bind(plan); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) + } + if err := validation.ValidateStruct(plan); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } -func(s *PlanService) Createplan(cmd cm.CreatePlanCommand) error { handler := commands.CreatePlanHandler{Repository: s.CommandRepo} - return handler.Handle(cmd) + if err := handler.Handle(*plan); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusCreated, plan) } -func(s *PlanService) GetPlan(id uint)(*models.Plan , error){ +func (s *PlanService) GetPlan(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid plan ID"}) + } + handler := queries.GetPlanHandler{Repository: s.QueryRepo} - return handler.Handle(id) + plan, err := handler.Handle(uint(id)) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusOK, plan) } +func (s *PlanService) UpdatePlan(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid plan ID"}) + } + + plan := new(cm.UpdatePlanCommand) + if err := c.Bind(plan); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) + } + if err := validation.ValidateStruct(plan); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } -func(s *PlanService) Updateplan(id uint,cmd cm.UpdatePlanCommand)(*models.Plan ,error) { handler := commands.UpdatePlanHandler{Repository: s.CommandRepo} - return handler.Handle(id,cmd) + updatedPlan, err := handler.Handle(uint(id), *plan) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusOK, updatedPlan) } -func(s *PlanService) Removeplan(id uint) error { +func (s *PlanService) RemovePlan(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid plan ID"}) + } + handler := commands.RemovePlanHandler{Repository: s.CommandRepo} - return handler.Handle(id) -} \ No newline at end of file + if err := handler.Handle(uint(id)); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.NoContent(http.StatusNoContent) +} diff --git a/services/store.go b/services/store.go index 9b23d4a..cdddf06 100644 --- a/services/store.go +++ b/services/store.go @@ -1,44 +1,101 @@ package services import ( + "net/http" "netina/commands" - models "netina/models" cm "netina/models/commands" "netina/queries" store_repository "netina/repositories/store" + "netina/validation" + "strconv" + + "github.com/labstack/echo/v4" ) - - type StoreService struct { - CommandRepo store_repository.StoreCommandRepository - QueryRepo store_repository.StoreQueryRepository + CommandRepo store_repository.StoreCommandRepository + QueryRepo store_repository.StoreQueryRepository } +func (s *StoreService) CreateStore(c echo.Context) error { + store := new(cm.CreateStoreCommand) + if err := c.Bind(store); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) + } + if err := validation.ValidateStruct(store); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } -func(s *StoreService) Createstore(cmd cm.CreateStoreCommand) error { handler := commands.CreateStoreHandler{Repository: s.CommandRepo} - return handler.Handle(cmd) + if err := handler.Handle(*store); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusCreated, store) } +func (s *StoreService) GetStore(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid store ID"}) + } -func(s *StoreService) GetStore(id uint)(*models.Store , error){ handler := queries.GetStoreHandler{Repository: s.QueryRepo} - return handler.Handle(id) + store, err := handler.Handle(uint(id)) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusOK, store) } -func(s *StoreService) GetStoresList(id uint)([]models.Store , error){ +func (s *StoreService) GetStoresList(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid store ID"}) + } handler := queries.GetStoreListHandler{Repository: s.QueryRepo} - return handler.Handle(id) + stores, err := handler.Handle(uint(id)) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusOK, stores) } +func (s *StoreService) UpdateStore(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid store ID"}) + } + + store := new(cm.UpdateStoreCommand) + if err := c.Bind(store); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) + } + if err := validation.ValidateStruct(store); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } -func(s *StoreService) Updatestore(id uint,cmd cm.UpdateStoreCommand)(*models.Store ,error) { handler := commands.UpdateStoreHandler{Repository: s.CommandRepo} - return handler.Handle(id,cmd) + updatedStore, err := handler.Handle(uint(id), *store) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusOK, updatedStore) } -func(s *StoreService) Removestore(id uint) error { +func (s *StoreService) RemoveStore(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid store ID"}) + } + handler := commands.RemoveStoreHandler{Repository: s.CommandRepo} - return handler.Handle(id) -} \ No newline at end of file + if err := handler.Handle(uint(id)); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.NoContent(http.StatusNoContent) +}