public ActionResult RedeemAward(int awardID, int userID)
        {
            var user = (from x in db.Users
                        where x.userid == userID
                        select x).First();

            var award = (from x in db.AwardsTables
                         where x.awardID == awardID
                         select x).First();

            var redemptionCount = (from x in db.AwardRedemptions
                                   where x.awardIdFK == awardID
                                   select x).Count();

            var checkForRedemption = (from x in db.AwardRedemptions
                                      where x.awardIdFK == awardID && x.useridFK == userID
                                      select x).Any();

            if ((award.awardLimit - redemptionCount) <= 0)
            {
                //No more rewards of this kind left:
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            if (checkForRedemption)
            {
                //User has already taken the reward:
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
            }

            if (user.credits < award.creditsRequired)
            {
                //User has insufficient credits to redeem:
                return(new HttpStatusCodeResult(HttpStatusCode.NotAcceptable));
            }

            var insertAwardRedemption = new AwardRedemption
            {
                awardIdFK   = awardID,
                useridFK    = userID,
                isAwardUsed = false,
                UUID        = Guid.NewGuid()
            };

            db.AwardRedemptions.Add(insertAwardRedemption);
            user.credits -= award.creditsRequired;

            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Json(ex, JsonRequestBehavior.AllowGet));

                throw;
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            AwardRedemption awardRedemption = db.AwardRedemptions.Find(id);

            db.AwardRedemptions.Remove(awardRedemption);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "redemptionID,useridFK,awardIdFK,isAwardUsed,UUID")] AwardRedemption awardRedemption)
 {
     if (ModelState.IsValid)
     {
         db.Entry(awardRedemption).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.awardIdFK = new SelectList(db.AwardsTables, "awardID", "awardName", awardRedemption.awardIdFK);
     ViewBag.useridFK  = new SelectList(db.Users, "userid", "username", awardRedemption.useridFK);
     return(View(awardRedemption));
 }
        // GET: AwardRedemptions/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            AwardRedemption awardRedemption = db.AwardRedemptions.Find(id);

            if (awardRedemption == null)
            {
                return(HttpNotFound());
            }
            return(View(awardRedemption));
        }
        // GET: AwardRedemptions/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            AwardRedemption awardRedemption = db.AwardRedemptions.Find(id);

            if (awardRedemption == null)
            {
                return(HttpNotFound());
            }
            ViewBag.awardIdFK = new SelectList(db.AwardsTables, "awardID", "awardName", awardRedemption.awardIdFK);
            ViewBag.useridFK  = new SelectList(db.Users, "userid", "username", awardRedemption.useridFK);
            return(View(awardRedemption));
        }