102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package services
|
|
|
|
import (
|
|
"net/http"
|
|
"netina/commands"
|
|
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 {
|
|
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()})
|
|
}
|
|
|
|
handler := commands.CreateStoreHandler{Repository: s.CommandRepo}
|
|
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"})
|
|
}
|
|
|
|
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 {
|
|
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()})
|
|
}
|
|
|
|
handler := commands.UpdateStoreHandler{Repository: s.CommandRepo}
|
|
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(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)
|
|
}
|