//[ValidateAntiForgeryToken] public async Task <ActionResult> Create(CreateUserRewardRedemptionModel model) { if (ModelState.IsValid) { model.UserId = CurrentUser.UserId.Value; model.RewardStatus = RewardStatus.Open; model.RedeemDate = DateTime.Now; var response = await WepApiMethod.SendApiAsync <int> (HttpVerbs.Post, $"Reward/UserRewardRedemptions", model); if (response.isSuccess) { await LogActivity(Modules.Setting, "User Redeemed Reward"); TempData["SuccessMessage"] = "Reward Redeemed successfully"; return(RedirectToAction("List")); } else { ErrorClass errorObj = new JavaScriptSerializer().Deserialize <ErrorClass>(response.ErrorMessage); TempData["ErrorMessage"] = "Failed to Redeem Reward : " + errorObj.Message; return(RedirectToAction("List")); } } else { TempData["ErrorMessage"] = "Failed to Redeem Reward"; return(RedirectToAction("List")); } }
// GET: Reward/UserRewardRedemptions/Create public ActionResult Create(int?id) { if (id == null) { return(HttpNotFound()); } CreateUserRewardRedemptionModel model = new CreateUserRewardRedemptionModel(); model.RewardRedemptionId = (int)id; model.RewardStatus = RewardStatus.Open; model.RedeemDate = DateTime.Now; model.UserId = CurrentUser.UserId.Value; return(View(model)); }
// 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 async Task <ActionResult> Redeem(int?id) { if (id == null) { return(HttpNotFound()); } var response = await WepApiMethod.SendApiAsync <DetailRewardRedemptionModel> (HttpVerbs.Get, $"Reward/RewardRedemption?id={id}"); if (!response.isSuccess) { return(HttpNotFound()); } CreateUserRewardRedemptionModel model = new CreateUserRewardRedemptionModel { UserId = CurrentUser.UserId.Value, RewardRedemptionId = id.Value, RedeemDate = DateTime.Now, RewardStatus = RewardStatus.Open }; return(await Create(model)); }