Пример #1
0
        public async Task <IActionResult> EditSell(int id, [Bind("SellListingId,SellerId,PriceTrendId,SellTitle,SellDescription,SellPrice,SellDate,SellItemType,SellQuantity, ImageFile, Display")] SellListing sellListing)
        {
            try
            {
                if (id != sellListing.SellListingId)
                {
                    return(NotFound());
                }

                sellListing.SellDate = DateTime.Now;
            }
            catch (Exception)
            {
                // Do nothing
            }

            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);

            var priceTrendExists = PriceTrendExists(sellListing.PriceTrendId);

            if (ModelState.IsValid)
            {
                try
                {
                    sellListing.SellerId = sellerAccountId.SellerId;
                    // If the user edits their sell listing,
                    // check to see if the listing is associated with any other cart
                    // that has been BOUGHT

                    var itemWasBought = _context.ItemsForCart
                                        .Include(s => s.Cart)
                                        .Where(s => s.SellListingId == id && s.Cart.TransactionComplete == true);

                    // If the edited item that was already bought by someone,
                    // we must create another sell listing with the new data
                    if (itemWasBought.ToList().Count != 0)
                    {
                        var editDisplay = await _context.SellListings
                                          .FirstOrDefaultAsync(s => s.SellListingId == sellListing.SellListingId);

                        editDisplay.Display = false;
                        _context.Update(editDisplay);
                        await _context.SaveChangesAsync();

                        sellListing.SellListingId = 0;
                        await CreateSell(sellListing);

                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        // This item was not bought by anyone
                        // However, if it is inside anyone's cart right now, remove it
                        var itemInCart = _context.ItemsForCart
                                         .Where(s => s.SellListingId == id && s.Cart.TransactionComplete == false);

                        if (itemInCart != null && itemInCart.ToList().Count != 0)
                        {
                            foreach (var item in itemInCart)
                            {
                                _context.Remove(item);
                            }
                        }

                        // Then proceed like usual
                        if (sellListing.ImageFile != null)
                        {
                            //Save image to wwwroot/images
                            string wwwRootPath = _hostEnvironment.WebRootPath;
                            string fileName    = Path.GetFileNameWithoutExtension(sellListing.ImageFile.FileName);
                            string extension   = Path.GetExtension(sellListing.ImageFile.FileName);
                            sellListing.SellImage = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                            string path = Path.Combine(wwwRootPath + "/Images/", fileName);
                            using (var fileStream = new FileStream(path, FileMode.Create))
                            {
                                await sellListing.ImageFile.CopyToAsync(fileStream);
                            }
                        }
                        else
                        {
                            sellListing.SellImage = "buy-icon.png";
                        }


                        try
                        {
                            sellListing.Display = true;
                            _context.Update(sellListing);
                            await _context.SaveChangesAsync();
                        }
                        catch (DbUpdateConcurrencyException)
                        {
                            if (!SellListingExists(sellListing.SellListingId))
                            {
                                return(NotFound());
                            }
                            else
                            {
                                throw;
                            }
                        }
                        return(RedirectToAction(nameof(Index)));
                    }
                }
                catch (Exception)
                {
                    // Do nothing
                }
            }
            ViewData["PriceTrendId"] = new SelectList(_context.PriceTrends, "PriceTrendId", "PriceTrendId", sellListing.PriceTrendId);
            ViewData["SellerId"]     = new SelectList(_context.SellerAccounts, "SellerId", "SellerId", sellListing.SellerId);
            return(View(sellListing));
        }