You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB
Go

package coupon
import (
"cls-server/internal/application/coupon"
middleware "cls-server/internal/infrastructure/middleware/auth"
"cls-server/internal/interfaces"
"cls-server/pkg/logger"
"github.com/gin-gonic/gin"
"net/http"
)
// Handler 优惠券 HTTP 处理器
type CouponHandler struct {
couponService *coupon.CouponService
authMiddleware *middleware.AuthMiddleware
log logger.Logger
}
var _ interfaces.Handler = (*CouponHandler)(nil)
// NewHandler 创建优惠券 HTTP 处理器
func NewCouponHandler(couponService *coupon.CouponService, authMiddleware *middleware.AuthMiddleware, log logger.New) *CouponHandler {
return &CouponHandler{
couponService: couponService,
authMiddleware: authMiddleware,
log: log("cls:interfaces:coupon"),
}
}
func (c CouponHandler) RegisterRouters(app gin.IRouter) {
coupon := app.Group("/coupon")
{
coupon.GET("/get", c.GetUserCoupons)
coupon.POST("/create", c.CreateCoupon)
}
}
// CreateCoupon 创建优惠券
func (h *CouponHandler) CreateCoupon(c *gin.Context) {
var req coupon.CouponDto
if err := c.ShouldBindJSON(&req); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if err := h.couponService.CreateCoupon(&req); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.AbortWithStatus(http.StatusOK)
}
}
// GetUserCoupons 获取用户的优惠券列表
func (h *CouponHandler) GetUserCoupons(c *gin.Context) {
ePhone, err := h.authMiddleware.DecodeToken(c)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
coupons, err := h.couponService.GetUserCoupons(ePhone)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
} else {
}
c.JSON(http.StatusOK, coupons)
}