public bool AddItemToActiveCart(long userId, long productId, long quantity) { var activeCart = dbContext.Carts.FirstOrDefault(c => c.UserId == userId && c.IsCurrentCart == true); if (activeCart == null) { var newCart = new Cart { UserId = userId, IsCurrentCart = true }; this.dbContext.Carts.Add(newCart); this.dbContext.SaveChanges(); activeCart = newCart; } var product = dbContext.Products.FirstOrDefault(p => p.ProductId == productId); var cartProduct = dbContext.CartProducts.FirstOrDefault(cp => cp.CartId == activeCart.CartId && cp.ProductId == product.ProductId); if (cartProduct == null) { dbContext.CartProducts.Add(new CartProduct { CartId = activeCart.CartId, ProductId = product.ProductId, Quantity = (int)quantity }); } else { cartProduct.Quantity += (int)quantity; } return(dbContext.SaveChanges() > 0); }
public UserEntity AddUser(UserEntity userEntity) { UserDetail userDetail = new UserDetail() { FirstName = userEntity.UserDetails.FirstName, LastName = userEntity.UserDetails.LastName, Age = userEntity.UserDetails.Age, Sex = userEntity.UserDetails.Sex, ProfilePicture = userEntity.UserDetails.ProfilePicture }; dbContext.UserDetails.Add(userDetail); dbContext.SaveChanges(); var user = new User() { Username = userEntity.Username, Password = userEntity.Password, Email = userEntity.Email, UserDetailId = userDetail.UserDetailId }; dbContext.Users.Add(user); dbContext.SaveChanges(); return(user != null?UserMapper.UserEntityMapper(user) : null); }
public bool AddProductToWishlist(long userId, long productId) { var activeWishlist = dbContext.WishLists.FirstOrDefault(ws => ws.UserId == userId && ws.IsCurrent == true); if (activeWishlist == null) { var newWishlist = new WishList() { UserId = userId, IsCurrent = true }; dbContext.WishLists.Add(newWishlist); dbContext.SaveChanges(); activeWishlist = newWishlist; } else { var item = dbContext.WishListProducts .FirstOrDefault(w => w.WishListId == activeWishlist.WishListId && w.ProductId == productId); if (item != null) { return(true); } } var product = dbContext.Products.FirstOrDefault(p => p.ProductId == productId); if (product == null) { return(false); } var newProductWishlist = new WishListProduct() { WishListId = activeWishlist.WishListId, ProductId = product.ProductId }; this.dbContext.WishListProducts.Add(newProductWishlist); return(this.dbContext.SaveChanges() > 0); }