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