//get products by pageNo and pageSize public List <Product> GetProducts(int pageNo, int pageSize) { using (var context = new eComContext()) { return(context.Products.OrderByDescending(p => p.Id).Skip((pageNo - 1) * pageSize).Take(pageSize).Include(p => p.Category).ToList()); } }
//get list of orders by status, pageNo public List <Order> GetOrders(string status, int pageNo, string date, int pageSize) { using (var context = new eComContext()) { IQueryable <Order> orders = context.Orders; if (!string.IsNullOrEmpty(date)) //linq-to-entities can't recognize convert datetime to satring then compare, so made this functioanlty. { var ordersList = new List <Order>(); foreach (var order in orders) { if (order.OrderedAt.ToString("y") == date) { ordersList.Add(order); } } orders = ordersList.AsQueryable(); } if (!string.IsNullOrEmpty(status)) { orders = orders.Where(o => o.Status == status); } if (string.IsNullOrEmpty(status) && string.IsNullOrEmpty(date)) { orders = orders.Where(o => o.Status == "Pending"); } return(orders.OrderBy(o => o.Id).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList()); } }
//get list of featured categoriess public List <Category> GetFeaturedCategories() { using (var context = new eComContext()) { return(context.Categories.Where(c => c.IsFeatured && c.ImageUrl != null).ToList()); } }
//get list of orders by userID public List <Order> GetOrdersByUserId(string userId, int pageNo, int pageSize) { using (var context = new eComContext()) { return(context.Orders.Where(o => o.UserId == userId).OrderByDescending(o => o.Id).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList()); } }
//get categories that have products public List <Category> GetFilledCategories() { using (var context = new eComContext()) { return(context.Categories.Include(c => c.Products).Where(c => c.Products.Count() > 0).ToList()); } }
//get category by id public Category GetCategory(int id) { using (var context = new eComContext()) { return(context.Categories.Where(c => c.Id == id).FirstOrDefault()); } }
//get wishlist by userId public Wishlist GetWishlist(string userId) { using (eComContext context = new eComContext()) { return(context.Wishlists.Where(w => w.UserId == userId).Include(w => w.WishlistItems).Include("WishlistItems.Product").FirstOrDefault()); } }
//get products by list of ids public List <Product> GetProducts(List <int> ids) { using (var context = new eComContext()) { return(context.Products.Where(p => ids.Contains(p.Id)).ToList()); } }
//get count of list of orders by userId public int GetOrdersCountByUserId(string userId) { using (var context = new eComContext()) { return(context.Orders.Where(o => o.UserId == userId).Count()); } }
//get count of list of orders public int GetOrdersCount(string status, string date) { using (var context = new eComContext()) { var orders = context.Orders.AsQueryable(); if (!string.IsNullOrEmpty(date)) { var ordersList = new List <Order>(); foreach (var order in orders) { if (order.OrderedAt.ToString("y") == date) { ordersList.Add(order); } } orders = ordersList.AsQueryable(); } if (!string.IsNullOrEmpty(status)) { orders = orders.Where(o => o.Status == status); } if (string.IsNullOrEmpty(status) && string.IsNullOrEmpty(date)) { orders = orders.Where(o => o.Status == "Pending"); } return(orders.Count()); } }
//get product by id public Product GetProduct(int id) { using (var context = new eComContext()) { return(context.Products.Where(c => c.Id == id).Include(p => p.Category).Include(p => p.Reviews).FirstOrDefault()); } }
public Config GetConfig(string key) { using (var context = new eComContext()) { return(context.Configurations.Find(key)); } }
//get latest products public List <Product> GetLatestProducts(int numberOfProducts) { using (var context = new eComContext()) { return(context.Products.OrderByDescending(p => p.Id).Take(numberOfProducts).Include(p => p.Category).Include(p => p.Reviews).ToList()); } }
//get reviews by productId public List <Review> GetReviews(int productId) { using (var context = new eComContext()) { return(context.Reviews.Where(r => r.ProductId == productId).ToList()); } }
//get all categoies public List <Category> GetCategories() { using (var context = new eComContext()) { return(context.Categories.Include(c => c.Products).ToList()); } }
//is there a wishlist by userId public bool IsExistWishlist(string userId) { using (eComContext context = new eComContext()) { var existing = context.Wishlists.Where(w => w.UserId == userId).Include(w => w.WishlistItems).FirstOrDefault(); return(existing != null); } }
//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 category public void SaveCategory(Category category) { using (var context = new eComContext()) { context.Categories.Add(category); context.SaveChanges(); } }
//is there a product public bool IsExistProduct(int productId, Wishlist existWishlist) { using (eComContext context = new eComContext()) { bool wishlist = existWishlist.WishlistItems.Exists(w => w.ProductId == productId); return(wishlist); } }
//create wishlist public bool SaveWishlist(Wishlist wishlist) { using (eComContext context = new eComContext()) { context.Wishlists.Add(wishlist); return(context.SaveChanges() > 0); } }
//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); } }
public int ShopPageSize() { using (var context = new eComContext()) { var pageSizeConfig = context.Configurations.Find("ShopPageSize"); return(pageSizeConfig.Value != string.Empty ? int.Parse(pageSizeConfig.Value) : 12); } }
//create a new review public bool SaveReview(Review newReview) { using (var context = new eComContext()) { context.Reviews.Add(newReview); return(context.SaveChanges() > 0); } }
//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(); } }
//delete product public void DeleteProduct(int id) { using (var context = new eComContext()) { var product = context.Products.Find(id); context.Products.Remove(product); 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(); } }
//get order by id public Order GetOrder(int id) { using (var context = new eComContext()) { return(context.Orders .Where(o => o.Id == id).Include(o => o.OrderItems) .Include("OrderItems.Product") .FirstOrDefault()); } }
//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(); } }