Hub-Api/services/plan.go

88 lines
2.6 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
plan_repository "netina/repositories/plan"
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 PlanService struct {
2024-06-07 15:14:01 +03:30
CommandRepo plan_repository.PlanCommandRepository
QueryRepo plan_repository.PlanQueryRepository
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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()})
}
2024-06-02 20:57:12 +03:30
handler := commands.CreatePlanHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
if err := handler.Handle(*plan); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusCreated, plan)
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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"})
}
2024-06-02 21:15:27 +03:30
handler := queries.GetPlanHandler{Repository: s.QueryRepo}
2024-06-07 15:14:01 +03:30
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)
2024-06-02 21:15:27 +03:30
}
2024-06-07 15:14:01 +03:30
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()})
}
2024-06-02 20:57:12 +03:30
handler := commands.UpdatePlanHandler{Repository: s.CommandRepo}
2024-06-07 15:14:01 +03:30
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)
2024-06-02 20:57:12 +03:30
}
2024-06-07 15:14:01 +03:30
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"})
}
2024-06-02 20:57:12 +03:30
handler := commands.RemovePlanHandler{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)
}