package paymentcard import ( "errors" "net/http" "strconv" "online-back/internal/appresult" "online-back/internal/handlers" "online-back/internal/identity/rbac" "online-back/internal/identity/rbac/perm" "online-back/internal/shared" "online-back/pkg/logging" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v4" ) const baseURL = "/payment-cards" type handler struct { logger *logging.Logger repository Repository } func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler { return &handler{logger: logger, repository: repository} } func (h *handler) Register(router *gin.RouterGroup) { group := rbac.Group(router, baseURL, rbac.Catalog(perm.Cards)) group.GET("", h.list) group.GET("/:id", h.getByID) group.POST("", h.create) group.PUT("/:id", h.update) group.PUT("/:id/active", h.updateActive) group.DELETE("/:id", h.delete) } // list godoc // @Summary List payment cards // @Description Returns paginated payment cards with optional filters // @Tags payment-card // @Produce json // @Security BearerAuth // @Param card_name query string false "Filter by card title (en, ru, tm)" // @Param active query bool false "Filter by active" // @Param page query int false "Page number" default(1) // @Param limit query int false "Items per page" default(10) // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-cards [get] func (h *handler) list(c *gin.Context) { filter, ok := parseListFilter(c) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } result, err := h.repository.List(c.Request.Context(), filter) if err != nil { h.logger.Errorf("list payment cards failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(result)) } // getByID godoc // @Summary Get payment card by ID // @Description Returns a single payment card // @Tags payment-card // @Produce json // @Security BearerAuth // @Param id path int true "Payment card ID" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-cards/{id} [get] func (h *handler) getByID(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.GetByID(c.Request.Context(), id) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("get payment card failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // create godoc // @Summary Create payment card // @Description Creates a new payment card // @Tags payment-card // @Accept json // @Produce json // @Security BearerAuth // @Param body body CreatePaymentCardDTO true "Payment card payload" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 201 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-cards [post] func (h *handler) create(c *gin.Context) { var dto CreatePaymentCardDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.Create(c.Request.Context(), dto) if err != nil { h.logger.Errorf("create payment card failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusCreated, appresult.SuccessWithData(item)) } // update godoc // @Summary Update payment card // @Description Updates an existing payment card // @Tags payment-card // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "Payment card ID" // @Param body body UpdatePaymentCardDTO true "Payment card payload" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-cards/{id} [put] func (h *handler) update(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } var dto UpdatePaymentCardDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.Update(c.Request.Context(), id, dto) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("update payment card failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // updateActive godoc // @Summary Update payment card active // @Description Enables or disables a payment card // @Tags payment-card // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "Payment card ID" // @Param body body UpdatePaymentCardActiveDTO true "Active payload" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-cards/{id}/active [put] func (h *handler) updateActive(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } var dto UpdatePaymentCardActiveDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.UpdateActive(c.Request.Context(), id, dto.Active) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("update payment card active failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // delete godoc // @Summary Delete payment card // @Description Deletes a payment card // @Tags payment-card // @Produce json // @Security BearerAuth // @Param id path int true "Payment card ID" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /payment-cards/{id} [delete] func (h *handler) delete(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } else if err != nil { h.logger.Errorf("delete payment card failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData("payment card removed")) } func parseListFilter(c *gin.Context) (ListFilter, bool) { page, limit, ok := shared.ParsePageLimit(c) if !ok { return ListFilter{}, false } filter := ListFilter{Page: page, Limit: limit} if raw := shared.QueryValue(c, "card_name", "cardName"); raw != "" { filter.CardName = raw } if raw := shared.QueryValue(c, "active"); raw != "" { active, err := strconv.ParseBool(raw) if err != nil { return ListFilter{}, false } filter.Active = &active } return filter, true }