diff --git a/core/api.go b/core/api.go index b59e6de..7f3959d 100644 --- a/core/api.go +++ b/core/api.go @@ -379,6 +379,11 @@ func (caller apiCaller) couponAPI(w http.ResponseWriter, r *http.Request) error logger.Println("begin listAllCouponNames") listAllCouponNames(caller.mg.mongoClient, w, r) } + + case "DELETE": + // 쿠폰 삭제 + logger.Println("begin deleteCoupon") + deleteCoupon(caller.mg.mongoClient, w, r) } return nil } diff --git a/core/api_coupon.go b/core/api_coupon.go index 8e788f6..46e690d 100644 --- a/core/api_coupon.go +++ b/core/api_coupon.go @@ -30,6 +30,7 @@ type couponDoc struct { Total int64 `json:"total" bson:"total"` Remains []string `json:"remains,omitempty" bson:"remains,omitempty"` Used []string `json:"used,omitempty" bson:"used,omitempty"` + Expire int64 `json:"expire" bson:"expire"` } func makeCouponKey(roundnum uint32, uid []byte) string { @@ -72,6 +73,7 @@ func generateCoupons(mongoClient gocommon.MongoClient, w http.ResponseWriter, r effect, _ := gocommon.ReadStringFormValue(r.Form, "effect") count, _ := gocommon.ReadIntegerFormValue(r.Form, "count") desc, _ := gocommon.ReadStringFormValue(r.Form, "desc") + expire, _ := gocommon.ReadIntegerFormValue(r.Form, "expire") if count == 0 { logger.Println("[generateCoupons] count == 0") @@ -92,6 +94,7 @@ func generateCoupons(mongoClient gocommon.MongoClient, w http.ResponseWriter, r Effect: effect, Desc: desc, Total: -1, + Expire: expire, }, }, options.Update().SetUpsert(true)); err != nil { logger.Println("[generateCoupons] Update failed :", err) @@ -154,6 +157,7 @@ func generateCoupons(mongoClient gocommon.MongoClient, w http.ResponseWriter, r Effect: effect, Desc: desc, Total: count, + Expire: expire, }, }, options.Update().SetUpsert(true)) } @@ -231,7 +235,7 @@ func queryCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *htt var coupon couponDoc if err := mongoClient.FindOneAs(CollectionCoupon, bson.M{ "_id": roundObj, - }, &coupon, options.FindOne().SetProjection(bson.M{"effect": 1, "name": 1, "reason": 1, "total": 1, "desc": 1}).SetReturnKey(false)); err != nil { + }, &coupon, options.FindOne().SetProjection(bson.M{"effect": 1, "name": 1, "reason": 1, "total": 1, "desc": 1, "expire": 1}).SetReturnKey(false)); err != nil { logger.Println("[queryCoupon] FindOneAs failed :", err) w.WriteHeader(http.StatusInternalServerError) return @@ -275,30 +279,13 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. round, _ = coupon.MakeCouponRoundHash(code) } - // 1. 내가 이 라운드의 쿠폰을 쓴 적이 있나 - already, err := mongoClient.Exists(CollectionCouponUse, bson.M{ - "_id": acc, - "rounds": round, - }) - if err != nil { - logger.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - - if already { - // 이미 이 라운드의 쿠폰을 사용한 적이 있다. - w.WriteHeader(http.StatusConflict) - return - } - var coupon couponDoc roundObj, _ := primitive.ObjectIDFromHex(round + round + round) if len(key) == 0 { // 무한 쿠폰일 수 있으므로 존재하는지 확인 if err := mongoClient.FindOneAs(CollectionCoupon, bson.M{ "_id": roundObj, - }, &coupon, options.FindOne().SetProjection(bson.M{"_id": 0, "effect": 1, "name": 1, "reason": 1, "total": 1})); err != nil { + }, &coupon, options.FindOne().SetProjection(bson.M{"_id": 0, "effect": 1, "name": 1, "reason": 1, "total": 1, "expire": 1})); err != nil { logger.Println(err) w.WriteHeader(http.StatusInternalServerError) return @@ -310,7 +297,7 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. return } } else { - // 2. 쿠폰을 하나 꺼냄 + // 쿠폰을 하나 꺼냄 matched, _, err := mongoClient.Update(CollectionCoupon, bson.M{ "_id": roundObj, }, bson.M{ @@ -328,7 +315,7 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. return } - // 3. round의 효과 읽기 + // round의 효과 읽기 if err := mongoClient.FindOneAndUpdateAs(CollectionCoupon, bson.M{ "_id": roundObj, }, bson.M{ @@ -340,6 +327,47 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. } } + if coupon.Expire > time.Now().Unix() { + // 쿠폰 만료시간 경과 + w.WriteHeader(http.StatusInternalServerError) + return + } + + alreadyused := false + + // 무한 쿠폰인지 일회용 쿠폰인지 구분 + // 쿠폰 사용 유무 검사 + if coupon.Total == -1 { + already, err := mongoClient.Exists(CollectionCouponUse, bson.M{ + "_id": acc, + "rounds": round, + }) + if err != nil { + logger.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + alreadyused = already + } else { + already, err := mongoClient.Exists(CollectionCouponUse, bson.M{ + "rounds": round, + }) + if err != nil { + logger.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + alreadyused = already + } + + if alreadyused { + // 이미 이 라운드의 쿠폰을 사용한 적이 있다. + w.WriteHeader(http.StatusConflict) + return + } + if len(coupon.Effect) == 0 { // 쿠폰이 없네? w.WriteHeader(http.StatusBadRequest) @@ -370,3 +398,22 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. w.Write([]byte(coupon.Effect)) } + +func deleteCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.Request) { + code, _ := gocommon.ReadStringFormValue(r.Form, "name") + if len(code) == 0 { + logger.Println("coupon delete code error") + w.WriteHeader(http.StatusBadRequest) + return + } + + _, err := mongoClient.Delete(CollectionCoupon, bson.M{ + "name": code, + }) + + if err != nil { + logger.Println("coupon delete error") + w.WriteHeader(http.StatusBadRequest) + return + } +}