예제 #1
0
        public void GetAllCategoriesTest()
        {
            var sut = new CatalogBll(this.catalog.Object, this.converter.Object);

            var actual = sut.GetAllCategories();

            this.catalog.Verify(
                cat => cat.GetAllCategories(),
                Times.Exactly(1));

            Assert.That(actual, Is.All.InstanceOf(typeof(CategoryDTO)));
            Assert.AreEqual(categories.Count, actual.Count());
        }
예제 #2
0
        public IHttpActionResult GetCategoriesByPage([FromBody] SearchDataModel searchDataModel)
        {
            int totalCount = 0;

            try
            {
                IEnumerable <CategoryDTO> categories = catalog.GetAllCategories();
                if (!string.IsNullOrEmpty(searchDataModel.SearchString))
                {
                    categories = categories.Where(category => category.Name.ToLower()
                                                  .Contains(searchDataModel.SearchString.ToLower()));
                }

                totalCount = categories.Count();
                categories = searchDataModel.Sort ? categories
                             .OrderByDescending(name => name.Name) : categories.OrderBy(name => name.Name);

                if (searchDataModel.Page == 1 && searchDataModel.PageSize == 0)
                {
                    categories = categories.ToList();
                }
                else
                {
                    categories = categories.Skip((searchDataModel.Page - 1) * searchDataModel.PageSize)
                                 .Take(searchDataModel.PageSize)
                                 .ToList();
                }

                var temp = new { items = categories, totalCount = totalCount };
                return(Ok(temp));
            }
            catch (ArgumentNullException ex)
            {
                var message = $"Categories collection is empty. {ex.Message}";
                return(BadRequest(message));
            }
            catch (Exception ex)
            {
                return(this.BadRequest(ex.Message));
            }
        }