예제 #1
0
        /// <summary>
        /// Create new cart
        /// </summary>
        /// <param name="websiteId"></param>
        /// <param name="userId"></param>
        /// <param name="cartCreateModel"></param>
        /// <returns></returns>
        public async Task CreateAsync(int websiteId, int userId, CartCreateModel cartCreateModel)
        {
            var cartRepository = unitOfWork.GetRepository <Cart>();
            var existedCart    = await cartRepository.FindByAsync(x => !x.DeletedDate.HasValue && x.WebsiteId == websiteId &&
                                                                  x.UserId == userId && x.ProductId == cartCreateModel.ProductId);

            if (existedCart != null)
            {
                existedCart.Quantity += cartCreateModel.Quantity;
                cartRepository.Update(existedCart);
                await unitOfWork.CommitAsync();

                return;
            }

            var productRepository = unitOfWork.GetRepository <Product>();
            var product           = await productRepository.FindByAsync(x => x.WebsiteId == websiteId &&
                                                                        x.Status == (int)Status.ACTIVE && x.Id == cartCreateModel.ProductId);

            if (product == null)
            {
                throw new NotFoundException(string.Format(
                                                MessageResponse.NotFoundError, nameof(Product), cartCreateModel.ProductId));
            }

            var cart = mapper.Map <Cart>(cartCreateModel);

            cart.WebsiteId   = websiteId;
            cart.UserId      = userId;
            cart.CreatedDate = DateTime.UtcNow;
            cartRepository.Add(cart);
            await unitOfWork.CommitAsync();
        }
예제 #2
0
        public void TestCreateAsync_WithValidData_ShouldNotThrowAnyException()
        {
            var cartCreateModel = new CartCreateModel
            {
                ProductId = 3,
                Quantity  = 5
            };

            Assert.DoesNotThrowAsync(() => cartService.CreateAsync(1, 1, cartCreateModel));
        }
예제 #3
0
        public void TestCreateAsync_WithNotFoundProduct_ShouldThrowNotFoundException()
        {
            var cartCreateModel = new CartCreateModel
            {
                ProductId = 5,
                Quantity  = 5
            };

            var ex = Assert.ThrowsAsync <NotFoundException>(() => cartService.CreateAsync(1, 1, cartCreateModel));

            Assert.AreEqual(string.Format(MessageResponse.NotFoundError, nameof(Product),
                                          cartCreateModel.ProductId), ex.Message);
        }
예제 #4
0
        public void TestCreateAsync_WithExistedCartHasSameProduct_ShouldUpdateQuantity()
        {
            var cartCreateModel = new CartCreateModel
            {
                ProductId = 1,
                Quantity  = 5
            };

            Assert.DoesNotThrowAsync(() => cartService.CreateAsync(1, 1, cartCreateModel));

            var existedCart = carts.FirstOrDefault(x => x.ProductId == cartCreateModel.ProductId);

            Assert.AreEqual(6, existedCart.Quantity);
        }
예제 #5
0
        public async Task <IActionResult> Post(CartCreateModel cartModel)
        {
            await this.cartService.CreateAsync(this.WebsiteId, this.UserId, cartModel);

            return(Ok(new ApiResponse <CartCreateModel>()));
        }