public object Login([FromBody] loginModel value) { try { var userData = _context.Users.Where(x => x.Email == value.email).FirstOrDefault(); if (userData != null) { var productCount = _context.ProductCart.Where(x => x.UserId == userData.UserId).Select(x => x.ProductCount).ToList().Sum(); var returnUser = new loginModel { UId = userData.UserId, email = userData.Email, fullName = userData.FullName, photoUrl = userData.PhotoUrl, cartCount = productCount }; return(returnUser); } else { var user = new Users { Email = value.email, FullName = value.fullName, FName = value.fName, LName = value.lName, PhotoUrl = value.photoUrl, MobileNo = null }; _context.Users.Add(user); _context.SaveChanges(); var newUserId = _context.Users.Where(x => x.Email == value.email).Select(x => x.UserId).FirstOrDefault(); var productCart = new ProductCart { UserId = newUserId, ProductCount = 0, CartProductId = "0" }; _context.ProductCart.Add(productCart); _context.SaveChanges(); var returnUser = new loginModel { UId = newUserId, email = value.email, fullName = value.fullName, photoUrl = value.photoUrl, cartCount = 0 }; return(returnUser); } } catch (DbUpdateConcurrencyException) { throw; } }
public async Task <ActionResult <ProductCart> > PostProductCart(ProductCart productCart) { try { var productCount = _context.ProductCart.Where(x => x.UserId == productCart.UserId && x.CartProductId == productCart.CartProductId).Select(x => x.ProductCount).FirstOrDefault(); if (productCount != 0) { var CartId = _context.ProductCart.Where(x => x.UserId == productCart.UserId && x.CartProductId == productCart.CartProductId).Select(x => x.Id).FirstOrDefault(); var pCart = await _context.ProductCart.FindAsync(CartId); productCount = productCount + 1; pCart.ProductCount = productCount; _context.ProductCart.Update(pCart); _context.SaveChanges(); return(pCart); } else { var pCart = new ProductCart { UserId = productCart.UserId, CartProductId = productCart.CartProductId, ProductCount = 1 }; _context.ProductCart.Add(pCart); _context.SaveChanges(); return(pCart); } } catch (DbUpdateConcurrencyException) { throw; } }