示例#1
0
        public async Task <IActionResult> Create()
        {
            HttpContext.Session.TryGetCart(out Cart cart);
            if (cart == null || !cart.Items.Any())
            {
                return(RedirectToAction("Index", "Home"));
            }
            Dictionary <Product, int> products = new Dictionary <Product, int>();

            await using Models.AppContext db = new Models.AppContext();
            foreach (var entry in cart.Items)
            {
                products.Add(db.Products.Include(p => p.Seller)
                             .FirstOrDefaultAsync(p => p.Id == entry.Key).Result, entry.Value);
            }

            foreach (var product in products)
            {
                if (product.Key.Count < product.Value)
                {
                    return(View());
                }
            }

            ViewBag.Products   = products;
            ViewBag.Deliveries = db.Deliveries.ToList();
            ViewBag.TotalPrice = cart.TotalPrice;
            return(View(new Order()));
        }
示例#2
0
        public async Task <IActionResult> Login(LoginModel model, Role role)
        {
            if (ModelState.IsValid)
            {
                await using Models.AppContext db = new Models.AppContext();
                if (role == Role.Buyer)
                {
                    Buyer user = await db.Buyers.FirstOrDefaultAsync(u => u.Email == model.Email &&
                                                                     u.Password == model.Password);

                    if (user != null)
                    {
                        await AuthenticateBuyer(user);

                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    Seller user = await db.Sellers.FirstOrDefaultAsync(u => u.Email == model.Email &&
                                                                       u.Password == model.Password);

                    if (user != null)
                    {
                        await AuthenticateSeller(user);

                        return(RedirectToAction("Index", "Home"));
                    }
                }
                ModelState.AddModelError("", "Incorrect login or/and password.");
            }
            return(View("Login", model));
        }
示例#3
0
        public async Task <IActionResult> EditSeller(Seller seller)
        {
            if (ModelState.IsValid)
            {
                await using Models.AppContext db = new Models.AppContext();
                string email = User?.FindFirst(u => u.Type == ClaimTypes.Email)?.Value;
                var    user  = await db.Sellers.FirstOrDefaultAsync(p =>
                                                                    p.Email == email);

                user.FirstName = seller.FirstName;
                user.LastName  = seller.LastName;
                user.City      = seller.City;
                await db.SaveChangesAsync();

                await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

                var claims = new List <Claim>
                {
                    new Claim(ClaimsIdentity.DefaultNameClaimType, seller.FirstName + " " + seller.LastName),
                    new Claim(ClaimTypes.Email, email),
                    new Claim(ClaimTypes.GivenName, seller.FirstName),
                    new Claim(ClaimTypes.Surname, seller.LastName),
                    new Claim(ClaimTypes.Locality, seller.City),
                    new Claim(ClaimTypes.Role, "Seller")
                };
                ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType,
                                                       "Seller");
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id));
            }

            return(RedirectToAction("Index", "Home"));
        }
示例#4
0
        public async Task <IActionResult> GoogleResponse()
        {
            var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);

            var claims = result.Principal?.Identities.FirstOrDefault()?.Claims
                         .ToDictionary(k => k.Type, v => v.Value);

            await using (var db = new Models.AppContext())
            {
                Buyer user = await db.Buyers
                             .FirstOrDefaultAsync(p => p.Email == claims[ClaimTypes.Email]);

                if (user == null)
                {
                    await db.Buyers.AddAsync(new Buyer()
                    {
                        Email     = claims[ClaimTypes.Email],
                        FirstName = claims[ClaimTypes.GivenName],
                        LastName  = claims[ClaimTypes.Surname],
                        Password  = "******"
                    });

                    await db.SaveChangesAsync();
                }
            }

            return(RedirectToAction("Index", "Home"));
        }
示例#5
0
        public async Task <IActionResult> RegisterAsSeller(SellerRegisterModel model)
        {
            if (ModelState.IsValid)
            {
                await using Models.AppContext db = new Models.AppContext();
                Buyer user = await db.Buyers.FirstOrDefaultAsync(u => u.Email == model.Email);

                if (user == null)
                {
                    EntityEntry <Seller> addedSeller = await db.Sellers.AddAsync(new Seller
                    {
                        Email     = model.Email,
                        Password  = model.Password,
                        FirstName = model.FirstName,
                        LastName  = model.LastName,
                        City      = model.City
                    });

                    await db.SaveChangesAsync();

                    await AuthenticateSeller(addedSeller.Entity);

                    return(RedirectToAction("Index", "Home"));
                }
                ModelState.AddModelError("", "Incorrect login or/and password.");
            }
            return(View("SellerRegister", model));
        }
