Exemplo n.º 1
0
        public async Task <IEnumerable <BookRent> > GetBookRentsAsync(BookRentFilterViewModel bookRentFilterViewModel)
        {
            var filters = new List <Expression <Func <BookRent, bool> > >();

            if (!string.IsNullOrEmpty(bookRentFilterViewModel.BookFilter))
            {
                Expression <Func <BookRent, bool> > bookFilterExp = x =>
                                                                    x.Book.Title.Contains(bookRentFilterViewModel.BookFilter) ||
                                                                    x.Book.Author.Contains(bookRentFilterViewModel.BookFilter);
                filters.Add(bookFilterExp);
            }

            if (!string.IsNullOrEmpty(bookRentFilterViewModel.NameFilter))
            {
                Expression <Func <BookRent, bool> > nameFilterExp = x =>
                                                                    x.Customer.LastName.Contains(bookRentFilterViewModel.NameFilter) ||
                                                                    x.Customer.FirstName.Contains(bookRentFilterViewModel.NameFilter) ||
                                                                    x.Customer.Patronymic.Contains(bookRentFilterViewModel.NameFilter);
                filters.Add(nameFilterExp);
            }

            if (bookRentFilterViewModel.RentStatusFilter != RentStatusEnum.All)
            {
                Expression <Func <BookRent, bool> > statusFilterExp;
                if (bookRentFilterViewModel.RentStatusFilter == RentStatusEnum.InRent)
                {
                    statusFilterExp = x => x.FactEndRent == null;
                }
                else
                {
                    // RentStatusEnum.RentExpired
                    statusFilterExp = x => x.PlanEndRent < DateTime.Now && x.FactEndRent == null;
                }

                filters.Add(statusFilterExp);
            }

            IQueryable <BookRent> query = _bookRent.GetAll();

            if (filters.Count > 0)
            {
                Expression <Func <BookRent, bool> > resultFilter = filters.OrTheseFiltersTogether();
                return(await query.Where(resultFilter).ToListAsync());
            }

            return(await query.ToListAsync());
        }
 public async Task <IActionResult> List(BookRentFilterViewModel bookRentFilterViewModel) =>
 Ok(await _bookRentService.GetBookRentsAsync(bookRentFilterViewModel));