278 lines
7.9 KiB
Go
278 lines
7.9 KiB
Go
package cardorder
|
|
|
|
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 = "/card-orders"
|
|
|
|
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.Require(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 card orders
|
|
// @Description Returns paginated card orders with optional filters
|
|
// @Tags card-orders
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param status query string false "Filter by status"
|
|
// @Param branch_id query int false "Filter by branch ID"
|
|
// @Param user_id query int false "Filter by user ID"
|
|
// @Param card_state_id query int false "Filter by card state ID"
|
|
// @Param card_type_id query int false "Filter by card type ID"
|
|
// @Param region query string false "Filter by region"
|
|
// @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 /card-orders [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 card orders failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// getByID godoc
|
|
// @Summary Get card order by ID
|
|
// @Description Returns a single card order
|
|
// @Tags card-orders
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Card order ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /card-orders/{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 card order failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
if !rbac.GuardOrder(c, item.UserId, item.BranchId) {
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// create godoc
|
|
// @Summary Create card order
|
|
// @Description Creates a new card order
|
|
// @Tags card-orders
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param body body CreateCardOrderDTO true "Card order payload"
|
|
// @Success 201 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /card-orders [post]
|
|
func (h *handler) create(c *gin.Context) {
|
|
var dto CreateCardOrderDTO
|
|
if err := c.ShouldBindBodyWithJSON(&dto); err != nil {
|
|
h.logger.Errorf("create card order bind failed: %v", err)
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
item, err := h.repository.Create(c.Request.Context(), dto)
|
|
if err != nil {
|
|
h.logger.Errorf("create card order failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// update godoc
|
|
// @Summary Update card order
|
|
// @Description Updates an existing card order
|
|
// @Tags card-orders
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Card order ID"
|
|
// @Param body body UpdateCardOrderDTO true "Card order payload"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /card-orders/{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
|
|
}
|
|
|
|
if !h.guardBranchByID(c, id) {
|
|
return
|
|
}
|
|
|
|
var dto UpdateCardOrderDTO
|
|
if err := c.ShouldBindBodyWithJSON(&dto); err != nil {
|
|
h.logger.Errorf("update card order bind failed: %v", err)
|
|
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 card order failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// delete godoc
|
|
// @Summary Delete card order
|
|
// @Description Deletes a card order
|
|
// @Tags card-orders
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Card order ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /card-orders/{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 !h.guardBranchByID(c, id) {
|
|
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 card order failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData("card order 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 id, ok := shared.OptionalQueryID(c, "branch_id", "branchId"); !ok {
|
|
return ListFilter{}, false
|
|
} else if id != nil {
|
|
filter.BranchId = id
|
|
}
|
|
|
|
if id, ok := shared.OptionalQueryID(c, "user_id", "userId"); !ok {
|
|
return ListFilter{}, false
|
|
} else if id != nil {
|
|
filter.UserId = id
|
|
}
|
|
|
|
if id, ok := shared.OptionalQueryID(c, "card_state_id", "cardStateId"); !ok {
|
|
return ListFilter{}, false
|
|
} else if id != nil {
|
|
filter.CardStateId = id
|
|
}
|
|
|
|
if id, ok := shared.OptionalQueryID(c, "card_type_id", "cardTypeId"); !ok {
|
|
return ListFilter{}, false
|
|
} else if id != nil {
|
|
filter.CardTypeId = id
|
|
}
|
|
|
|
if raw := shared.QueryValue(c, "status"); raw != "" {
|
|
filter.Status = &raw
|
|
}
|
|
|
|
if raw := shared.QueryValue(c, "region"); raw != "" {
|
|
filter.Region = raw
|
|
}
|
|
|
|
rbac.BindBranchIDs(c, &filter.BranchIds)
|
|
return filter, true
|
|
}
|
|
|
|
func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
|
if rbac.SkipRecordGuard(c) {
|
|
return true
|
|
}
|
|
item, err := h.repository.GetByID(c.Request.Context(), id)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return false
|
|
}
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return false
|
|
}
|
|
return rbac.GuardOrder(c, item.UserId, item.BranchId)
|
|
}
|