public IActionResult GetCategories(CategoriesParametersModel parameters)
        {
            if (parameters.Page < Configurations.DefaultPageValue)
            {
                return(Error(HttpStatusCode.BadRequest, "page", "Invalid page parameter"));
            }

            var allCategories = _categoryApiService.GetCategories(parameters.Ids, parameters.CreatedAtMin, parameters.CreatedAtMax,
                                                                  parameters.UpdatedAtMin, parameters.UpdatedAtMax,
                                                                  50000, parameters.Page, parameters.SinceId,
                                                                  parameters.ProductId, parameters.PublishedStatus)
                                .Where(c => StoreMappingService.Authorize(c));

            IList <CategoryDto> categoriesAsDtos = allCategories.Select(category =>
            {
                return(_dtoHelper.PrepareCategoryDTO(category));
            }).ToList();

            var categoriesRootObject = new CategoriesRootObject()
            {
                Categories = categoriesAsDtos
            };

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

            return(new RawJsonActionResult(json));
        }
        public async Task <IActionResult> GetCategories(CategoriesParametersModel 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"));
            }

            var allCategories = _categoryApiService.GetCategories(parameters.Ids, parameters.CreatedAtMin, parameters.CreatedAtMax,
                                                                  parameters.UpdatedAtMin, parameters.UpdatedAtMax,
                                                                  parameters.Limit, parameters.Page, parameters.SinceId,
                                                                  parameters.ProductId, parameters.PublishedStatus)
                                .WhereAwait(async c => await StoreMappingService.AuthorizeAsync(c));

            IList <CategoryDto> categoriesAsDtos = await allCategories.SelectAwait(async category => await _dtoHelper.PrepareCategoryDTOAsync(category)).ToListAsync();

            var categoriesRootObject = new CategoriesRootObject
            {
                Categories = categoriesAsDtos
            };

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

            return(new RawJsonActionResult(json));
        }
Exemple #3
0
        public void WhenSomeValidParametersPassed_ShouldCallTheServiceWithTheSameParameters()
        {
            var parameters = new CategoriesParametersModel();

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

            autoMocker.Get <ICategoryApiService>()
            .Expect(x => x.GetCategories(parameters.Ids,
                                         parameters.CreatedAtMin,
                                         parameters.CreatedAtMax,
                                         parameters.UpdatedAtMin,
                                         parameters.UpdatedAtMax,
                                         parameters.Limit,
                                         parameters.Page,
                                         parameters.SinceId,
                                         parameters.ProductId,
                                         parameters.PublishedStatus)).Return(new List <Category>());

            //Act
            autoMocker.ClassUnderTest.GetCategories(parameters);

            //Assert
            autoMocker.Get <ICategoryApiService>().VerifyAllExpectations();
        }
        public IHttpActionResult GetCategories(CategoriesParametersModel 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 <Category> allCategories = _categoryApiService.GetCategories(parameters.Ids, parameters.CreatedAtMin, parameters.CreatedAtMax,
                                                                               parameters.UpdatedAtMin, parameters.UpdatedAtMax,
                                                                               parameters.Limit, parameters.Page, parameters.SinceId,
                                                                               parameters.ProductId, parameters.PublishedStatus);

            IList <CategoryDto> categoriesAsDtos = allCategories.Select(category =>
            {
                CategoryDto categoryDto = category.ToDto();

                PrepareDtoAditionalProperties(category, categoryDto);

                return(categoryDto);
            }).ToList();

            var categoriesRootObject = new CategoriesRootObject()
            {
                Categories = categoriesAsDtos
            };

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

            return(new RawJsonActionResult(json));
        }
        public void WhenNoCategoriesExist_ShouldCallTheSerializerWithRootObjectWithoutCategories()
        {
            var parameters = new CategoriesParametersModel();

            //Arange
            _authMocker.Get <ICategoryApiService>().Stub(x => x.GetCategories()).Return(new List <Category>());

            //Act
            _authMocker.ClassUnderTest.GetCategories(parameters);

            //Assert
            _authMocker.Get <IJsonFieldsSerializer>().AssertWasCalled(
                x => x.Serialize(Arg <CategoriesRootObject> .Matches(r => r.Categories.Count == 0),
                                 Arg <string> .Is.Equal(parameters.Fields)));
        }
        public void WhenAnyFieldsParametersPassed_ShouldCallTheSerializerWithTheSameFields()
        {
            var parameters = new CategoriesParametersModel()
            {
                Fields = "id,name"
            };

            //Arange
            _authMocker.Get <ICategoryApiService>().Stub(x => x.GetCategories()).Return(new List <Category>());

            //Act
            _authMocker.ClassUnderTest.GetCategories(parameters);

            //Assert
            _authMocker.Get <IJsonFieldsSerializer>().AssertWasCalled(
                x => x.Serialize(Arg <CategoriesRootObject> .Is.Anything, Arg <string> .Is.Equal(parameters.Fields)));
        }
        public void WhenInvalidPageParameterPassed_ShouldReturnBadRequest(int invalidPage)
        {
            var parameters = new CategoriesParametersModel()
            {
                Page = invalidPage
            };

            //Arange
            _authMocker.Get <IJsonFieldsSerializer>().Stub(x => x.Serialize(Arg <CategoriesRootObject> .Is.Anything, Arg <string> .Is.Anything))
            .IgnoreArguments()
            .Return(string.Empty);

            //Act
            IActionResult result = _authMocker.ClassUnderTest.GetCategories(parameters);

            //Assert
            var statusCode = ActionResultExecutor.ExecuteResult(result);

            Assert.AreEqual(HttpStatusCode.BadRequest, statusCode);
        }
Exemple #8
0
        public void WhenInvalidPageParameterPassed_ShouldReturnBadRequest(int invalidPage)
        {
            var parameters = new CategoriesParametersModel()
            {
                Page = invalidPage
            };

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

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

            //Act
            IHttpActionResult result = autoMocker.ClassUnderTest.GetCategories(parameters);

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

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