public async Task <BookPaginationDto> Find(int pageSize, int currentPage, Sort sort, string column, string authorId) { try { IEnumerable <Book> books = await _bookRepository.FindAsync(); // filter if (authorId != null) { books = books.Where(x => x.AuthorId == Guid.Parse(authorId)); } // sort if (column != null) { if (typeof(Book).GetProperty(column) == null) { string[] joinColumns = { "AuthorName" }; if (joinColumns.Contains(column)) { if (column.Equals("AuthorName")) { Func <Book, string> AuthorNameSelector() => x => x?.Author?.Name; books = DynamicSorting.SortJoinColumn <Book, string>(books, sort, AuthorNameSelector()); } } else { throw ErrorResponse.BadRequest(); } } else { books = DynamicSorting.SortColumn <Book>(books, sort, typeof(Book).GetProperty(column)); } } else { books = DynamicSorting.SortColumn(books, sort, typeof(Book).GetProperty("Id")); } // pagination int booksCount = books.Count(); books = Pagination.Paging <Book>(books, currentPage, pageSize); return(new BookPaginationDto { Total = booksCount, Books = _mapper.Map <IEnumerable <Book>, IEnumerable <BookDto> >(books), }); } catch (Exception ex) { await _unitOfWork.RollbackAsync(); throw ErrorResponse.InternalServerError(ex); } }
public async Task <AuthorPaginationDto> Find(int pageSize, int currentPage, Sort sort, string column) { try { IEnumerable <Author> authors = await _authorRepository.FindAsync(); // sort if (column != null) { if (typeof(Author).GetProperty(System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(column)) == null) { string[] joinColumns = { "TotalBooks" }; if (joinColumns.Contains(column)) { if (column.Equals("TotalBooks")) { Func <Author, int> TotalBookSelector() => x => x.Books.Count(); authors = DynamicSorting.SortJoinColumn <Author, int>(authors, sort, TotalBookSelector()); } } else { throw ErrorResponse.BadRequest(); } } else { authors = DynamicSorting.SortColumn <Author>(authors, sort, typeof(Author).GetProperty(System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(column))); } } else { authors = DynamicSorting.SortColumn(authors, sort, typeof(Author).GetProperty("Id")); } // pagination int authorCount = authors.Count(); authors = Pagination.Paging <Author>(authors, currentPage, pageSize); return(new AuthorPaginationDto() { Total = authorCount, Authors = _mapper.Map <IEnumerable <Author>, IEnumerable <AuthorDto> >(authors), }); } catch (Exception ex) { await _unitOfWork.RollbackAsync(); throw ErrorResponse.InternalServerError(ex); } }