public async void CreateSell_InputViableSellData_ModelReturnsValid()
        {
            // Arrange
            //GenerateSession();
            MyListingsController controller = new MyListingsController(context, _hostEnvironment);
            SellListing          list       = InitializeSellListing();

            // Act
            await controller.CreateSell(list);

            // Assert
            Assert.True(controller.ModelState.IsValid);
        }
Example #2
0
        public List <ItemsForCart> Cart()
        {
            SellListing  sellListing = Product();
            ItemsForCart product     = new ItemsForCart
            {
                SellListing = sellListing,
                Quantity    = 1
            };
            List <ItemsForCart> cart = new List <ItemsForCart>();

            cart.Add(product);
            return(cart);
        }
Example #3
0
        public SellListing Product()
        {
            SellListing sellListing = new SellListing
            {
                SellListingId   = 20,
                SellTitle       = "Graphics Card",
                SellDescription = "Price scalping it bad",
                SellPrice       = 1500,
                SellQuantity    = 1,
            };

            return(sellListing);
        }
Example #4
0
        public SellListing Product()
        {           //Initializing a sample selllisting to use in use cases
            SellListing sellListing = new SellListing
            {
                SellListingId   = 21,
                SellTitle       = "PS5",
                SellDescription = "PS5 because it was restocked",
                SellPrice       = 500,
                SellQuantity    = 3,
            };

            return(sellListing);
        }
        public async Task EditSell_PassSellIdAndModifiedSellListing_ModelIsValid()
        {
            // Assert
            MyListingsController controller = new MyListingsController(context, _hostEnvironment);
            SellListing          list       = InitializeSellListing();

            // Act
            context.Add(list);
            list.SellPrice = 555555;
            var result = await controller.EditSell(list.SellListingId, list);

            // Assert
            Assert.True(controller.ModelState.IsValid);
            Assert.IsType <ViewResult>(result);
        }
        public async Task DeleteConfirmedSelling_PassInInitializedSellListingId_ReturnRedirectionToActionResult()
        {
            // Assert
            MyListingsController controller = new MyListingsController(context, _hostEnvironment);
            SellListing          list       = InitializeSellListing();

            InitializeSeller();

            // Act
            context.Add(list);
            var result = await controller.DeleteConfirmedSelling(0);

            // Assert
            Assert.IsType <RedirectToActionResult>(result);
        }
        public SellListing InitializeSellListing()
        {
            InitializePriceTrend();
            SellListing listing = new SellListing
            {
                SellerId           = 1,
                PriceTrendId       = 1,
                SellTitle          = "Sell 1",
                SellPrice          = 99,
                SellDate           = Convert.ToDateTime("2020-10-02"),
                SellItemType       = "Game",
                SellQuantity       = 3,
                PriceTrendKeywords = "PS5",
            };

            return(listing);
        }
        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));
        }
        public async Task <IActionResult> CreateSell([Bind("SellListingId,SellerId,PriceTrendId,SellTitle,SellDescription,SellPrice,SellDate,SellItemType,SellQuantity,ImageFile,Display")] SellListing sellListing)
        {
            sellListing.SellDate = DateTime.Now;
            sellListing.Display  = true;
            string userId = "";

            try
            {
                userId = HttpContext.Session.GetString("userId");
            }
            catch (Exception)
            {
                // Do nothing
            }

            // We need the seller ID associated with this account ID
            var sellerAccountId = await _context.SellerAccounts
                                  .Include(s => s.Account)
                                  .FirstOrDefaultAsync(s => s.Account.AccountId.ToString() == userId);

            //Find price trend items with same title as new sell listing title
            var priceTrend = await _context.PriceTrends
                             .Where(m => m.ItemName == sellListing.SellTitle)
                             .ToListAsync();


            if (ModelState.IsValid)
            {
                try
                {
                    sellListing.SellerId = sellerAccountId.SellerId;
                    //Check if a price trend exists for the sell listing, and create one if it does not exist
                    if (priceTrend.Count == 0)
                    {
                        PriceTrend trend = new PriceTrend();
                        trend.ItemName     = sellListing.SellTitle;
                        trend.DateOfUpdate = DateTime.Now;
                        trend.AveragePrice = sellListing.SellPrice;
                        trend.HighestPrice = sellListing.SellPrice;
                        trend.LowestPrice  = sellListing.SellPrice;

                        PriceTrendsController priceTrendsController = new PriceTrendsController(_context);
                        await priceTrendsController.Create(trend);
                    }

                    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";
                    }

                    _context.Add(sellListing);
                    await _context.SaveChangesAsync();
                }
                catch (Exception)
                {
                    return(RedirectToAction(nameof(Index)));
                }

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["PriceTrendId"] = new SelectList(_context.PriceTrends, "PriceTrendId", "PriceTrendId", sellListing.PriceTrendId);
            ViewData["SellerId"]     = new SelectList(_context.SellerAccounts, "SellerId", "SellerId", sellListing.SellerId);
            return(View(sellListing));
        }