public IActionResult UseCoupon([FromBody] UseCoupon useCoupon) // 买家使用优惠券 { if (buyerCouponService.deleteBuyerCoupon(useCoupon.CouponId)) { JsonData jsondata = new JsonData(); jsondata["deleteCoupon"] = "SUCCESS"; return(Json(jsondata.ToJson())); } else { JsonData jsondata = new JsonData(); jsondata["deleteCoupon"] = "ERROR"; return(Json(jsondata.ToJson())); } }
public ActionResult UseCoupon([FromBody] UseCoupon data, string code) { try { var checkData = MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon"); if (checkData != null) { var result = BsonSerializer.Deserialize <Coupon>(checkData); var filter = Builders <BsonDocument> .Filter.Eq("Code", code); if (data.UsageCount > 0) { var coupon = BsonSerializer.Deserialize <Coupon>(MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon")); if (result.Percentage == true) { var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("UsageCount", result.UsageCount - data.UsageCount)); } else { if (data.Amount > coupon.Value) { return(Ok(new ResponseData { Code = "401", Message = "Amount is higher than the coupon value", Data = null })); } else { var balance = coupon.Value - data.Amount; var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("Value", balance)); if (balance == 0) { var updateUsageCount = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("UsageCount", 0)); } } } } return(Ok(new ResponseData { Code = "200", Message = "Coupon updated successfully", Data = null })); } else { return(BadRequest(new ResponseData { Code = "404", Message = "Coupon not found", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("CouponController", "UseCoupon", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }