Exemplo n.º 1
0
        public IActionResult GetShoppingCartItemsByCustomerId(int customerId, ShoppingCartItemsForCustomerParametersModel parameters)
        {
            if (customerId <= Configurations.DefaultCustomerId)
            {
                return(Error(HttpStatusCode.BadRequest, "customer_id", "invalid customer_id"));
            }

            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,
                                                                                                          parameters.CreatedAtMin,
                                                                                                          parameters.CreatedAtMax, parameters.UpdatedAtMin,
                                                                                                          parameters.UpdatedAtMax, parameters.Limit,
                                                                                                          parameters.Page);

            if (shoppingCartItems == null)
            {
                return(Error(HttpStatusCode.NotFound, "shopping_cart_item", "not found"));
            }

            List <ShoppingCartItemDto> shoppingCartItemsDtos = shoppingCartItems
                                                               .Select(shoppingCartItem => _dtoHelper.PrepareShoppingCartItemDTO(shoppingCartItem))
                                                               .ToList();

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

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

            return(new RawJsonActionResult(json));
        }
        public void WhenIdIsPositiveNumberButNoSuchShoppingCartItemsExists_ShouldReturn404NotFound()
        {
            int nonExistingShoppingCartItemId = 5;
            var parameters = new ShoppingCartItemsForCustomerParametersModel();

            // Arange
            var autoMocker = new RhinoAutoMocker <ShoppingCartItemsController>();

            autoMocker.Get <IShoppingCartItemApiService>().Stub(x => x.GetShoppingCartItems(nonExistingShoppingCartItemId)).Return(null);

            autoMocker.Get <IJsonFieldsSerializer>().Stub(x => x.Serialize(Arg <ShoppingCartItemsRootObject> .Is.Anything, Arg <string> .Is.Anything))
            .IgnoreArguments()
            .Return(string.Empty);

            // Act
            IHttpActionResult result = autoMocker.ClassUnderTest.GetShoppingCartItemsByCustomerId(nonExistingShoppingCartItemId, parameters);

            // Assert
            var statusCode = result.ExecuteAsync(new CancellationToken()).Result.StatusCode;

            Assert.AreEqual(HttpStatusCode.NotFound, statusCode);
        }