public IHttpActionResult AddToCart(string bookId, string userId) { int bookIdd = Convert.ToInt32(bookId); ShoppingCartListDto cartDto = null; if (bookIdd != 0) { var itemIdExcept = _itemRepository .All.Select(x => x.Id) .Except(_shoppingCartRepository .All.Select(x => x.ItemId)) .ToList(); if (itemIdExcept.Count > 0) { var cart = new ShoppingCart { UserId = userId, TotalItems = 1, DateCreated = DateTime.Now, ItemId = itemIdExcept.First() }; _shoppingCartRepository.Add(cart); } else { BadRequest("The Book has not items in stock"); } } _unitOfWork.Commit(); cartDto = GetCartDto(userId); return(Ok(cartDto)); }
public ShoppingCartListDto GetCartDto(string userId) { string cartId = userId;// Convert.ToString(HttpContext.Current.Session["userId"]); var cartItems = new List <CartItemDto>(); var carts = GroupBy(userId); int totalItems = 0; foreach (var cart in carts) { var itemId = cart.Value.Select(x => x.ItemId).FirstOrDefault(); var itemBook = _itemRepository.GetSingle(itemId); cartItems.Add(new CartItemDto { Title = itemBook.Book.Title, Author = itemBook.Book.Author, Price = itemBook.Price, Quantity = cart.Value.Count(), RecordId = cart.Key, }); totalItems += cart.Value.Count(); } var cartDto = new ShoppingCartListDto() { UserId = cartId, CartItems = cartItems, CartTotal = 15, Count = totalItems }; return(cartDto); }