示例#6
0
        public async Task <IActionResult> Products()
        {
            await using Models.AppContext db = new Models.AppContext();
            string email    = User?.FindFirst(u => u.Type == ClaimTypes.Email)?.Value;
            var    products = db.Products.Include(p => p.Seller)
                              .Where(p => p.Seller.Email == email).ToList();

            return(View(products));
        }
示例#7
0
        public async Task <IActionResult> Create(Order order)
        {
            HttpContext.Session.TryGetCart(out Cart cart);
            if (cart == null || !cart.Items.Any())
            {
                return(RedirectToAction("Index", "Home"));
            }
            await using Models.AppContext db = new Models.AppContext();
            var allProducts = (db.Products.AsEnumerable()
                               .Where(p => (cart.Items.Keys.Contains(p.Id) && p.Count >= cart.Items[p.Id]))).ToList();
            var orders = allProducts.GroupBy(key => key.SellerId).Select(p => new
            {
                SellerId = p.Key,
                Products = p.Select(pr => pr)
            });

            foreach (var item in orders)
            {
                Order newOrder = (await db.Orders.AddAsync(new Order()
                {
                    BuyerId = db.Buyers.AsEnumerable().FirstOrDefault(p => p.Email ==
                                                                      User.FindFirst(u => u.Type == ClaimTypes.Email).Value).Id,
                    DeliveryId = order.DeliveryId,
                    Destination = order.Destination,
                    SellerId = item.SellerId,
                    TotalPrice = item.Products.Sum(p => p.Price * cart.Items[p.Id])
                })).Entity;

                foreach (var product in item.Products)
                {
                    product.Count -= cart.Items[product.Id];
                    order.ProductOrder.Add(new ProductOrder()
                    {
                        Count     = cart.Items[product.Id],
                        Price     = product.Price,
                        ProductId = product.Id,
                        OrderId   = newOrder.Id
                    });
                    await db.SaveChangesAsync();

                    await db.ProductOrders.AddAsync(new ProductOrder()
                    {
                        Count     = cart.Items[product.Id],
                        Price     = product.Price,
                        ProductId = product.Id,
                        OrderId   = newOrder.Id
                    });
                }

                await db.SaveChangesAsync();
            }

            return(RedirectToAction("Index", "Home"));
        }
示例#8
0
        public async Task <IActionResult> EditProduct(Product product)
        {
            await using Models.AppContext db = new Models.AppContext();
            string email         = User?.FindFirst(u => u.Type == ClaimTypes.Email)?.Value;
            var    productToEdit = await db.Products.FirstOrDefaultAsync(p => p.Id == product.Id);

            productToEdit.Count       = product.Count;
            productToEdit.Description = product.Description;
            productToEdit.Quantity    = product.Quantity;
            productToEdit.Price       = product.Price;
            productToEdit.Name        = product.Name;
            await db.SaveChangesAsync();

            return(View("Products"));
        }
示例#9
0
        public async Task <IActionResult> AddProduct(Product product)
        {
            string email = User.FindFirst(u => u.Type == ClaimTypes.Email)?.Value;

            await using Models.AppContext db = new Models.AppContext();
            Seller seller = await db.Sellers.FirstOrDefaultAsync(s => s.Email == email);

            product.SellerId = seller.Id;

            await db.Products.AddAsync(product);

            await db.SaveChangesAsync();


            return(RedirectToAction("Index", "Home"));
        }
示例#10
0
        public async Task <IActionResult> EditProduct(int id)
        {
            await using Models.AppContext db = new Models.AppContext();
            string  email   = User.FindFirst(u => u.Type == ClaimTypes.Email).Value;
            Product?product = db.Products.Include(p => p.Seller).AsEnumerable()
                              .FirstOrDefault(p => p.Id == id && p.Seller.Email == email);

            if (product == null)
            {
                return(View("Error", new ErrorViewModel()
                {
                    RequestId = "404 Not Found."
                }));
            }
            return(View(product));
        }
示例#11
0
        public async Task <IActionResult> Index()
        {
            if (!User.IsInRole("Seller"))
            {
                return(RedirectToAction("RegisterAsSeller", "Account"));
            }

            await using Models.AppContext db = new Models.AppContext();
            string email = User.FindFirst(u
                                          => u.Type == ClaimTypes.Email) !.Value;
            var orders = db.Orders.Include(s => s.Seller)
                         ?.Where(o => o.Seller.Email == email)
                         .Include(o => o.Buyer)
                         .Include(o => o.Delivery)
                         .Include(o => o.ProductOrder)
                         .ThenInclude(po => po.Product).ToList();

            if (orders == null)
            {
                return(View(new List <Order>()));
            }

            return(View(orders));
        }