Hub-Api/services/store.go

102 lines
3.1 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
store_repository "netina/repositories/store"
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 StoreService struct {
2024-06-07 15:14:01 +03:30
CommandRepo store_repository.StoreCommandRepository
QueryRepo store_repository.StoreQueryRepository
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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()})
}
2024-06-02 20:57:12 +03:30
handler := commands.CreateStoreHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
if err := handler.Handle(*store); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusCreated, store)
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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"})
}
2024-06-02 20:57:12 +03:30
2024-06-02 21:15:27 +03:30
handler := queries.GetStoreHandler{Repository: s.QueryRepo}
2024-06-07 15:14:01 +03:30
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)
2024-06-02 21:15:27 +03:30
}
2024-06-07 15:14:01 +03:30
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"})
}
2024-06-02 21:15:27 +03:30
handler := queries.GetStoreListHandler{Repository: s.QueryRepo}
2024-06-07 15:14:01 +03:30
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)
2024-06-02 21:15:27 +03:30
}
2024-06-07 15:14:01 +03:30
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()})
}
2024-06-02 21:15:27 +03:30
2024-06-02 20:57:12 +03:30
handler := commands.UpdateStoreHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
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)
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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"})
}
2024-06-02 20:57:12 +03:30
handler := commands.RemoveStoreHandler{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)
}