public IHttpActionResult CheckRewardValidity(CheckRewardValidity reward) { //if (reward.UserId == null) { return BadRequest(); } //if (reward.RewardCode == null) { return BadRequest(); } if (!ModelState.IsValid) { return(BadRequest()); } UserRewardRedemption model = db.UserRewardRedemption.Where(r => r.UserId == reward.UserId && r.RewardRedemption.RewardCode == reward.RewardCode).FirstOrDefault(); if (model == null) { return(BadRequest("Sorry, Reward Code not valid")); } //if (model.RewardStatus == RewardStatus.Closed) { return BadRequest("Sorry, Reward already been used"); } DateTime rewardExpiredDate = model.RedeemDate.AddDays(model.RewardRedemption.ValidDuration); var tempModel = new { UserId = model.UserId, RewardCode = model.RewardRedemption.RewardCode, DiscountValue = model.RewardRedemption.DiscountValue, RedeemedDate = model.RedeemDate, ValidDurationInDays = model.RewardRedemption.ValidDuration, ExpiredDate = rewardExpiredDate, }; return(Ok(tempModel)); }
// POST: api/UserRewardRedemptions public IHttpActionResult Post(CreateUserRewardRedemptionModel model) { if (!ModelState.IsValid) { return(BadRequest("data not valid")); } var reward = db.RewardRedemption.Find(model.RewardRedemptionId); if (reward == null) { return(BadRequest("Reward not available")); } var points = db.UserRewardPoints.Where(u => u.UserId == model.UserId && u.Display).ToList(); int totalPoints = points.Sum(p => p.PointsReceived); var redeemed = db.UserRewardRedemption.Where(r => r.UserId == model.UserId).ToList(); var getExistingReward = redeemed.Where(r => r.RewardRedemptionId == model.RewardRedemptionId).FirstOrDefault(); if (getExistingReward != null) { return(BadRequest("You already claimed this reward")); } int usedPoints = redeemed.Sum(u => u.RewardRedemption.PointsToRedeem); int pointsLeft = totalPoints - usedPoints; //check if pointsLeft enough to redeem this reward if (pointsLeft < reward.PointsToRedeem) { return(BadRequest("Insuficient Points")); } var obj = new UserRewardRedemption { UserId = model.UserId, RewardRedemptionId = model.RewardRedemptionId, RedeemDate = DateTime.Now, RewardStatus = RewardStatus.Open }; db.UserRewardRedemption.Add(obj); db.SaveChanges(); return(Ok(obj.Id)); }
public IHttpActionResult UsedReward(string rewardCode, int?userId) { if (userId == null) { return(BadRequest()); } if (rewardCode == null) { return(BadRequest()); } UserRewardRedemption model = db.UserRewardRedemption.Where(r => r.UserId == userId && r.RewardRedemption.RewardCode == rewardCode).FirstOrDefault(); if (model == null) { return(BadRequest("Sorry, Reward Code not valid")); } if (model.RewardStatus == RewardStatus.Closed) { return(BadRequest("Sorry, Reward already been used")); } DateTime rewardExpiredDate = model.RedeemDate.AddDays(model.RewardRedemption.ValidDuration); if (rewardExpiredDate < DateTime.Now) { { return(BadRequest("Sorry, Reward Code expired")); } } model.RewardStatus = RewardStatus.Closed; db.UserRewardRedemption.Attach(model); db.Entry(model).Property(x => x.RewardStatus).IsModified = true; db.Configuration.ValidateOnSaveEnabled = true; db.SaveChanges(); var tempModel = new { UserId = model.UserId, RewardCode = model.RewardRedemption.RewardCode, DiscountValue = model.RewardRedemption.DiscountValue, }; return(Ok(tempModel)); }