Beispiel #1
0
        public JsonResult RedeemCoupon(RedeemCouponViewModel model)
        {
            using (var db = new TourEntities())
            {
                var couponTransaction = db.RedeemCoupon.Where(x => x.CouponCode == model.CouponCode).FirstOrDefault();

                if (couponTransaction != null)
                {
                    ModelState.AddModelError("CouponCode", "Coupon Code had been redeem before.");
                }

                if (ModelState.IsValid)
                {
                    string       imageName = "";
                    RedeemCoupon newCoupon = new RedeemCoupon();

                    if (model.Image != null)
                    {
                        imageName = System.IO.Path.GetFileName(model.Image.FileName);
                        imageName = MetadataServices.GetDateTimeWithoutSlash() + "-" + imageName;
                        string physicalPath = Server.MapPath("~/Image/RedeemCoupon/" + imageName);
                        model.Image.SaveAs(physicalPath);
                    }

                    var currentUser = MetadataServices.GetCurrentUser();

                    var customer = db.Customer.Where(x => x.UserId == currentUser.UserId).FirstOrDefault();

                    newCoupon.CouponCode        = model.CouponCode;
                    newCoupon.TVRAmount         = model.Amount;
                    newCoupon.ImageProof        = imageName;
                    newCoupon.CustomerId        = customer.CustomerId;
                    newCoupon.CreatedBy         = currentUser.Username;
                    newCoupon.CreatedAt         = DateTime.Now;
                    newCoupon.UpdatedAt         = DateTime.Now;
                    newCoupon.UpdatedBy         = currentUser.Username;
                    newCoupon.TransactionStatus = (int)EnjoyOurTour.Helpers.TransactionStatus.Pending;

                    db.RedeemCoupon.Add(newCoupon);
                    db.SaveChanges();

                    return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    List <string> errors = new List <string>();

                    foreach (ModelState modelState in ViewData.ModelState.Values)
                    {
                        foreach (ModelError error in modelState.Errors)
                        {
                            errors.Add(string.IsNullOrEmpty(error.ErrorMessage) ? error.Exception.ToString() : error.ErrorMessage);
                        }
                    }
                    return(Json(new { success = false, errors = errors }, JsonRequestBehavior.AllowGet));
                }
            }
        }
Beispiel #2
0
        public ActionResult RedeemCoupon()
        {
            using (var db = new TourEntities())
            {
                RedeemCouponViewModel redeemCouponViewModel = new RedeemCouponViewModel();

                return(View(redeemCouponViewModel));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> RedeemCoupon(RedeemCouponViewModel redeemCouponViewModel)
        {
            var redeemCouponCommand = new RedeemCoupon()
            {
                AccountId  = User.ReadClaimAsGuidValue("urn:codefliptodo:accountid"),
                CouponCode = redeemCouponViewModel.CouponCode
            };

            var redeemed = await _mediator.Send(redeemCouponCommand);

            if (redeemed)
            {
                return(Ok("Coupon has been successfully applied to your account!"));
            }

            return(BadRequest("Unable to apply the coupon code."));
        }