public ActionResult BuyVaucher(BuyVaucherBindingModel bm) { if (!this.ModelState.IsValid || !this.service.BuyVaucher(bm, this.User.Identity.Name)) { this.AddNotification("You must agree with our Terms and Conditions to buy the vaucher!", NotificationType.WARNING); return(this.RedirectToAction($"VaucherDetails/{bm.Id}", "Home", new { area = "" })); } this.AddNotification("Vaucher bought!", NotificationType.SUCCESS); return(this.RedirectToAction("Index", "Home", new { area = "" })); }
public bool BuyVaucher(BuyVaucherBindingModel bm, string name) { if (bm.Agrees != true) { return(false); } var vaucher = this.db.Vauchers.Find(v => v.Id == bm.Id); vaucher.Quantity--; if (vaucher.Quantity == 0) { vaucher.IsActive = false; } var customer = this.db.Customers.Find(c => c.AppUser.UserName == name); var vaucherUniqueCode = vaucher.UniqueVaucherCode.FirstOrDefault(uvc => uvc.IsBought == false); vaucherUniqueCode.CustomerId = customer.Id; vaucherUniqueCode.IsBought = true; if (!this.db.CustomersVauchers.GetAll().Any(cv => cv.CustomerId == customer.Id && cv.VaucherId == vaucher.Id)) { this.db.Customers.Find(c => c.AppUser.UserName == name) .CustomersVauchers.Add(new CustomersVauchers() { VaucherId = bm.Id, BoughtVauchers = 1 }); } else { this.db.CustomersVauchers.Find(cv => cv.CustomerId == customer.Id).BoughtVauchers++; } vaucher.Merchant.SoldVauchers++; this.db.SaveChanges(); return(true); }