package services import ( "net/http" "netina/commands" "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 } func (s *StoreService) CreateStore(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 } 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()}) } _ , err = FindOwner(store.Owner_id) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid owner ID"}) } handler := commands.CreateStoreHandler{Repository: s.CommandRepo} if err := handler.Handle(*store , u.Name); 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"}) } handler := queries.GetStoreHandler{Repository: s.QueryRepo} 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(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} 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 { 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 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()}) } _ , err = FindOwner(store.Owner_id) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid owner ID"}) } handler := commands.UpdateStoreHandler{Repository: s.CommandRepo} updatedStore, err := handler.Handle(uint(id), *store , u.Name) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } return c.JSON(http.StatusOK, updatedStore) } 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} if err := handler.Handle(uint(id)); err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } return c.NoContent(http.StatusNoContent) }