public Book FindById(int id) { Book book = db.Books.FirstOrDefault(b => b.BookId == id); book.VisitCounter++; db.SaveChanges(); return(book); }
public CartDetailsViewModel RemoveBookFromCart(string userName, int bookId) { BooksStoreDBContext db = new BooksStoreDBContext(); CartDetailsViewModel cartVM = new CartDetailsViewModel(); cartVM.CartItems = new List <CartItemViewModel>(); CartItem item = db.CartItems.FirstOrDefault(c => c.UserName == userName && c.BookId == bookId); if (item != null) { item.Count--; item.Book.Count++; cartVM.CartItems.Add(ConvertEntityToVM(item)); Hubs.HubAccessor.Instance.UpdateBookCounter(bookId, item.Book.Count.Value); if (item.Count == 0) { db.CartItems.Remove(item); } db.SaveChanges(); } cartVM.TotalBooksCount = db.CartItems.Count(c => c.UserName == userName); cartVM.TotalItemsCount = this.GetItemsCount(userName); cartVM.TotalPrice = CalculateTotalPrice(userName); return(cartVM); }
public CartViewModel AddBookToCart(int bookId, string userName, int count = 1) { using (BooksStoreDBContext db = new BooksStoreDBContext()) { CartItem item = db.CartItems.FirstOrDefault(c => c.BookId == bookId && c.UserName == userName); if (item == null) { item = new CartItem { BookId = bookId, UserName = userName, DateCreated = DateTime.Now, Count = count }; db.CartItems.Add(item); } else { item.Count += count; } db.SaveChanges(); } CartViewModel cartVM = new CartViewModel(); cartVM.BooksCount = DecrementBookCount(bookId, count); cartVM.ItemsCount = GetItemsCount(userName); return(cartVM); }
public void CreateOrder(string userName, Order order) { BooksStoreDBContext db = new BooksStoreDBContext(); order.Username = userName; order.OrderDate = DateTime.Now; db.Orders.Add(order); db.SaveChanges(); CreateOrderFormCart(userName, order, db); }
public int AddAuthor(string name) { Author a = db.Authors.FirstOrDefault(au => au.Name == name); if (a == null) { a = new Author(); a.Name = name; db.Authors.Add(a); db.SaveChanges(); } return(a.AuthorId); }
private int DecrementBookCount(int bookId, int value = 1) { int res = 0; using (BooksStoreDBContext db = new BooksStoreDBContext()) { Book book = db.Books.FirstOrDefault(b => b.BookId == bookId); book.Count -= value; res = book.Count.Value; db.SaveChanges(); } return(res); }
public int AddCategory(string name) { Category c = db.Categories.FirstOrDefault(cat => cat.Name == name); if (c == null) { c = new Category(); c.Name = name; db.Categories.Add(c); db.SaveChanges(); } return(c.CategoryId); }
public ActionResult Contact() { ViewBag.Message = "Your contact page."; BooksStoreDBContext db = new BooksStoreDBContext(); var books = db.Books.Where(b => b.Description == null); foreach (var b in books) { db.Books.Remove(b); } db.SaveChanges(); return(View()); }
public void ClearCart(string userName, bool isOrder = false) { BooksStoreDBContext db = new BooksStoreDBContext(); IEnumerable <CartItem> items = db.CartItems.Where(c => c.UserName == userName); if (!isOrder) { foreach (CartItem c in items) { c.Book.Count += c.Count; Hubs.HubAccessor.Instance.UpdateBookCounter(c.BookId, c.Book.Count.Value); } } db.CartItems.RemoveRange(items); db.SaveChanges(); }
private void CreateOrderFormCart(string userName, Order order, BooksStoreDBContext db) { CartService service = new CartService(); CartDetailsViewModel cartVM = service.GetCartItems(userName); order.Total = cartVM.TotalPrice; List <OrderDetail> orderDetails = new List <OrderDetail>(); foreach (CartItemViewModel item in cartVM.CartItems) { OrderDetail detail = ConvertCartItemToOrderDetails(item); detail.OrderId = order.OrderId; db.OrdersDetails.Add(detail); } db.SaveChanges(); service.ClearCart(userName, true); }