public async Task <IActionResult> AddToWishlist(int productId) { var user = await _userManager.GetUserAsync(User); var assetModel = _assets.WishlistByUserid(user.Id).ToList(); //gets the basket of the logged in user if (!assetModel.Select(x => x.ProductId).Contains(productId)) // the basket already contains the product, add the amount by 1 { var wishlist = new Wishlist { UserId = user.Id, ProductId = productId }; _context.wishlists.Add(wishlist); _context.SaveChanges(); } return(Json(new { success = true })); }
public async Task <IActionResult> Index(int?page, int?pageAmount, string cardType, string sortBy, [FromQuery] List <string> catagorie, [FromQuery] List <string> grades, float?priceLow, float?priceHigh) { //if page/pageamount/price parameters are empty, fill with standard value var pageNumber = page ?? 1; var pageAmnt = pageAmount ?? 16; float priceL = priceLow ?? 0; float priceH = priceHigh ?? 10000; //queries to get cards and catagories from database var assetModels = _assets.GetbyCardType(cardType); List <string> cardscategory = assetModels.SelectMany(x => x.Catnames).Distinct().ToList(); List <int> wishlistproducts = new List <int>(); //send wishlist items to view for wishlist icon toggle ViewBag.wishlist = ""; var user = await _userManager.GetUserAsync(User); if (user != null) { wishlistproducts = _wishlist.WishlistByUserid(user.Id).Select(x => x.ProductId).ToList(); ViewBag.wishlist = wishlistproducts; } //viewbags to send to the view ViewBag.page = page; ViewBag.PageAmount = pageAmount; ViewBag.name = "Name"; ViewBag.totalCategory = cardscategory; ViewBag.catagorie = catagorie; ViewBag.catagoriestring = ""; ViewBag.grades = grades; ViewBag.PriceLow = priceL; ViewBag.PriceHigh = priceH; ViewBag.cardType = cardType; // sorting list for product sorting var sorting = new List <SelectListItem> { new SelectListItem { Text = "Name A-Z", Value = "name" }, new SelectListItem { Text = "Name Z-A", Value = "name_desc" }, new SelectListItem { Text = "Price High-Low", Value = "Price" }, new SelectListItem { Text = "Price Low-High", Value = "price_desc" } }; ViewBag.Sorting = sorting; ViewBag.SelectSort = sortBy ?? "Name A-Z"; ViewBag.sortBy = sortBy; //bind products from query(assetModels) to productviewmodel(listingResult) that is used in the view. var listingResult = assetModels .Select(result => new ProductsViewModel { Id = result.prods.ProductId, Name = result.prods.Name,/*.Length < 20 ? result.prods.Name : result.prods.Name.Substring(0, 15) + "...",*/ Price = Convert.ToDecimal(result.prods.Price.ToString("F")), ImageUrl = result.prods.ImageUrl, Grade = result.prods.Grade, Stock = result.prods.Stock, CardCatagoryList = result.Catnames, Favorites = false, AuctionEnd = result.prods.AuctionEndTime, AuctionStart = result.prods.DateCreated }); //filters if (catagorie.Count > 0) { //if statement to make sure when in catagorie if you select same catagorie name it doesn't run any code, for performance if (catagorie.Count == 1 && catagorie.Contains(cardType)) { } else { listingResult = listingResult.Where(x => x.CardCatagoryList.Intersect(catagorie).Any()); } } if (priceL > 0 || priceH < 10000) { listingResult = listingResult.Where(x => x.Price >= (decimal)priceL && x.Price <= (decimal)priceH); } //viewbag for the view with all the grades in it. ViewBag.Grade = listingResult.OrderBy(x => int.Parse(x.Grade)).Select(x => x.Grade).Distinct(); if (grades.Count > 0 && listingResult.Count(x => grades.Contains(x.Grade)) > 0) { listingResult = listingResult.Where(x => grades.Contains(x.Grade)); } //sorting switch (sortBy) { case "name_desc": listingResult = listingResult.OrderByDescending(s => s.Name); break; case "Price": listingResult = listingResult.OrderByDescending(s => s.Price); break; case "price_desc": listingResult = listingResult.OrderBy(s => s.Price); break; default: listingResult = listingResult.OrderBy(s => s.Name); break; } var onePageOfProducts = await listingResult.ToPagedListAsync(pageNumber, pageAmnt); ViewBag.OnePageOfProducts = onePageOfProducts; if (cardType == "Pokemon") { return(View("~/Views/Products/Pokemon/Pokemon.cshtml")); } if (cardType == "YuGiOh") { return(View("~/Views/Products/YuGiOh/YuGiOh.cshtml")); } if (cardType == "Magic") { return(View("~/Views/Products/Magic/Magic.cshtml")); } return(View()); }