public async Task <ActionResult <CartModel> > AddBookToCart(Guid cartId, [FromBody] AddBookToCartCommand command)
        {
            command.CartId = cartId;
            var cart = await _mediator.Send(command);

            return(Ok(cart));
        }
Пример #2
0
        public async Task <CartModel> AddBookToCart(AddBookToCartCommand command)
        {
            var url = command.CartId == null
                ? "carts/empty/books"
                : $"carts/{command.CartId.Value}/books";

            using var response = await _httpClient.PostAsJsonAsync(url, command);

            var cart = await SafelyReadFromJsonAsync <CartModel>(
                response,
                HttpStatusCode.Created,
                HttpStatusCode.OK);

            cart.Id.Should().NotBeEmpty();
            if (command.CartId == null)
            {
                cart.Books.Should().HaveCount(1);
            }
            else
            {
                cart.Id.Should().Be(command.CartId.Value);
                cart.Books.Should().HaveCountGreaterOrEqualTo(1);
            }
            return(cart);
        }
        public async Task <ActionResult <CartModel> > AddBookToEmptyCart([FromBody] AddBookToCartCommand command)
        {
            var cart = await _mediator.Send(command);

            var url = Url.Action(nameof(GetCart), new { cartId = cart.Id });

            return(Created(url, cart));
        }
        public async Task Step3__Customer_Adds_Books_To_Cart()
        {
            var firstBook           = _selectedBooks.First();
            var addFirstBookCommand = new AddBookToCartCommand
            {
                BookId = firstBook.Id,
                CartId = null
            };
            var cart = await Client.AddBookToCart(addFirstBookCommand);

            cart.Id.Should().NotBeEmpty();
            var expectedCart = new CartModel
            {
                Id    = cart.Id,
                Books = new List <BookModel> {
                    firstBook
                }
            };

            cart.Should().BeEquivalentTo(expectedCart);
            var subsequentBooks = _selectedBooks.Skip(1);

            foreach (var book in subsequentBooks)
            {
                var addSubsequentBookCommand = new AddBookToCartCommand
                {
                    BookId = book.Id,
                    CartId = cart.Id
                };
                cart = await Client.AddBookToCart(addSubsequentBookCommand);
            }
            // Testing idempotency just in case UI does something weird
            cart = await Client.AddBookToCart(new AddBookToCartCommand
            {
                BookId = firstBook.Id,
                CartId = cart.Id,
            });

            var expectedFinalCart = new CartModel
            {
                Id    = cart.Id,
                Books = _selectedBooks
            };

            cart.Should().BeEquivalentTo(expectedFinalCart);
            _cart = cart;
        }
        public async Task AddBookToCartCommandTest_Test()
        {
            var bookCarts1 = new BookCarts
            {
                Id           = 10,
                UserId       = 1,
                BookId       = 1,
                Status       = (int)BookStatus.InOrder,
                ModifiedDate = DateTime.Now
            };

            _dbContext.Set <BookCarts>().Add(bookCarts1);

            var bookCarts2 = new BookCarts
            {
                Id           = 20,
                UserId       = 1,
                BookId       = 2,
                Status       = (int)BookStatus.Waiting,
                ModifiedDate = DateTime.Now
            };

            _dbContext.Set <BookCarts>().Add(bookCarts2);

            await _dbContext.SaveChangesAsync();

            var bookCartRepository = new EfRepository <BookCarts>(_dbContext);

            var        addBookToCartCommand = new AddBookToCartCommand(bookCartRepository, _httpContextAccessor);
            List <int> bookIds = new List <int>()
            {
                3, 4
            };
            var result = await addBookToCartCommand.ExecuteAsync(bookIds);

            Assert.True(result.Succeeded);
        }