public IHttpActionResult GetShoppingCartItems(ShoppingCartItemsParametersModel parameters)
        {
            if (parameters.Limit < Configurations.MinLimit || parameters.Limit > Configurations.MaxLimit)
            {
                return Error(HttpStatusCode.BadRequest, "limit", "invalid limit parameter");
            }

            if (parameters.Page < Configurations.DefaultPageValue)
            {
                return Error(HttpStatusCode.BadRequest, "page", "invalid page parameter");
            }

            IList<ShoppingCartItem> shoppingCartItems = _shoppingCartItemApiService.GetShoppingCartItems(customerId: null,
                                                                                                         createdAtMin: parameters.CreatedAtMin,
                                                                                                         createdAtMax: parameters.CreatedAtMax, 
                                                                                                         updatedAtMin: parameters.UpdatedAtMin,
                                                                                                         updatedAtMax: parameters.UpdatedAtMax, 
                                                                                                         limit: parameters.Limit,
                                                                                                         page: parameters.Page);

            List<ShoppingCartItemDto> shoppingCartItemsDtos = shoppingCartItems.Select(x => x.ToDto()).ToList();

            var shoppingCartsRootObject = new ShoppingCartItemsRootObject()
            {
                ShoppingCartItems = shoppingCartItemsDtos
            };

            var json = _jsonFieldsSerializer.Serialize(shoppingCartsRootObject, parameters.Fields);

            return new RawJsonActionResult(json);
        }
Exemplo n.º 2
0
        public void WhenCalledWithCreatedAtMinParameter_GivenSomeShoppingCartItemsCreatedAfterThatDate_ShouldReturnThemSortedById()
        {
            // Arange
            DateTime createdAtMinDate = _baseDate.AddMonths(5);

            // Ensure that the date will be in the collection because in the setup method we are using a random number to generate the dates.
            _existigShoppingCartItems.Add(new ShoppingCartItem()
            {
                Id           = _existigShoppingCartItems.Count + 1,
                CreatedOnUtc = createdAtMinDate
            });

            var expectedCollection =
                _existigShoppingCartItems.Where(x => x.CreatedOnUtc > createdAtMinDate).OrderBy(x => x.Id).Take(Configurations.DefaultLimit);

            var expectedProductsCount = expectedCollection.Count();

            // Act
            var shoppingCartItems = _shoppingCartItemsApiService.GetShoppingCartItems(createdAtMin: createdAtMinDate);

            // Assert
            CollectionAssert.IsNotEmpty(shoppingCartItems);
            Assert.AreEqual(expectedProductsCount, shoppingCartItems.Count);
            Assert.IsTrue(shoppingCartItems.Select(x => x.Id).SequenceEqual(expectedCollection.Select(x => x.Id)));
        }
        public IActionResult GetShoppingCartItems(ShoppingCartItemsParametersModel parameters)
        {
            if (parameters.Limit < Constants.Configurations.MinLimit || parameters.Limit > Constants.Configurations.MaxLimit)
            {
                return(Error(HttpStatusCode.BadRequest, "limit", "invalid limit parameter"));
            }

            if (parameters.Page < Constants.Configurations.DefaultPageValue)
            {
                return(Error(HttpStatusCode.BadRequest, "page", "invalid page parameter"));
            }

            IList <ShoppingCartItem> shoppingCartItems = _shoppingCartItemApiService.GetShoppingCartItems(null,
                                                                                                          parameters.CreatedAtMin,
                                                                                                          parameters.CreatedAtMax,
                                                                                                          parameters.UpdatedAtMin,
                                                                                                          parameters.UpdatedAtMax,
                                                                                                          parameters.Limit,
                                                                                                          parameters.Page);

            var shoppingCartItemsDtos = shoppingCartItems.Select(shoppingCartItem => { return(_dtoHelper.PrepareShoppingCartItemDTO(shoppingCartItem)); }).ToList();

            var shoppingCartsRootObject = new ShoppingCartItemsRootObject
            {
                ShoppingCartItems = shoppingCartItemsDtos
            };

            var json = JsonFieldsSerializer.Serialize(shoppingCartsRootObject, parameters.Fields);

            return(new RawJsonActionResult(json));
        }
Exemplo n.º 4
0
        public void WhenCalledWithLimitParameter_GivenMoreShoppingCartItemsThanTheLimit_ShouldReturnCollectionWithCountEqualToTheLimit()
        {
            //Arange
            var expectedLimit = 5;

            //Act
            var shoppingCartItems = _shoppingCartItemApiService.GetShoppingCartItems(limit: expectedLimit);

            // Assert
            CollectionAssert.IsNotEmpty(shoppingCartItems);
            Assert.AreEqual(expectedLimit, shoppingCartItems.Count);
        }
        public void WhenPassedPositiveCustomerId_GivenShoppingCartItemsForThisCustomer_ShouldReturnOnlyTheShoppingCartItemsForThisCustomerSortedById()
        {
            // Arange
            int customerId     = 5;
            var expectedResult = _shoppingCartItems.Where(x => x.CustomerId == customerId).OrderBy(x => x.Id).Take(Configurations.DefaultLimit);

            // Act
            var result = _shoppingCartItemApiService.GetShoppingCartItems(customerId);

            // Assert
            Assert.IsTrue(expectedResult.Select(x => new { x.Id, x.CustomerId })
                          .SequenceEqual(result.Select(x => new { x.Id, x.CustomerId })));
        }
Exemplo n.º 6
0
        public void WhenCalledWithPageParameter_ShouldReturnThePartOfTheCollectionThatCorrespondsToThePage()
        {
            //Arange
            var limit = 5;
            var page  = 6;
            var expectedCollection = new ApiList <ShoppingCartItem>(_existigShoppingCartItems.AsQueryable(), page - 1, limit);

            //Act
            var result = _shoppingCartItemApiService.GetShoppingCartItems(limit: limit, page: page);

            // Assert
            CollectionAssert.IsNotEmpty(result);
            Assert.AreEqual(expectedCollection.Count(), result.Count);
            Assert.IsTrue(result.Select(x => x.Id).SequenceEqual(expectedCollection.Select(x => x.Id)));
        }