public async Task <IActionResult> GetAllPeople([FromQuery] PeopleParamsDto peopleParams) { try { var peoplePaged = await _repo.GetAllPeople(peopleParams); if (peoplePaged == null) { return((IActionResult)NotFound()); } Response.AddPagination(peoplePaged.CurrentPage, peoplePaged.PageSize, peoplePaged.TotalCount, peoplePaged.AllPages); return(Ok(peoplePaged.AsEnumerable())); } catch (Exception) { return(StatusCode(500)); } }
public async Task <PagedList <Person> > GetAllPeople(PeopleParamsDto peopleParams) { var users = _context.People.AsQueryable(); // Filtrowanie if (!String.IsNullOrEmpty(peopleParams.FirstName)) { users = users.Where(x => x.FirstName == peopleParams.FirstName); } if (!String.IsNullOrEmpty(peopleParams.LastName)) { users = users.Where(x => x.LastName == peopleParams.LastName); } if (!String.IsNullOrEmpty(peopleParams.Occupation)) { users = users.Where(x => x.Occupation == peopleParams.Occupation); } // Sortowanie switch (peopleParams.OrderBy) { case "firstName": users = users.OrderBy(x => x.FirstName); break; case "firstNameDescending": users = users.OrderByDescending(x => x.FirstName); break; case "lastName": users = users.OrderBy(x => x.LastName); break; case "lastNameDescending": users = users.OrderByDescending(x => x.LastName); break; } return(await PagedList <Person> .CreateAsync(users, peopleParams.PageNumber, peopleParams.PageSize)); }
public async Task <IActionResult> GetAllPeopleCsv([FromQuery] PeopleParamsDto peopleParams) { try { var peoplePaged = await _repo.GetAllPeople(peopleParams); if (peoplePaged == null) { return((IActionResult)NotFound()); } var peoplePagedCsvString = CsvConverter.ToCsv(peoplePaged); var peoplePagedCsv = System.Text.Encoding.UTF8.GetBytes(peoplePagedCsvString); string fileName = "people.csv"; return(File(peoplePagedCsv, "text/csv", fileName)); } catch (Exception) { return(StatusCode(500)); } }