예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        public ActionResult Create()
        {
            BooksStoreDBContext db = new BooksStoreDBContext();
            Book b = new Book();

            b.ReleaseDate    = DateTime.Now;
            ViewBag.EditMode = false;
            return(View(b));
        }
예제 #4
0
        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);
        }
예제 #5
0
        public static SelectList GetcategoriesList(int?selectedId)
        {
            BooksStoreDBContext db = new BooksStoreDBContext();

            if (selectedId.HasValue)
            {
                return(new SelectList(db.Categories.OrderBy(a => a.Name), "CategoryId", "Name", selectedId.Value));
            }
            else
            {
                return(new SelectList(db.Categories.OrderBy(a => a.Name), "CategoryId", "Name"));
            }
        }
예제 #6
0
        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);
        }
예제 #7
0
        public static SelectList GetAuthorList(int?selectedId)
        {
            BooksStoreDBContext db = new BooksStoreDBContext();

            if (selectedId.HasValue)
            {
                return(new SelectList(db.Authors.OrderBy(a => a.Name), "AuthorId", "Name", selectedId.Value));
            }
            else
            {
                return(new SelectList(db.Authors.OrderBy(a => a.Name), "AuthorId", "Name"));
            }
        }
예제 #8
0
        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());
        }
예제 #9
0
 private decimal CalculateTotalPrice(string userName)
 {
     using (BooksStoreDBContext db = new BooksStoreDBContext())
     {
         if (db.CartItems.Any(c => c.UserName == userName))
         {
             return(db.CartItems.Where(c => c.UserName == userName).Sum(c => c.Book.Price * c.Count));
         }
         else
         {
             return(0);
         }
     }
 }
예제 #10
0
 public int GetItemsCount(string userName)
 {
     using (BooksStoreDBContext db = new BooksStoreDBContext())
     {
         if (db.CartItems.Any(c => c.UserName == userName))
         {
             return(db.CartItems.Where(c => c.UserName == userName).Sum(c => c.Count));
         }
         else
         {
             return(0);
         }
     }
 }
예제 #11
0
        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();
        }
예제 #12
0
        public CartDetailsViewModel GetCartItems(string userName)
        {
            BooksStoreDBContext      db        = new BooksStoreDBContext();
            List <CartItemViewModel> itemsList = new List <CartItemViewModel>();
            CartDetailsViewModel     cartVM    = new CartDetailsViewModel();

            foreach (CartItem item in db.CartItems.Where(c => c.UserName == userName))
            {
                itemsList.Add(ConvertEntityToVM(item));
            }
            cartVM.CartItems       = itemsList;
            cartVM.TotalBooksCount = itemsList.Count;
            cartVM.TotalItemsCount = this.GetItemsCount(userName);
            cartVM.TotalPrice      = CalculateTotalPrice(userName);
            return(cartVM);
        }
예제 #13
0
        public ActionResult Edit(Book book)
        {
            BooksStoreDBContext db = new BooksStoreDBContext();

            ViewBag.EditMode = true;
            if (ModelState.IsValid)
            {
                BooksService service = new BooksService();
                //HttpPostedFileBase file = Request.Files.Count > 0 ? Request.Files.Get(0) : null;
                //string path = Server.MapPath("~/images/" + book.Title.Replace(' ', '_') + "_img.png");
                //service.SaveBook(book, file, path);
                service.SaveBook(book);
                ViewBag.Message = "Książka zapisana pomyślnie";
            }
            return(View("Create", book));
        }
예제 #14
0
        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);
        }
예제 #15
0
        public ActionResult Create(Book book)
        {
            BooksStoreDBContext db = new BooksStoreDBContext();

            ViewBag.EditMode = false;
            if (ModelState.IsValid)
            {
                BooksService       service = new BooksService();
                HttpPostedFileBase file    = Request.Files.Count > 0 ? Request.Files.Get(0) : null;
                string             path    = Server.MapPath("~/images/" + book.Title.Replace(' ', '_') + "_img.png");
                service.SaveBook(book, file, path);
                ViewBag.Message = "Książka zapisana pomyślnie";
                return(View(new Book {
                    ReleaseDate = DateTime.Now
                }));
            }
            else
            {
                return(View(book));
            }
        }
예제 #16
0
        public List <Order> GetUserOrders(string userName)
        {
            BooksStoreDBContext db = new BooksStoreDBContext();

            return(db.Orders.Where(o => o.Username == userName).ToList());
        }
예제 #17
0
        public ActionResult Details(int id)
        {
            BooksStoreDBContext db = new BooksStoreDBContext();

            return(new ViewAsPdf(db.Orders.Include("OrderDetails").FirstOrDefault(o => o.OrderId == id)));
        }