Exemplo n.º 1
0
        public async Task <IActionResult> CreateTrade([Bind("TradeListingId,SellerId,TradeTitle,TradeDescription,TradeFor,TradeDate,TradeItemType,TradeQuantity,ImageFile, TradeLocation")] TradeListing tradeListing)
        {
            tradeListing.TradeDate = DateTime.Now;

            var info = await CityStateCountByIp("99.226.48.14");

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

                    _context.Add(tradeListing);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception)
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }
            ViewData["SellerId"] = new SelectList(_context.SellerAccounts, "SellerId", "SellerId", tradeListing.SellerId);
            return(View(tradeListing));
        }
Exemplo n.º 2
0
        void InitializeSeller()
        {
            SellerAccount seller = new SellerAccount
            {
                AccountId     = 0,
                AverageRating = 0,
                Account       = InitializeAccount()
            };

            context.Add(seller);
        }
Exemplo n.º 3
0
        //Will determine which reward is being redeemed then create a new reward object associated to that account
        public async Task AddRewardToAccount(int id, Account account)
        {
            if (ModelState.IsValid)
            {
                Reward newReward = new Reward();
                newReward.AccountId    = account.AccountId;
                newReward.DateReceived = DateTime.Now;
                newReward.RewardCode   = RandomString();

                int oldPointBalance = (int)account.PointBalance;
                int newPointBalance = 0;

                if (id == 1)
                {
                    newReward.RewardType = "-25% Discount Code";
                    newReward.PointCost  = 50;

                    newPointBalance = oldPointBalance - 50;
                }
                else if (id == 2)
                {
                    newReward.RewardType = "-50% Discount Code";
                    newReward.PointCost  = 100;

                    newPointBalance = oldPointBalance - 100;
                }
                else if (id == 3)
                {
                    newReward.RewardType = "-75% Discount Code";
                    newReward.PointCost  = 150;

                    newPointBalance = oldPointBalance - 150;
                }
                else
                {
                    ModelState.AddModelError("", "Invalid Reward Id");
                }

                _context.Add(newReward);
                await _context.SaveChangesAsync();

                AccountsController accountsController = new AccountsController(_context, _hostEnvironment);
                await accountsController.EditPoints(account, newPointBalance);

                HttpContext.Session.SetInt32("pointsBalance", newPointBalance);
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("PriceTrendId,ItemName,DateOfUpdate,AveragePrice,LowestPrice,HighestPrice")] PriceTrend priceTrend)
        {
            if (ModelState.IsValid)
            {
                _context.Add(priceTrend);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(priceTrend));
        }
Exemplo n.º 5
0
        TradeListing InitializeTrade()
        {
            context.Add(InitializeAccount());
            TradeListing listing = new TradeListing
            {
                Seller           = InitializeSeller(),
                SellerId         = 1,
                TradeTitle       = "Trade 1",
                TradeDescription = "This is the first trade",
                TradeFor         = "Water",
                TradeDate        = Convert.ToDateTime("2020-10-02"),
                TradeItemType    = "Game",
                TradeQuantity    = 3,
                TradeImage       = null,
                TradeLocation    = "Ontario"
            };

            context.Add(listing);
            return(listing);
        }
        public async Task <IActionResult> Create([Bind("SellerId,AccountId,AverageRating")] SellerAccount sellerAccount)
        {
            if (ModelState.IsValid)
            {
                _context.Add(sellerAccount);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AccountId"] = new SelectList(_context.Accounts, "AccountId", "Email", sellerAccount.AccountId);
            return(View(sellerAccount));
        }
Exemplo n.º 7
0
        // Creates the cart
        public async Task <IActionResult> CreateCart(string id)
        {
            string userId = id;
            Cart   cart   = new Cart();

            cart.AccountId           = int.Parse(userId);
            cart.TransactionComplete = false;
            cart.NumberOfProducts    = 0;
            cart.TotalPrice          = 0;
            cart.PointsGained        = 0;

            if (ModelState.IsValid)
            {
                _context.Add(cart);
                await _context.SaveChangesAsync();

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

            return(RedirectToAction("Index"));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Create([Bind("AccountPurchaseId,AccountId,CartId,PurchaseDate,PurchasePrice,TrackingNumber,PointsGained")] AccountPurchase accountPurchase)
        {
            if (ModelState.IsValid)
            {
                _context.Add(accountPurchase);
                await _context.SaveChangesAsync();

                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));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Create(AccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                foreach (var dbItem in _context.Accounts)
                {
                    if (dbItem.UserName == model.Username)
                    {
                        ModelState.AddModelError("", "Username already taken");
                        return(View(model));
                    }
                    else if (dbItem.Email == model.Email)
                    {
                        ModelState.AddModelError("", "Email already taken");
                        return(View(model));
                    }
                }

                var    originalPassword = model.Password;
                string key = RandomString();

                var encryptedPassword = EncryptString(originalPassword, key);

                Account account = new Account();
                account.UserName     = model.Username;
                account.UserPassword = encryptedPassword.ToString();
                account.PaswordHash  = key.ToString();
                account.FirstName    = model.FirstName;
                account.LastName     = model.LastName;
                account.Email        = model.Email;
                account.PointBalance = 0;


                _context.Add(account);
                await _context.SaveChangesAsync();

                LoginViewModel loginModel = new LoginViewModel();
                loginModel.Username = model.Username;
                loginModel.Password = model.Password;

                await Login(loginModel);

                return(RedirectToAction("Index", "Home"));
            }
            ModelState.AddModelError("", "Invalid attempt");
            return(View(model));
        }
Exemplo n.º 10
0
        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));
        }