public async Task <IActionResult> Edit(int id, [Bind("AccountId,UserName,UserPassword,PaswordHash,FirstName,LastName,Email,PointBalance")] Account account) { if (id != account.AccountId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(account); await _context.SaveChangesAsync(); //Set Session objects for the User Name to be used throughout the web application HttpContext.Session.SetString("username", account.UserName); HttpContext.Session.SetString("userEmail", account.Email); } catch (DbUpdateConcurrencyException) { if (!AccountExists(account.AccountId)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index", "Home")); } return(View(account)); }
// Adding the item to cart public async Task <IActionResult> Add(int listingId, string userId) { string url = "/Accounts/Login"; if (userId == null) { return(LocalRedirect(url)); } // Find the cart associated with this account var cartContext = await _context.Cart .Where(s => s.TransactionComplete == false) .FirstOrDefaultAsync(s => s.AccountId.ToString() == userId); // If the account does not have a cart associated with them, create one if (cartContext == null) { await CreateCart(userId); cartContext = await _context.Cart. Where(s => s.TransactionComplete == false) .FirstOrDefaultAsync(s => s.AccountId.ToString() == userId); } // Does this item already exist in this cart? var itemContext = await _context.ItemsForCart .Where(s => s.CartId == cartContext.CartId) .FirstOrDefaultAsync(s => s.SellListingId == listingId); if (itemContext == null) { ItemsForCart item = new ItemsForCart(); item.CartId = cartContext.CartId; item.SellListingId = listingId; item.Quantity = 1; if (ModelState.IsValid) { _context.Add(item); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } else { // Find the associated cart item itemContext.Quantity++; _context.Update(itemContext); await _context.SaveChangesAsync(); } return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(int id, [Bind("SellerId,AccountId,AverageRating")] SellerAccount sellerAccount) { if (id != sellerAccount.SellerId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(sellerAccount); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SellerAccountExists(sellerAccount.SellerId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["AccountId"] = new SelectList(_context.Accounts, "AccountId", "Email", sellerAccount.AccountId); return(View(sellerAccount)); }
public async Task <IActionResult> Edit(int id, [Bind("PriceTrendId,ItemName,DateOfUpdate,AveragePrice,LowestPrice,HighestPrice")] PriceTrend priceTrend) { if (id != priceTrend.PriceTrendId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(priceTrend); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PriceTrendExists(priceTrend.PriceTrendId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(priceTrend)); }
public async Task <IActionResult> Edit(int id, [Bind("RewardId,AccountId,RewardType,RewardCode,PointCost,DateReceived")] Reward reward) { if (id != reward.RewardId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(reward); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RewardExists(reward.RewardId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["AccountId"] = new SelectList(_context.Accounts, "AccountId", "Email", reward.AccountId); return(View(reward)); }
public async Task <IActionResult> Edit(int id, [Bind("AccountPurchaseId,AccountId,CartId,PurchaseDate,PurchasePrice,TrackingNumber,PointsGained")] AccountPurchase accountPurchase) { if (id != accountPurchase.AccountPurchaseId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(accountPurchase); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AccountPurchaseExists(accountPurchase.AccountPurchaseId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["AccountId"] = new SelectList(_context.Accounts, "AccountId", "Email", accountPurchase.AccountId); ViewData["CartId"] = new SelectList(_context.Cart, "CartId", "CartId", accountPurchase.CartId); return(View(accountPurchase)); }
public async Task <IActionResult> EditTrade(int id, [Bind("TradeListingId,SellerId,TradeTitle,TradeDescription,TradeFor,TradeDate,TradeItemType,TradeQuantity, ImageFile, TradeLocation")] TradeListing tradeListing) { if (id != tradeListing.TradeListingId) { return(NotFound()); } tradeListing.TradeDate = DateTime.Now; string userId = ""; try { userId = HttpContext.Session.GetString("userId"); } catch (Exception) { // Do nothing } var sellerAccountId = await _context.SellerAccounts .Include(s => s.Account) .FirstOrDefaultAsync(s => s.Account.AccountId.ToString() == userId); if (ModelState.IsValid) { try { tradeListing.SellerId = sellerAccountId.SellerId; if (tradeListing.ImageFile != null) { //Save image to wwwroot/images string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(tradeListing.ImageFile.FileName); string extension = Path.GetExtension(tradeListing.ImageFile.FileName); tradeListing.TradeImage = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; string path = Path.Combine(wwwRootPath + "/Images/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await tradeListing.ImageFile.CopyToAsync(fileStream); } } else { tradeListing.TradeImage = "trade-icon.png"; } try { _context.Update(tradeListing); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TradeListingExists(tradeListing.TradeListingId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } catch (Exception) { return(RedirectToAction(nameof(Index))); } } ViewData["SellerId"] = new SelectList(_context.SellerAccounts, "SellerId", "SellerId", tradeListing.SellerId); return(View(tradeListing)); }
public async Task <IActionResult> Create([Bind("SellerReviewId,AccountId,SellerId,BuyerRating,ReviewDescription")] SellerReview sellerReview) { string userId = ""; try { userId = HttpContext.Session.GetString("userId"); } catch (Exception) { userId = ""; } string id = ""; try { id = HttpContext.Session.GetString("sellerid"); } catch (Exception) { id = ""; } var account = await _context.Accounts .FirstOrDefaultAsync(s => s.AccountId.ToString() == userId); // seller id is incorrect if (ModelState.IsValid) { try { sellerReview.SellerId = Convert.ToInt32(id); sellerReview.Account = account; sellerReview.AccountId = account.AccountId; _context.Add(sellerReview); await _context.SaveChangesAsync(); TempData["Message"] = "Successfully added review!"; var sellerId = sellerReview.SellerId; // All the reviews var reviewContext = _context.SellerReviews .Include(s => s.Seller) .Include(s => s.Seller.Account) .Include(s => s.Account) .Where(s => s.Seller.SellerId == sellerId); var sellerAccount = await _context.SellerAccounts .Include(s => s.Account) .FirstOrDefaultAsync(s => s.SellerId == sellerId); sellerAccount.AverageRating = Math.Round((double)reviewContext.Average(s => s.BuyerRating), 2); _context.Update(sellerAccount); await _context.SaveChangesAsync(); } catch (Exception) { // Do nothing, error should never happen } return(RedirectToAction(nameof(Index))); } ViewData["SellerId"] = new SelectList(_context.SellerAccounts, "SellerId", "SellerId", sellerReview.SellerId); return(View(sellerReview)); }