package controller import ( "database/sql" "net/http" "strconv" "github.com/labstack/echo/v4" "epic-ent/internal/domain/dto" "epic-ent/internal/domain/vo" "epic-ent/internal/service" ) type HeroController struct { svc *service.HeroService } func NewHeroController(svc *service.HeroService) *HeroController { return &HeroController{svc: svc} } func (h *HeroController) Create(c echo.Context) error { var req dto.HeroCreateRequest if err := c.Bind(&req); err != nil { return echo.NewHTTPError(http.StatusBadRequest, "invalid request") } hero, err := h.svc.Create(c.Request().Context(), req) if err != nil { return err } return c.JSON(http.StatusCreated, vo.OK(hero)) } func (h *HeroController) GetByID(c echo.Context) error { id, err := parseID(c.Param("id")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "invalid id") } hero, err := h.svc.GetByID(c.Request().Context(), id) if err != nil { if err == sql.ErrNoRows { return echo.NewHTTPError(http.StatusNotFound, "hero not found") } return err } return c.JSON(http.StatusOK, vo.OK(hero)) } func (h *HeroController) Update(c echo.Context) error { id, err := parseID(c.Param("id")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "invalid id") } var req dto.HeroUpdateRequest if err := c.Bind(&req); err != nil { return echo.NewHTTPError(http.StatusBadRequest, "invalid request") } hero, err := h.svc.Update(c.Request().Context(), id, req) if err != nil { if err == sql.ErrNoRows { return echo.NewHTTPError(http.StatusNotFound, "hero not found") } return err } return c.JSON(http.StatusOK, vo.OK(hero)) } func (h *HeroController) Delete(c echo.Context) error { id, err := parseID(c.Param("id")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "invalid id") } if err := h.svc.Delete(c.Request().Context(), id); err != nil { if err == sql.ErrNoRows { return echo.NewHTTPError(http.StatusNotFound, "hero not found") } return err } return c.JSON(http.StatusOK, vo.OK(nil)) } func parseID(raw string) (int64, error) { return strconv.ParseInt(raw, 10, 64) }