public async Task <PaginatedList <Club> > GetAsync(ClubQuery clubQuery) { var query = Context.Clubs .Include(c => c.Stadium) .AsQueryable(); if (clubQuery.SeasonId.HasValue) { query = query.Where(c => c.Squads .Any(s => s.SeasonId == clubQuery.SeasonId)); } var columnsMap = new Dictionary <string, Expression <Func <Club, object> > >() { ["id"] = c => c.Id, ["code"] = c => c.Code, ["name"] = c => c.Name, ["establishedYear"] = c => c.EstablishedYear, ["stadium"] = c => c.Stadium.Name }; query = query.Sort(clubQuery, columnsMap); return(await PaginatedList <Club> .CreateAsync(query, clubQuery.PageNumber, clubQuery.PageSize)); }
public async Task <IActionResult> GetClubs([FromQuery] ClubQuery clubQuery) { var clubs = await _clubService.GetAsync(clubQuery); var returnClubs = _mapper.Map <PaginatedList <ClubListDto> >(clubs); return(Ok(returnClubs)); }
public async Task GetClubs_WhenCalled_ReturnClubsFromDb() { var clubQuery = new ClubQuery { PageNumber = 1, PageSize = 10, SortBy = "name", IsSortAscending = true }; var expectedClub = new PaginatedList <Club> { Pagination = new Pagination { PageNumber = 1, PageSize = 10 }, Items = new List <Club>() { new Club { Id = 1 }, new Club { Id = 2 }, new Club { Id = 3 }, } }; _clubService.Setup(c => c.GetAsync(clubQuery)).ReturnsAsync(expectedClub); var result = await _clubsController.GetClubs(clubQuery); var okObjectResult = result as OkObjectResult; var okObjectResultValue = okObjectResult.Value as PaginatedList <ClubListDto>; Assert.That(result, Is.TypeOf <OkObjectResult>()); Assert.That(okObjectResultValue, Is.Not.Null); Assert.That(okObjectResultValue.Pagination.PageNumber, Is.EqualTo(1)); Assert.That(okObjectResultValue.Pagination.PageSize, Is.EqualTo(10)); Assert.That(okObjectResultValue.Items.Count(), Is.EqualTo(3)); }
public async Task GetAsync_WhenCalled_ClubsFromDb() { var clubQuery = new ClubQuery { PageNumber = 1, PageSize = 10, SortBy = "name", IsSortAscending = true }; var expectedClubs = new PaginatedList <Club> { Pagination = new Pagination { PageNumber = 1, PageSize = 10 }, Items = new List <Club>() { new Club { Id = 1 }, new Club { Id = 2 }, new Club { Id = 3 }, } }; _clubRepository.Setup(c => c.GetAsync(clubQuery)).ReturnsAsync(expectedClubs); var result = await _clubService.GetAsync(clubQuery); Assert.That(result, Is.Not.Null); Assert.That(result, Is.EqualTo(expectedClubs)); }
public async Task <PaginatedList <Club> > GetAsync(ClubQuery clubQuery) { return(await _clubRepository.GetAsync(clubQuery)); }