215 lines
6.3 KiB
Go
215 lines
6.3 KiB
Go
package reasonscard
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"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 = "/reasons-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.DELETE("/:id", h.delete)
|
|
}
|
|
|
|
// list godoc
|
|
// @Summary List reason cards
|
|
// @Description Returns paginated reason cards with optional filters
|
|
// @Tags reason-card
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param reason_name query string false "Filter by title (en, ru, tm)"
|
|
// @Param page query int false "Page number" default(1)
|
|
// @Param limit query int false "Items per page" default(10)
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /reasons-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 reason cards failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// getByID godoc
|
|
// @Summary Get reason card by ID
|
|
// @Description Returns a single reason card
|
|
// @Tags reason-card
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Reason card ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /reasons-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 reason card failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// create godoc
|
|
// @Summary Create reason card
|
|
// @Description Creates a new reason card
|
|
// @Tags reason-card
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param body body CreateReasonCardDTO true "Reason card payload"
|
|
// @Success 201 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /reasons-cards [post]
|
|
func (h *handler) create(c *gin.Context) {
|
|
var dto CreateReasonCardDTO
|
|
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 reason card failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// update godoc
|
|
// @Summary Update reason card
|
|
// @Description Updates an existing reason card
|
|
// @Tags reason-card
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Reason card ID"
|
|
// @Param body body UpdateReasonCardDTO true "Reason card payload"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /reasons-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 UpdateReasonCardDTO
|
|
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 reason card failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// delete godoc
|
|
// @Summary Delete reason card
|
|
// @Description Deletes a reason card
|
|
// @Tags reason-card
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Reason card ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /reasons-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 reason card failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData("reason 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, "reason_name", "reasonName"); raw != "" {
|
|
filter.ReasonName = raw
|
|
}
|
|
|
|
return filter, true
|
|
}
|