Beispiel #1
0
        // GET: Books
        public async Task <IActionResult> Index(string searchauthor, int?searchagefrom, int?searchageto)
        {
            var authors = await authorsService.GetAll();

            var ages = authors
                       .OrderBy(x => x.Age)
                       .Select(x => x.Age);

            if (!String.IsNullOrEmpty(searchauthor))
            {
                authors = authors.Where(x => x.Name.Contains(searchauthor, StringComparison.InvariantCultureIgnoreCase)).ToList();
            }

            if (searchagefrom.HasValue && searchageto.HasValue)
            {
                authors = authors.Where(x => x.Age >= searchagefrom && x.Age <= searchageto).ToList();
            }

            var autorsVM = new AuthorViewModel
            {
                Authors = authors,
                Ages    = new SelectList(ages.Distinct())
            };

            return(View(autorsVM));
        }
Beispiel #2
0
        // GET: Books
        public async Task <IActionResult> Index(int?bookAutor, string searchString, int?bookDateTimeto, int?bookDateTimefrom)
        {
            var autorQuery = await authorsService.GetAll();

            var books = await booksService.GetAll();

            var dates = books
                        .OrderBy(x => x.ReleaseDate)
                        .Select(x => x.ReleaseDate.ToString("d"));

            if (!String.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.Title.Contains(searchString)).ToList();
            }


            if (bookDateTimeto.HasValue && bookDateTimefrom.HasValue)
            {
                books = books.Where(x => x.ReleaseDate.Year >= bookDateTimeto && x.ReleaseDate.Year <= bookDateTimefrom).ToList();
            }



            if (bookAutor.HasValue)
            {
                books = books.Where(x => x.Autor.Id == bookAutor).ToList();
            }

            var bookAutorVM = new BookAutorViewModel
            {
                Autors = new SelectList(autorQuery, nameof(Autor.Id), nameof(Autor.Name)),
                Dates  = new SelectList(dates.Distinct()),
                Books  = books
            };

            return(View(bookAutorVM));
        }