//create order with creating list of orderItems { orderId , productId } public int SaveOrder(Order order) { using (var context = new eComContext()) { context.Orders.Add(order); return(context.SaveChanges()); } }
//edit config value public bool EditConfig(Config config) { using (var context = new eComContext()) { context.Entry(config).State = EntityState.Modified; return(context.SaveChanges() > 0); } }
//create category public void SaveCategory(Category category) { using (var context = new eComContext()) { context.Categories.Add(category); context.SaveChanges(); } }
//edit category public void UpdateCategory(Category category) { using (var context = new eComContext()) { context.Entry(category).State = EntityState.Modified; context.SaveChanges(); } }
//edit product public void UpdateProduct(Product product) { using (var context = new eComContext()) { context.Entry(product).State = EntityState.Modified; context.SaveChanges(); } }
//create wishlist public bool SaveWishlist(Wishlist wishlist) { using (eComContext context = new eComContext()) { context.Wishlists.Add(wishlist); return(context.SaveChanges() > 0); } }
//create a new review public bool SaveReview(Review newReview) { using (var context = new eComContext()) { context.Reviews.Add(newReview); return(context.SaveChanges() > 0); } }
//delete review public void RemoveReview(int id) { using (var context = new eComContext()) { var review = context.Reviews.Where(r => r.Id == id).First(); context.Reviews.Remove(review); context.SaveChanges(); } }
//delete category public void DeleteCategory(int id) { using (var context = new eComContext()) { var category = context.Categories.Find(id); context.Categories.Remove(category); context.SaveChanges(); } }
//delete product public void DeleteProduct(int id) { using (var context = new eComContext()) { var product = context.Products.Find(id); context.Products.Remove(product); context.SaveChanges(); } }
//add product public void SaveProduct(Product product) { using (var context = new eComContext()) { context.Entry(product.Category).State = EntityState.Unchanged; //prevent adding category again with different id context.Products.Add(product); context.SaveChanges(); } }
//Empty wishlistItems by userId public bool EmptyWishlistItems(string userId) { using (eComContext context = new eComContext()) { var wishlist = context.Wishlists.Where(w => w.UserId == userId).Include(w => w.WishlistItems).FirstOrDefault(); context.WishlistItems.RemoveRange(wishlist.WishlistItems); return(context.SaveChanges() > 0); } }
//edit wishlist public bool AddWishlistItem(List <WishlistItem> WishlistItems) { using (eComContext context = new eComContext()) { foreach (var wishItem in WishlistItems) { context.WishlistItems.Add(wishItem); } return(context.SaveChanges() > 0); } }
//edit order status (by admin) public bool UpdateOrderStatus(string status, int id) { using (var context = new eComContext()) { var order = context.Orders.Where(o => o.Id == id).FirstOrDefault(); order.Status = status; context.Entry(order).State = EntityState.Modified; return(context.SaveChanges() > 0); } }
//update main picture in home page (by admin) public bool UpdateMainPicture(string key, string imageUrl) { using (var context = new eComContext()) { var existConfig = context.Configurations.Where(c => c.Key == key).FirstOrDefault(); if (existConfig != null) { existConfig.Value = imageUrl; context.Entry(existConfig).State = EntityState.Modified; return(context.SaveChanges() > 0); } return(false); } }
//remove product from wishlist public bool RemoveItemFromWishlist(string userId, int productId) { using (eComContext context = new eComContext()) { var wishlistItems = context.WishlistItems.Where(wi => wi.Wishlist.UserId == userId).ToList(); foreach (var wishItem in wishlistItems) { if (wishItem.ProductId == productId) { context.WishlistItems.Remove(wishItem); return(context.SaveChanges() > 0); } } return(false); } }