diff --git a/api/v1/coupon/coupon.go b/api/v1/coupon/coupon.go index 2ad07f3..9530ce3 100644 --- a/api/v1/coupon/coupon.go +++ b/api/v1/coupon/coupon.go @@ -1,5 +1,7 @@ package coupon +import "github.com/gogf/gf/v2/os/gtime" + type GetCouponListReq struct { PageNo int `v:"required#页码不能为空" dc:"页码"` PageSize int `v:"required#页面大小不能为空" dc:"页面大小"` @@ -12,3 +14,10 @@ type GetCouponReq struct { type DeleteCouponReq struct { Id int `v:"required#优惠券id不能为空" dc:"优惠券id"` } + +type InsertCouponReq struct { + Title string `v:"required#优惠券名字不能为空" dc:"优惠券名字"` + Image string `v:"required#优惠券图片不能为空" dc:"优惠券图片"` + StartTime *gtime.Time `v:"required#优惠券生效时间不能为空" dc:"生效时间"` + EndTime *gtime.Time `v:"required#优惠券失效时间不能为空" dc:"失效时间"` +} diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 2a0b422..6472e48 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -20,6 +20,7 @@ var ( group.POST("/get-coupon-list", coupon.Coupon().GetCouponList) group.POST("/get-coupon", coupon.Coupon().GetCoupon) group.POST("/delete-coupon", coupon.Coupon().DeleteCoupon) + group.POST("/insert-coupon", coupon.Coupon().InsertCoupon) }) s.Run() return nil diff --git a/internal/controller/coupon/coupon.go b/internal/controller/coupon/coupon.go index af7188a..8c544b4 100644 --- a/internal/controller/coupon/coupon.go +++ b/internal/controller/coupon/coupon.go @@ -82,3 +82,24 @@ func (c cCoupon) DeleteCoupon(r *ghttp.Request) { Message: "success", }) } + +func (c cCoupon) InsertCoupon(r *ghttp.Request) { + var req *coupon.InsertCouponReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + err := service.Coupon().InsertCoupon(r.Context(), req) + if err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + r.Response.WriteJsonExit(dto.Result{ + Code: 200, + Message: "success", + }) +} diff --git a/internal/logic/coupon/coupon.go b/internal/logic/coupon/coupon.go index 81807e2..0c480b0 100644 --- a/internal/logic/coupon/coupon.go +++ b/internal/logic/coupon/coupon.go @@ -1,6 +1,7 @@ package coupon import ( + "CouponBackendGo/api/v1/coupon" "CouponBackendGo/internal/dao" "CouponBackendGo/internal/model/do" "CouponBackendGo/internal/model/dto" @@ -112,6 +113,9 @@ func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ Deleted: 1, }).WherePri(id).Update() + if err != nil { + return err + } // 软删除数据后,删除所有缓存 keys, err := g.Redis().Keys(ctx, "*-* couponlist") @@ -121,3 +125,24 @@ func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { return } + +func (s *sCoupon) InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) (err error) { + _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ + Title: req.Title, + Cover: req.Image, + ImgUrl: req.Image, + StartTime: req.StartTime.Unix(), + EndTime: req.EndTime.Unix(), + }).Insert() + if err != nil { + return err + } + + // 新增数据后,删除所有缓存 + keys, err := g.Redis().Keys(ctx, "*-* couponlist") + if len(keys) > 0 { + _, err = g.Redis().Del(ctx, keys...) + } + + return +} diff --git a/internal/service/coupon.go b/internal/service/coupon.go index 86948c7..7c266bd 100644 --- a/internal/service/coupon.go +++ b/internal/service/coupon.go @@ -6,6 +6,7 @@ package service import ( + "CouponBackendGo/api/v1/coupon" "CouponBackendGo/internal/model/dto" "context" ) @@ -16,6 +17,7 @@ type ( GetCouponListTotal(ctx context.Context) (total int, err error) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) DeleteCoupon(ctx context.Context, id int) (err error) + InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) (err error) } )