Ejemplo n.º 1
0
        public void Should_ReturnOneBook_Given_SubcategoryId()
        {
            // Arrange.
            var expectedResult = 2;
            var sut            = new BookService(_dbContext, null);
            var model          = new BookSearchServiceModel
            {
                SubcategoryId = 1
            };

            // Act.
            var returnedResult = sut.Find(model).NumberOfBooks;

            // Assert.
            Assert.AreEqual(expectedResult, returnedResult);
        }
Ejemplo n.º 2
0
        public void Should_ReturnOneBook_Given_PublishDate()
        {
            // Arrange.
            var expectedResult = 1;
            var sut            = new BookService(_dbContext, null);
            var model          = new BookSearchServiceModel
            {
                PublishDate = new DateTime(2015, 9, 22)
            };

            // Act.
            var returnedResult = sut.Find(model).NumberOfBooks;

            // Assert.
            Assert.AreEqual(expectedResult, returnedResult);
        }
Ejemplo n.º 3
0
        public void Should_ReturnOneBook_Given_PartialDescription()
        {
            // Arrange.
            var expectedResult = 1;
            var sut            = new BookService(_dbContext, null);
            var model          = new BookSearchServiceModel
            {
                Description = "concurrent Java"
            };

            // Act.
            var returnedResult = sut.Find(model).NumberOfBooks;

            // Assert.
            Assert.AreEqual(expectedResult, returnedResult);
        }
Ejemplo n.º 4
0
        public void Should_ReturnOneBook_Given_FullAuthorName()
        {
            // Arrange.
            var expectedResult = 1;
            var sut            = new BookService(_dbContext, null);
            var model          = new BookSearchServiceModel
            {
                Author = "R. B. Whitaker"
            };

            // Act.
            var returnedResult = sut.Find(model).NumberOfBooks;

            // Assert.
            Assert.AreEqual(expectedResult, returnedResult);
        }
Ejemplo n.º 5
0
        public void Should_ReturnTwoBooks_Given_PartialBookTitle()
        {
            // Arrange.
            var expectedResult = 2;
            var sut            = new BookService(_dbContext, null);
            var model          = new BookSearchServiceModel
            {
                Title = "C#"
            };

            // Act.
            var returnedResult = sut.Find(model).NumberOfBooks;

            // Assert.
            Assert.AreEqual(expectedResult, returnedResult);
        }
Ejemplo n.º 6
0
        public JsonResult GetBooks(BookSearchViewModel searchModel)
        {
            int itemPerPage = Int32.Parse(ConfigurationManager.AppSettings["PageSize"]);

            var searchServiceModel = new BookSearchServiceModel()
            {
                Author        = searchModel.Author,
                Description   = searchModel.Description,
                ISBN          = searchModel.ISBN,
                PublishDate   = searchModel.PublishDate,
                Title         = searchModel.Title,
                CategoryId    = searchModel.CategoryId,
                SubcategoryId = searchModel.SubcategoryId,
                ItemPerPage   = itemPerPage,
                PageNumber    = searchModel.PageNumber,
            };

            var findResult = _bookService.Find(searchServiceModel);
            var booksList  = findResult.Books.Select(b => new BookViewModel
            {
                Id          = b.Id,
                ISBN        = b.ISBN,
                Title       = b.Title,
                PublishDate = b.PublishDate.ToShortDateString(),
                FrontCover  = Url.Content(b.FrontCover),
                Authors     = b.Authors.Select(a =>
                                               string.Join(" ", a.FirstName, (a.MiddleName ?? ""), a.LastName)),
                Categories = b.SubCategories.Select(sc => new CategoryViewModel
                {
                    Category    = sc.Category.Name,
                    SubCategory = sc.Name
                }),
                Description = b.Description,
                BookLink    = Url.RouteUrl("Default", new { controller = "BookDetails", action = "Show", id = b.Id })
            }).ToList();


            var bookListViewModel = new BooksListViewModel()
            {
                NumberOfPages = (int)Math.Ceiling((double)findResult.NumberOfBooks / itemPerPage),
                Books         = booksList,
            };

            return(Json(bookListViewModel, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 7
0
        public BookPaginationModel Find(BookSearchServiceModel model)
        {
            // Find by basic fields.
            var books = _dbContext.Books
                        .WhereIf(model.PublishDate != null, b => b.PublishDate == model.PublishDate)
                        .WhereIf(model.ISBN != null, b => b.ISBN.Contains(model.ISBN));

            // Find by title.
            if (model.Title != null)
            {
                string[] words        = model.Title.Split(' ');
                var      booksByTitle = _dbContext.Books.Where(b => words.Any(w => b.Title.Contains(w)));
                books = books.Intersect(booksByTitle);
            }

            // Find by description.
            if (model.Description != null)
            {
                string[] words = model.Description.Split(' ');
                var      booksByDescription = _dbContext.Books.Where(b => words.Any(w => b.Description.Contains(w)));
                books = books.Intersect(booksByDescription);
            }

            // Find by authors.
            if (model.Author != null)
            {
                string[] words = model.Author.Split(' ');

                var booksByAuthors = _dbContext.Authors
                                     .Where(a => words.Any(w =>
                                                           (a.FirstName != null && a.FirstName.Contains(w)) ||
                                                           (a.MiddleName != null && a.MiddleName.Contains(w)) ||
                                                           (a.LastName != null && a.LastName.Contains(w))))
                                     .SelectMany(a => a.Books);

                books = booksByAuthors.Intersect(books);
            }

            // Find by categories.
            if (model.CategoryId != null && model.SubcategoryId == null)
            {
                var booksByCategory = (from c in _dbContext.Categories
                                       join sc in _dbContext.SubCategories.Include(sc => sc.Books)
                                       on c.Id equals sc.CategoryId
                                       where c.Id == model.CategoryId
                                       select sc.Books)
                                      .SelectMany(lb => lb)
                                      .Distinct();

                books = booksByCategory.Intersect(books);
            }

            // Find by subcategories.
            if (model.SubcategoryId != null)
            {
                var booksBySubcategory = _dbContext.SubCategories
                                         .Include(sc => sc.Books)
                                         .Where(sc => sc.Id == model.SubcategoryId)
                                         .SelectMany(sc => sc.Books)
                                         .Distinct();

                books = booksBySubcategory.Intersect(books);
            }

            var result = new BookPaginationModel();

            // Set the number of all books.
            result.NumberOfBooks = books.Count();

            // Take books of a specific page.
            var foundBooks = books.OrderBy(b => b.Id)
                             .Skip((model.PageNumber - 1) * model.ItemPerPage)
                             .Take(model.ItemPerPage)
                             .ToList();

            result.Books = foundBooks;

            return(result);
        }