diff --git a/api/v1/coupon/coupon.go b/api/v1/coupon/coupon.go index 4904a0e..fa36d49 100644 --- a/api/v1/coupon/coupon.go +++ b/api/v1/coupon/coupon.go @@ -16,14 +16,7 @@ type DeleteCouponReq struct { } 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:"失效时间"` -} - -type UpdateCouponReq struct { - Id int `v:"required#优惠券id不能为空" dc:"优惠券id"` + Id int `dc:"优惠券id"` Title string `v:"required#优惠券名字不能为空" dc:"优惠券名字"` Image string `v:"required#优惠券图片不能为空" dc:"优惠券图片"` StartTime *gtime.Time `v:"required#优惠券生效时间不能为空" dc:"生效时间"` diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index e8da64b..6472e48 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -21,7 +21,6 @@ var ( group.POST("/get-coupon", coupon.Coupon().GetCoupon) group.POST("/delete-coupon", coupon.Coupon().DeleteCoupon) group.POST("/insert-coupon", coupon.Coupon().InsertCoupon) - group.POST("/update-coupon", coupon.Coupon().UpdateCoupon) }) s.Run() return nil diff --git a/internal/logic/coupon/coupon.go b/internal/logic/coupon/coupon.go index 0c480b0..6d3f1d3 100644 --- a/internal/logic/coupon/coupon.go +++ b/internal/logic/coupon/coupon.go @@ -127,18 +127,28 @@ func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { } 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 req.Id == 0 { + _, 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() + } else { + _, 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(), + }).WherePri(req.Id).Update() + } if err != nil { return err } - // 新增数据后,删除所有缓存 + // 新增&编辑数据后,删除所有缓存 keys, err := g.Redis().Keys(ctx, "*-* couponlist") if len(keys) > 0 { _, err = g.Redis().Del(ctx, keys...)