public PagedList <Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) { if (authorsResourceParameters == null) { throw new ArgumentNullException(nameof(authorsResourceParameters)); } //IQueryable - DEFERRED EXECUTION - Store command not a result var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory)) { var mainCategory = authorsResourceParameters.MainCategory.Trim(); collection = collection.Where(a => a.MainCategory == mainCategory); } if (!string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) { var searchQuery = authorsResourceParameters.SearchQuery.Trim(); collection = collection.Where(a => a.MainCategory.Contains(searchQuery) || a.FirstName.Contains(searchQuery) || a.LastName.Contains(searchQuery)); } if (!string.IsNullOrWhiteSpace(authorsResourceParameters.OrderBy)) { // Para evitamos multiple de combinaciones, implementamos LINQ DYNAMIC con los mapeos. //if(authorsResourceParameters.OrderBy.ToLowerInvariant() =="name") // collection = collection.OrderBy(a => a.FirstName).ThenBy(a => a.LastName); //LINQ DYNAMIC //Ejemplo: http://localhost:51044/api/authors/?orderBy=mainCategory, age desc // get property mapping dictionary var authorPropertyMappingDictionary = _propertyMappingService.GetPropertyMapping <Models.AuthorDto, Author>(); collection = collection.ApplySort(authorsResourceParameters.OrderBy, authorPropertyMappingDictionary); } return(PagedList <Author> .Create(collection, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
private IEnumerable <Link> CreateLinks(AuthorsResourceParameters authorsResourceParameters, bool hasNext, bool hasPrevious) { var links = new List <Link>(); links.Add(new Link(CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.Current), "self", "GET")); if (hasNext) { links.Add(new Link(CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.Next), "nextPage", "GET")); } if (hasNext) { links.Add(new Link(CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.Previous), "previousPage", "GET")); } return(links); }
public IActionResult GetAuthors(AuthorsResourceParameters authorsResourceParameters) { var authorsFromRepo = _repo.GetAuthors(authorsResourceParameters); var previousPageLink = authorsFromRepo.HasPrevious ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = authorsFromRepo.HasNext ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPages = authorsFromRepo.TotalPages, previousPageLink = previousPageLink, nextPageLink = nextPageLink }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); var authors = Mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo); //foreach (var author in authorsFromRepo) //{ // authors.Add(new AuthorDto() // { // Id = author.Id, // Name = $"{author.FirstName} {author.LastName}", // Genre = author.Genre, // Age = author.DateOfBirth.GetCurrentAge() // }); //} return(Ok(authors)); }
private string CreateAuthorsResourceUri(AuthorsResourceParameters authorsResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_urlHelper.Link("GetAuthors", new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber - 1, pageSize = authorsResourceParameters.PageSize })); case ResourceUriType.NextPage: return(_urlHelper.Link("GetAuthors", new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber + 1, pageSize = authorsResourceParameters.PageSize })); case ResourceUriType.Current: default: return(_urlHelper.Link("GetAuthors", new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber, pageSize = authorsResourceParameters.PageSize })); } }
//public IEnumerable<Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) public PagedList <Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) { //need to add the system.linq.dynamic.core to be able to convert the specific orderby into a string search /* * var collectionBeforePaging = _context.Authors * .OrderBy(a => a.FirstName) * .ThenBy(a => a.LastName).AsQueryable(); //add the as queryable so the filtering clause works */ //build extension method to accept sorting, with mapping service for resource mapping var collectionBeforePaging = _context.Authors.ApplySort( authorsResourceParameters.OrderBy, _propertyMappingService.GetPropertyMapping <AuthorDto, Author>() ); //For filtering.. if (!string.IsNullOrEmpty(authorsResourceParameters.Genre)) { //trim and ignore case var genreForWhereClause = authorsResourceParameters.Genre.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging.Where(c => c.Genre.ToLowerInvariant() == genreForWhereClause); } //searching (could use full search component like Lucene - will go basic here) if (!string.IsNullOrEmpty(authorsResourceParameters.SearchQuery)) { var searchQueryForWhereClause = authorsResourceParameters.SearchQuery.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging.Where(a => a.Genre.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.FirstName.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.LastName.ToLowerInvariant().Contains(searchQueryForWhereClause) ); } return(PagedList <Author> .Create(collectionBeforePaging, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); // .Skip((authorsResourceParameters.PageNumber - 1) * authorsResourceParameters.PageSize) // .Take(authorsResourceParameters.PageSize); }
public IActionResult GetAuthors(AuthorsResourceParameters authorsResourceParameters) { if (!_propertyMappingService.ValidMappingExistsFor <AuthorDto, Author>(authorsResourceParameters.OrderBy)) { return(BadRequest()); } if (authorsResourceParameters.Fields != null) { if (!_typeHelperService.TypeHasProperties <AuthorDto>(authorsResourceParameters.Fields)) { return(BadRequest()); } } var authorsFromRepo = _libraryRepository.GetAuthors(authorsResourceParameters); var previousPageLink = authorsFromRepo.HasPrevious ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = authorsFromRepo.HasNext ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPages = authorsFromRepo.TotalPages, previousPageLink = previousPageLink, nextPageLink = nextPageLink }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); var authors = Mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo); return(Ok(authors.ShapeData(authorsResourceParameters.Fields))); // return 200 if successful }
public PagedList <Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) { if (authorsResourceParameters == null) { throw new ArgumentNullException(nameof(authorsResourceParameters)); } //if (string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory) // && string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) // return GetAuthors(); var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory)) { var mainCategory = authorsResourceParameters.MainCategory.Trim(); collection = collection.Where(a => a.MainCategory == mainCategory); } if (!string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) { var searchQuery = authorsResourceParameters.SearchQuery.Trim(); collection = collection.Where(a => a.MainCategory.Contains(searchQuery) || a.FirstName.Contains(searchQuery) || a.LastName.Contains(searchQuery)); } if (!string.IsNullOrWhiteSpace(authorsResourceParameters.OrderBy)) { // get property mapping dictionary var authorPropertyMappingDictionary = _propertyMappingService.GetPropertyMapping <AuthorDto, Author>(); collection = collection.ApplySort(authorsResourceParameters.OrderBy, authorPropertyMappingDictionary); } return(PagedList <Author> .Create(collection, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
public ActionResult <IEnumerable <AuthorDto> > GetAuthors([FromQuery] AuthorsResourceParameters authorsResourceParameters)//[FromQuery] AuthorsResourceParameters authorsResourceParameters) //viene usato [FromQuery] perche il parametro authorsResourceParameters è un parametro complesso { var authorsFromRepo = _courseLibreryRepository.GetAuthors(authorsResourceParameters); //var authors = new List<AuthorDto>(); //foreach(var author in authorsFromRep) //{ // authors.Add(new AuthorDto() // { // Id= author.Id, // Name = $"{author.FirstName} {author.LastName}", // MainCategory = author.MainCategory, // age = author.DateOfBirth.GetCurrentAge() // } // ); //} return(Ok(_mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo))); }
public PagedList <Author> GetAuthors(AuthorsResourceParameters AuthrorsResourceParameter) { var collction = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(AuthrorsResourceParameter.MainCategory)) { collction = collction.Where(x => x.MainCategory == AuthrorsResourceParameter.MainCategory.Trim()); } if (!string.IsNullOrWhiteSpace(AuthrorsResourceParameter.SearchQuery)) { collction = collction.Where(x => x.MainCategory.Contains(AuthrorsResourceParameter.SearchQuery.Trim()) || x.LastName.Contains(AuthrorsResourceParameter.SearchQuery.Trim()) || x.FirstName.Contains(AuthrorsResourceParameter.SearchQuery.Trim())); } if (!string.IsNullOrEmpty(AuthrorsResourceParameter.OrderBy)) { var _mappingDictionary = _propertyMappingService.GetPropertyMapping <AuthorDto, Author>(); collction = collction.ApplySort(AuthrorsResourceParameter.OrderBy, _mappingDictionary); } return(PagedList <Author> .Create(collction, AuthrorsResourceParameter.PageNumber, AuthrorsResourceParameter.PageSize)); }
public PagedList <Author> GetAuthors(AuthorsResourceParameters parameters) { var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(parameters.MainCategory)) { parameters.MainCategory = parameters.MainCategory.Trim(); collection = collection.Where(x => x.MainCategory == parameters.MainCategory); } if (!string.IsNullOrWhiteSpace(parameters.SearchQuery)) { parameters.SearchQuery = parameters.SearchQuery.Trim(); collection = collection.Where(x => x.MainCategory.Contains(parameters.SearchQuery) || x.FirstName.Contains(parameters.SearchQuery) || x.LastName.Contains(parameters.SearchQuery)); } return(PagedList <Author> .Create(collection, parameters.PageNumber, parameters.PageSize)); }
private string CreateAuthorsResourceUrl( AuthorsResourceParameters authorsResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(Url.Link("GetAuthors", new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, pageNumber = authorsResourceParameters.PageNumber - 1, pageSize = authorsResourceParameters.PageSize, mainCategory = authorsResourceParameters.MainCategory, searchQuery = authorsResourceParameters.SearchQuery })); case ResourceUriType.NextPage: return(Url.Link("GetAuthors", new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, pageNumber = authorsResourceParameters.PageNumber + 1, pageSize = authorsResourceParameters.PageSize, mainCategory = authorsResourceParameters.MainCategory, searchQuery = authorsResourceParameters.SearchQuery })); default: return(Url.Link("GetAuthors", new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, pageNumber = authorsResourceParameters.PageNumber, pageSize = authorsResourceParameters.PageSize, mainCategory = authorsResourceParameters.MainCategory, searchQuery = authorsResourceParameters.SearchQuery })); } }
public async Task <PagedList <Author> > GetAuthorsAsync(AuthorsResourceParameters resourceParameters) { if (resourceParameters == null) { throw new ArgumentNullException(nameof(resourceParameters)); } // Cast the Authors db set as IQueryable<Author>. Can use for filters (Where) or searches as // required. Avail of deferred execution facility. var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(resourceParameters.MainCategory)) { var mainCategory = resourceParameters.MainCategory.Trim(); collection = _context.Authors.Where(a => a.MainCategory == mainCategory); } if (!string.IsNullOrWhiteSpace(resourceParameters.SearchQuery)) { var searchQuery = resourceParameters.SearchQuery.Trim(); collection = collection.Where(a => a.MainCategory.Contains(searchQuery) || a.FirstName.Contains(searchQuery) || a.LastName.Contains(searchQuery)); } // Implement default ordering if (!string.IsNullOrWhiteSpace(resourceParameters.OrderBy)) { // Get property mapping dictionary var authorPropertyMapping = propertyMappingService.GetPropertyMapping <AuthorDto, Author>(); // ApplySort - generic extension method on IQueryable available to all resources collection = collection.ApplySort(resourceParameters.OrderBy, authorPropertyMapping); } // Add paging after search and filtering var pagedList = await PagedList <Author> .CreateAsync(collection, resourceParameters.PageNumber, resourceParameters.PageSize); return(pagedList); }
public PagedList <Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) { // all of the items // var collectionBeforePaging = _context.Authors // .OrderBy(a => a.FirstName) // .ThenBy(a => a.LastName).AsQueryable(); var collectionBeforePaging = _context.Authors.ApplySort(authorsResourceParameters.OrderBy, _propertyMappingService.GetPropertyMapping <AuthorDto, Author>()); // take filters into account if (!string.IsNullOrEmpty(authorsResourceParameters.Genre)) { // trim & ignore casing var genreForWhereClause = authorsResourceParameters.Genre .Trim().ToLowerInvariant(); // apply filter collectionBeforePaging = collectionBeforePaging .Where(a => a.Genre.ToLowerInvariant() == genreForWhereClause); } // take search query into account if (!string.IsNullOrEmpty(authorsResourceParameters.SearchQuery)) { // trim & ignore casing var searchQueryForWhereClause = authorsResourceParameters.SearchQuery .Trim().ToLowerInvariant(); // apply search query collectionBeforePaging = collectionBeforePaging .Where(a => a.Genre.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.FirstName.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.LastName.ToLowerInvariant().Contains(searchQueryForWhereClause)); } // the items paged return(PagedList <Author> .Create(collectionBeforePaging, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
/// <summary> /// GetAuthors allows for returning a list of authors with filtering and searching /// </summary> /// <param name="mainCategory"></param> /// <param name="searchQuery"></param> /// <returns>List of Authors</returns> public PagedList <Author> GetAuthors( AuthorsResourceParameters authorsResourceParameters) { if (authorsResourceParameters == null) { throw new ArgumentNullException(nameof(authorsResourceParameters)); } var collection = _context.Authors as IQueryable <Author>; // allows us to use deferred execution if (!string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory)) { var mainCategory = authorsResourceParameters.MainCategory.Trim(); collection = collection.Where(a => a.MainCategory == mainCategory); } // Note: Our search will ONLY search the MainCategory, FirstName OR LastName // for the searchQuery string. It's not a full text search. if (!string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) { var searchQuery = authorsResourceParameters.SearchQuery.Trim(); collection = collection.Where( a => a.MainCategory.Contains(searchQuery) || a.FirstName.Contains(searchQuery) || a.LastName.Contains(searchQuery)); } if (!string.IsNullOrWhiteSpace(authorsResourceParameters.OrderBy)) { // get property mapping dictionary var authorPropertyMappingDictionary = _propertyMappingService.GetPropertyMapping <AuthorDTO, Author>(); collection = collection.ApplySort(authorsResourceParameters.OrderBy, authorPropertyMappingDictionary); } return(PagedList <Author> .Create(collection, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
/// <summary> /// Implimentation? Get a list of authors for a given category filter and/or search based on query criteria /// </summary> /// <param name="authorsResourceParameters">Variable list of author parameters</param> /// <returns>A list of authors in a given category</returns> public IEnumerable <Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) { //Check to see if value is null if (authorsResourceParameters == null) { throw new ArgumentNullException(nameof(authorsResourceParameters)); } //If everything is blank/not provided, return base if (string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory) && string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) { return(GetAuthors()); } /*IMPORTANT: * Notice we make it queryable for multiple criteria. * This is deferred execution and is most efficent method of querying data available. * This builds the query in memory based on the criteria provided and only executes a get on iteration. * Iteration meaning foreach, ToList, ToArray, ToDictionary, or with singletons */ var collection = _context.Authors as IQueryable <Author>; //filter the category if (!string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory)) { var mainCategory = authorsResourceParameters.MainCategory.Trim(); collection = collection.Where(author => author.MainCategory == mainCategory); } //search for the collection if (!string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) { var searchQuery = authorsResourceParameters.SearchQuery.Trim(); collection = collection.Where(author => author.MainCategory.Contains(searchQuery) || author.FirstName.Contains(searchQuery) || author.LastName.Contains(searchQuery) ); } return(collection.ToList <Author>()); }
private string CreateAuthorsResourceUri(AuthorsResourceParameters parameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_linkGenerator.GetUriByRouteValues(HttpContext, "GetAuthors", new { fields = parameters.Fields, orderBy = parameters.OrderBy, searchQuery = parameters.SearchQuery, genre = parameters.Genre, pageNumber = parameters.PageNumber - 1, pageSize = parameters.PageSize })); case ResourceUriType.NextPage: return(_linkGenerator.GetUriByRouteValues(HttpContext, "GetAuthors", new { fields = parameters.Fields, orderBy = parameters.OrderBy, searchQuery = parameters.SearchQuery, genre = parameters.Genre, pageNumber = parameters.PageNumber + 1, pageSize = parameters.PageSize })); case ResourceUriType.Current: default: return(_linkGenerator.GetUriByRouteValues(HttpContext, "GetAuthors", new { fields = parameters.Fields, orderBy = parameters.OrderBy, searchQuery = parameters.SearchQuery, genre = parameters.Genre, pageNumber = parameters.PageNumber, pageSize = parameters.PageSize })); } }
//public IActionResult GetAuthors([FromQuery(Name = "page")] int pageNumber = 1, // [FromQuery(Name = "size")] int pageSize = 5) public IActionResult GetAuthors(AuthorsResourceParameters authorsResourceParameters) { //pageSize = (pageSize > maxAuthorPageSize) ? maxAuthorPageSize : pageSize; //throw new Exception("Random exception for testing purpose!"); var authorsFromRepo = _libraryRepository.GetAuthors(authorsResourceParameters); var previousPageLink = authorsFromRepo.HasPrevious ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = authorsFromRepo.HasNext ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, tatalPages = authorsFromRepo.TotalPages, previousPageLink = previousPageLink, nextPageLink = nextPageLink, }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); //var authors = new List<AuthorDto>(); //foreach (var author in authorsFromRepo) //{ // authors.Add(new AuthorDto() // { // Id = author.Id, // Name = $"{author.FirstName} {author.LastName}", // Genre = author.Genre, // Age = author.DateOfBirth.GetCurrentAge() // }); //} var authors = Mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo); return(Ok(authors)); }
public IActionResult GetAuthors([FromQuery] AuthorsResourceParameters authorsResourceParameters) { if (!_propertyMappingService.ValidMappingExistsFor <AuthorDto, Entities.Author>(authorsResourceParameters.OrderBy)) { return(BadRequest()); } if (!_propertyCheckerService.TypeHasProperties <AuthorDto>(authorsResourceParameters.Fields)) { return(BadRequest()); } var authorsFromRepo = _courseLibraryRepository.GetAuthors(authorsResourceParameters); var previousPageLink = authorsFromRepo.HasPrevious ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = authorsFromRepo.HasNext ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPages = authorsFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata)); var links = CreateLinksForAuthors(authorsResourceParameters, authorsFromRepo.HasNext, authorsFromRepo.HasPrevious); var shapeAuthors = _mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo).ShapeData(authorsResourceParameters.Fields); var shapedAuthorsWithLink = shapeAuthors.Select(author => { var authorAsDictionary = author as IDictionary <string, object>; var authorLinks = CreateLinksForAuthor((Guid)authorAsDictionary["id"], null); authorAsDictionary.Add("links", authorLinks); return(authorAsDictionary); ; }); return(Ok(_mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo).ShapeData(authorsResourceParameters.Fields))); }
private string CreatePagingUri(AuthorsResourceParameters authorsResourceParameters, ResourceUriPagingType type) { return(type switch { ResourceUriPagingType.Next => this.Url.Link( "GetAuthors", new { authorsResourceParameters.Fields, authorsResourceParameters.MainCategory, authorsResourceParameters.SearchQuery, authorsResourceParameters.OrderBy, PageNumber = authorsResourceParameters.PageNumber + 1, authorsResourceParameters.PageSize }), ResourceUriPagingType.Prev => this.Url.Link( "GetAuthors", new { authorsResourceParameters.Fields, authorsResourceParameters.MainCategory, authorsResourceParameters.SearchQuery, authorsResourceParameters.OrderBy, PageNumber = authorsResourceParameters.PageNumber - 1, authorsResourceParameters.PageSize }), ResourceUriPagingType.Current => this.Url.Link( "GetAuthors", new { authorsResourceParameters.Fields, authorsResourceParameters.MainCategory, authorsResourceParameters.SearchQuery, authorsResourceParameters.OrderBy, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize }), _ => string.Empty });
private IEnumerable <LinkDto> CreateAuthorCollectionLinks(AuthorsResourceParameters authorsResourceParameters, bool hasNext, bool hasPrevious) { var links = new List <LinkDto> { // self new LinkDto(CreateAuthorResourceUri(authorsResourceParameters, ResourceUriType.CurrentPage), "self", "GET") }; if (hasNext) { links.Add(new LinkDto(CreateAuthorResourceUri(authorsResourceParameters, ResourceUriType.NextPage), "nextPage", "GET")); } if (hasPrevious) { links.Add(new LinkDto(CreateAuthorResourceUri(authorsResourceParameters, ResourceUriType.PreviousPage), "previousPage", "GET")); } return(links); }
private string CreateAuthorResourseUri( AuthorsResourceParameters resourceParameters, ResourseUriType type) { return(Url.Link("GetAuthors", new { fields = resourceParameters.Fields, orderBy = resourceParameters.OrderBy, mainCategory = resourceParameters.MainCategory, searchQuery = resourceParameters.SearchQuery, pageSize = resourceParameters.PageSize, pageNumber = type switch { ResourseUriType.PreviousPage => resourceParameters.PageNumber - 1, ResourseUriType.NextPage => resourceParameters.PageNumber + 1, //Current page _ => resourceParameters.PageNumber } }));
public PagedList <Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) { var collection = _context.Authors.ApplySort(authorsResourceParameters.OrderBy, _propertyMappingService.GetPropertyMapping <AuthorDto, Author>()); if (authorsResourceParameters.Genre != null) { var genre = authorsResourceParameters.Genre.Trim().ToLowerInvariant(); collection = collection.Where(a => a.Genre.ToLowerInvariant() == genre); } if (authorsResourceParameters.SearchQuery != null) { var searchQuery = authorsResourceParameters.SearchQuery.Trim().ToLowerInvariant(); collection = collection.Where(a => a.Genre.ToLowerInvariant().Contains(searchQuery) || a.FirstName.ToLowerInvariant().Contains(searchQuery) || a.LastName.ToLowerInvariant().Contains(searchQuery)); } return(PagedList <Author> .Create(collection, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
public IActionResult GetAuthors( [FromQuery] AuthorsResourceParameters authorsResourceParameters) { if (!_propertyMappingService.ValidMappingExistsFor <AuthorDto, Author> (authorsResourceParameters.OrderBy)) { return(BadRequest()); } if (!_propertyCheckerService.TypeHasProperties <AuthorDto> (authorsResourceParameters.Fields)) { return(BadRequest()); } var authorsFromRepo = _repositoryWrapper.Authors.GetAuthors(authorsResourceParameters); var previousPageLink = authorsFromRepo.HasPrevious ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = authorsFromRepo.HasNext ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPages = authorsFromRepo.TotalPages, previousPageLink, nextPageLink }; Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata)); return(Ok(_mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo) .ShapeData(authorsResourceParameters.Fields))); }
//TODO : 12 - Creo un metodo helper que me genra las url's private string CreateAuthorsResourceUri( AuthorsResourceParameters authorsResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_urlHelper.Link("GetAuthors", new { //TODO : 15 - Agrego a la uri el parametro se search y genere searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber - 1, pageSize = authorsResourceParameters.PageSize })); case ResourceUriType.NextPage: return(_urlHelper.Link("GetAuthors", new { searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber + 1, pageSize = authorsResourceParameters.PageSize })); default: return(_urlHelper.Link("GetAuthors", new { searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber, pageSize = authorsResourceParameters.PageSize })); } }
public PagedList <Author> GetAuthors(AuthorsResourceParameters authorsResource) { if (authorsResource == null) { throw new ArgumentNullException(nameof(authorsResource)); } //IQueryable<Author> queryableAuthors; var collection = _context.Authors as IQueryable <Author>; // Cast operation if (!string.IsNullOrWhiteSpace(authorsResource.MainCategory)) //filter { var mainCategory = authorsResource.MainCategory.Trim(); collection = collection.Where(c => c.MainCategory == mainCategory); } if (!string.IsNullOrWhiteSpace(authorsResource.SearchQuery)) //search { var searchQuery = authorsResource.SearchQuery.Trim(); collection = collection.Where(c => c.FirstName.Contains(searchQuery) || c.LastName.Contains(searchQuery) || c.MainCategory.Contains(searchQuery)); } if (!string.IsNullOrWhiteSpace(authorsResource.OrderBy)) { if (authorsResource.OrderBy.ToLower() == "name") { collection = collection.OrderBy(c => c.FirstName).ThenBy(c => c.LastName); } } var pagedSource = PagedList <Author> .Create(collection, authorsResource.PageNumber, authorsResource.PageSize); return(pagedSource); //// paging //collection = collection.Skip(authorsResource.PageSize * (authorsResource.PageNumber - 1)) // .Take(authorsResource.PageSize); //return await collection.ToListAsync(); }
public PagedList <Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) { var collectionBeforePaging = _context.Authors.ApplySort(authorsResourceParameters.OrderBy, _propertyMappingService.GetPropertyMapping <AuthorDto, Author>()); if (!string.IsNullOrEmpty(authorsResourceParameters.Genre)) { var genreForWhereClause = authorsResourceParameters.Genre.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging.Where(a => a.Genre.ToLowerInvariant() == genreForWhereClause); } if (!string.IsNullOrEmpty(authorsResourceParameters.SearchQuery)) { var searchQueryForWhereClause = authorsResourceParameters.SearchQuery.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(a => a.Genre.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.FirstName.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.LastName.ToLowerInvariant().Contains(searchQueryForWhereClause)); } return(PagedList <Author> .Create(collectionBeforePaging, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
[HttpHead] // head is like get only is not return the body public ActionResult <IEnumerable <AuthorDto> > GetAuthors([FromQuery] AuthorsResourceParameters authorsResourceParameters) { var authorsFromRepo = _courseLibraryRepository.GetAuthors(authorsResourceParameters); /*var authors = new List<AuthorDto>(); * * foreach(var author in authorsFromRepo) * { * authors.Add(new AuthorDto() * { * Id = author.Id, * Name = $"{author.FirstName} {author.LastName}", * MainCategory = author.MainCategory, * Age = author.DateOfBirth.GetCurrentAge() * }); * * }*/ return(Ok(_mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo))); }
public IActionResult Get(AuthorsResourceParameters page) { var result = _libraryRepository.Paginate(page.PageNumber, page.PageSize); var authors = result.Select(a => new AuthorGetModel(a)); var previousPageLink = CreateAuthorResourcesUri(page, ResourceUriType.PreviousPage); var nextPageLink = CreateAuthorResourcesUri(page, ResourceUriType.NextPage); var metadata = new { totalCount = result.TotalCount, pageSize = result.PageSize, currentPage = result.CurrentPage, totalPages = result.TotalPages, previous = previousPageLink, next = nextPageLink }; Response.Headers.Add("X-Paginatoin", JsonConvert.SerializeObject(metadata)); return(Ok(authors)); }
public PageList <Author> GetAuthors(AuthorsResourceParameters authorsResourceParameters) { var collectionBeforePaging = _context.Authors .OrderBy(a => a.FirstName) .OrderBy(a => a.LastName); if (!string.IsNullOrEmpty(authorsResourceParameters.Genre)) { string genre = authorsResourceParameters.Genre.Trim().ToLowerInvariant(); collectionBeforePaging = (IOrderedQueryable <Author>)collectionBeforePaging.Where(item => item.Genre.ToLowerInvariant() == genre); } if (!string.IsNullOrEmpty(authorsResourceParameters.SearchQuery)) { string searchQuery = authorsResourceParameters.SearchQuery.Trim().ToLowerInvariant(); var collection = (IOrderedQueryable <Author>)collectionBeforePaging.Where(item => item.Genre.ToLowerInvariant().Contains(searchQuery) || item.FirstName.ToLowerInvariant().Contains(searchQuery) || item.LastName.ToLowerInvariant().Contains(searchQuery)).AsQueryable(); } return(PageList <Author> .Create(collectionBeforePaging, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
public Uri CreateAuthorsResourceUri(AuthorsResourceParameters authorsResourceParameters, ResourceUriType type) { if (authorsResourceParameters == null) { return(null); } return(new Uri(type switch { ResourceUriType.PreviousPage => _urlHelper.Link(ApiNames.GetAuthors, new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber - 1, pageSize = authorsResourceParameters.PageSize }), ResourceUriType.NextPage => _urlHelper.Link(ApiNames.GetAuthors, new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber + 1, pageSize = authorsResourceParameters.PageSize }), _ => _urlHelper.Link(ApiNames.GetAuthors, new { fields = authorsResourceParameters.Fields, orderBy = authorsResourceParameters.OrderBy, searchQuery = authorsResourceParameters.SearchQuery, genre = authorsResourceParameters.Genre, pageNumber = authorsResourceParameters.PageNumber, pageSize = authorsResourceParameters.PageSize }), }));