/// <summary> /// Creates a new ShoppingCartItem for selected whiskey or adds more if SHCartItem already existed. /// </summary> /// <param name="whiskey">Whiskey that is being added.</param> /// <param name="amount">Amount of whiskey added to the cart.</param> public async Task AddToCartAsync(WhiskeyBase whiskey, int amount) { //Check if that whiskey is already in cart. var ShCartItem = await _appDbContext.ShoppingCartItems.SingleOrDefaultAsync( s => s.Whiskey.Id == whiskey.Id && s.ShoppingCartId == ShoppingCartId); //If whiskey is not already in cart, make new CartItem with specified amount. //Otherwise add new amount to already added amount. if (ShCartItem == null) { ShCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Whiskey = whiskey, Amount = amount }; _appDbContext.ShoppingCartItems.Add(ShCartItem); } else { ShCartItem.Amount += amount; } await _appDbContext.SaveChangesAsync(); }
public async Task <IActionResult> OnGet(int?whiskeyId) { if (whiskeyId.HasValue) { Whiskey = await context.GetWhiskeyById(whiskeyId.Value); CountriesList = new SelectList(await general.GetAllCountriesAsync(), "Id", "Name", Whiskey.CountryOfOrigin.Id); } else { Whiskey = new WhiskeyBase(); CountriesList = new SelectList(await general.GetAllCountriesAsync(), "Id", "Name"); } if (Whiskey == null) { return(RedirectToPage("./NotFound")); } Types = HtmlHelper.GetEnumSelectList <WhiskeyType>(); Images = new SelectList(new List <string> { Core.Helpers.ImageNames.Img1, Core.Helpers.ImageNames.Img2, Core.Helpers.ImageNames.Img3 }); return(Page()); }
public async Task <IActionResult> OnGet(int whiskeyId) { Whiskey = await context.GetWhiskeyById(whiskeyId); if (Whiskey == null) { return(RedirectToPage("./NotFound")); } return(Page()); }
public async Task <IActionResult> OnPost(int whiskeyId) { Whiskey = await context.DeleteWhiskey(whiskeyId); await general.Commit(); if (Whiskey == null) { return(RedirectToPage("./NotFound")); } TempData["Message"] = $"{Whiskey.Brand}{Whiskey.Name} deleted"; return(RedirectToPage("./Index")); }
/// <summary> /// Stella's update code. /// </summary> /// <param name="updatedWhiskey"></param> /// <param name="addNewCountry">Bool to check if a new country is being added.</param> /// <param name="countryName">Name of new country being added.</param> /// <returns></returns> public async Task <WhiskeyBase> UpdateWiskeyAsync(WhiskeyBase updatedWhiskey, bool addNewCountry, string countryName) { int whiskeyCountryId = 0; if (updatedWhiskey.CountryOfOrigin != null) { whiskeyCountryId = updatedWhiskey.CountryOfOrigin.Id; } updatedWhiskey.CountryOfOrigin = await _general.CheckNewCountry(addNewCountry, countryName, whiskeyCountryId); entity.State = EntityState.Modified; return(updatedWhiskey); }
/// <summary> /// Adds whiskey async /// </summary> /// <param name="NewWhiskey"></param> /// <param name="addNewCountry">Bool to check if a new country is being added.</param> /// <param name="CountryName">Name of new country being added.</param> /// <returns></returns> public async Task <WhiskeyBase> AddWhiskeyAsync(WhiskeyBase NewWhiskey, bool addNewCountry, string CountryName) { var WhiskeyCountry = NewWhiskey.CountryOfOrigin.Id; NewWhiskey.CountryOfOrigin = await general.CheckNewCountry(addNewCountry, CountryName, WhiskeyCountry); if (NewWhiskey.CountryOfOrigin != null) { whiskeyCountryId = NewWhiskey.CountryOfOrigin.Id; } NewWhiskey.CountryOfOrigin = await _general.CheckNewCountry(addNewCountry, countryName, whiskeyCountryId); db.Add(NewWhiskey); return(NewWhiskey); }
/// <summary> /// Removes a certain amount of whiskey from cart. /// </summary> /// <param name="whiskey">Specified whiskey to remove.</param> /// <param name="amount">Amount to remove.</param> public async Task RemoveFromCartAsync(WhiskeyBase whiskey, int amount) { //Check for whiskey in the cart. var ShCartItem = await _appDbContext.ShoppingCartItems.SingleOrDefaultAsync( s => s.Whiskey.Id == whiskey.Id && s.ShoppingCartId == ShoppingCartId); if (ShCartItem != null) { //If removed amount is less than amount in cart, adjust amount. //Otherwise remove ShCartItem entirely. if ((ShCartItem.Amount - amount) >= 1) { ShCartItem.Amount -= amount; } else { _appDbContext.ShoppingCartItems.Remove(ShCartItem); } } await _appDbContext.SaveChangesAsync(); }
public static void Seed(UserManager <ApplicationUser> userManager, ApplicationDbContext db, RoleManager <IdentityRole> roleManager) { if (roleManager.FindByNameAsync("Employee").Result == null) { IdentityRole Employees = new IdentityRole { Name = "Employee", NormalizedName = "EMPLOYEE" }; db.Add(Employees); db.SaveChanges(); } ; if (db.Countries.FirstOrDefault(c => c.Name == "Scotland") == null) { Country country = new Country { Name = "Scotland" }; db.Add(country); db.SaveChanges(); } if (db.Countries.FirstOrDefault(c => c.Name == "America") == null) { Country country = new Country { Name = "America" }; db.Add(country); db.SaveChanges(); } if (db.Countries.FirstOrDefault(c => c.Name == "Ireland") == null) { Country country = new Country { Name = "Ireland" }; db.Add(country); db.SaveChanges(); } if (db.Whiskeys.FirstOrDefault(w => w.Name == "Lagavulin 9 years House Lannister ") == null) { WhiskeyBase whiskey = new WhiskeyBase { Name = "Lagavulin 9 years House Lannister ", Brand = "Lagavulin", AgeYears = 9, Type = WhiskeyType.Single_Malt, CountryOfOrigin = db.Countries.FirstOrDefault(c => c.Name == "Scotland"), Price = 74.95m, Percentage = 0.46m, ImagePath = ImageNames.Img3, AmountInStorage = 7, SoftDeleted = false }; db.Add(whiskey); } if (db.Whiskeys.FirstOrDefault(w => w.Name == "Ardbeg 5 years Wee Beastie") == null) { WhiskeyBase whiskey = new WhiskeyBase { Name = "Ardbeg 5 years Wee Beastie", Brand = "Ardbeg", AgeYears = 4, Type = WhiskeyType.Single_Malt, CountryOfOrigin = db.Countries.FirstOrDefault(c => c.Name == "Scotland"), Price = 44m, Percentage = 0.47m, ImagePath = ImageNames.Img1, AmountInStorage = 18, SoftDeleted = false }; db.Add(whiskey); } if (db.Whiskeys.FirstOrDefault(w => w.Name == "Glenfiddich Fire & Cane") == null) { WhiskeyBase whiskey = new WhiskeyBase { Name = "Glenfiddich Fire & Cane", Brand = "Glenfiddich", AgeYears = 18, Type = WhiskeyType.Single_Malt, CountryOfOrigin = db.Countries.FirstOrDefault(c => c.Name == "Scotland"), Price = 44m, Percentage = 0.43m, ImagePath = ImageNames.Img2, AmountInStorage = 12, SoftDeleted = false }; db.Add(whiskey); } if (db.Whiskeys.FirstOrDefault(w => w.Name == "Jack Daniels") == null) { WhiskeyBase whiskey = new WhiskeyBase { Name = "Jack Daniels", Brand = "Jack Daniels", AgeYears = 5, Type = WhiskeyType.Blended_Grain, CountryOfOrigin = db.Countries.FirstOrDefault(c => c.Name == "America"), Price = 23.50m, Percentage = 0.40m, ImagePath = ImageNames.Img3, AmountInStorage = 10, SoftDeleted = false }; db.Add(whiskey); } if (db.Whiskeys.FirstOrDefault(w => w.Name == "Talisker 10 years Gift Tube") == null) { WhiskeyBase whiskey = new WhiskeyBase { Name = "Talisker 10 years Gift Tube", Brand = "Talisker", AgeYears = 10, Type = WhiskeyType.Single_Malt, CountryOfOrigin = db.Countries.FirstOrDefault(c => c.Name == "Scotland"), Price = 37.50m, Percentage = 0.45m, ImagePath = ImageNames.Img2, AmountInStorage = 20, SoftDeleted = false }; db.Add(whiskey); } if (db.Whiskeys.FirstOrDefault(w => w.Name == "Tullamore Dew") == null) { WhiskeyBase whiskey = new WhiskeyBase { Name = "Tullamore Dew", Brand = "Tullamore", AgeYears = 21, Type = WhiskeyType.Blended, CountryOfOrigin = db.Countries.FirstOrDefault(c => c.Name == "Ireland"), Price = 25m, Percentage = 0.4m, ImagePath = ImageNames.Img1, AmountInStorage = 22, SoftDeleted = false }; db.Add(whiskey); } if (userManager.FindByNameAsync("Admin1@Admin1").Result == null) { ApplicationUser user = new ApplicationUser { FName = "Rowan", MName = "", LName = "Brouwer", SoftDeleted = false, Employee = true, Email = "Admin1@Admin1", NormalizedEmail = "ADMIN1@ADMIN1", EmailConfirmed = true, UserName = "******", NormalizedUserName = "******", }; IdentityResult result = userManager.CreateAsync(user, "!Admin123").Result; if (result.Succeeded) { userManager.AddToRoleAsync(user, "Employee").Wait(); var roleId = db.UserRoles.FirstOrDefault(u => u.UserId == user.Id).RoleId.ToString(); userManager.AddClaimAsync(user, new Claim("EmployeeRole", roleId)).Wait(); } db.Add(user); } db.SaveChanges(); }