public async Task <PagedList <ApplicationClient> > FindAllAsync(ApplicationClientResourceParameter resourceParameter) { if (resourceParameter is null) { throw new ArgumentNullException(nameof(resourceParameter)); } var collection = this.context.ApplicationClient as IQueryable <ApplicationClient>; if (resourceParameter.IsEnabled != null) { collection = collection.Where(ac => ac.Enabled == resourceParameter.IsEnabled); } if (!string.IsNullOrEmpty(resourceParameter.SearchQuery)) { var searchQuery = resourceParameter.SearchQuery.Trim(); collection = collection.Where(ac => ac.Name.Contains(searchQuery, StringComparison.OrdinalIgnoreCase) || ac.JWTAudienceCategory.Contains(searchQuery, StringComparison.OrdinalIgnoreCase)); } if (!string.IsNullOrEmpty(resourceParameter.OrderBy)) { var propertyMappingDictionary = this.propertyMappingService.GetPropertyMapping <ApplicationClientDto, ApplicationClient>(); collection = collection.ApplySort(resourceParameter.OrderBy, propertyMappingDictionary); } return(await PagedList <ApplicationClient> .CreateAsync( collection, resourceParameter.PageNumber, resourceParameter.PageSize)); }
private IEnumerable <LinkDto> CreateLinksForApplicationClients( ApplicationClientResourceParameter resourceParameters, bool hasNextPage, bool hasPreviousPage) { var links = new List <LinkDto>(); links.Add(new LinkDto( CreateApplicationClientResourceUri(resourceParameters, ResourceUriType.Current), "self", "GET")); if (hasNextPage) { links.Add(new LinkDto( CreateApplicationClientResourceUri(resourceParameters, ResourceUriType.NextPage), "next_page", "GET")); } if (hasPreviousPage) { links.Add(new LinkDto( CreateApplicationClientResourceUri(resourceParameters, ResourceUriType.PreviousPage), "previous_page", "GET")); } return(links); }
// GET: api/applicationclients public async Task <IActionResult> GetApplicationClients( [FromQuery] ApplicationClientResourceParameter resourceParameter, [FromHeader(Name = "Accept")] string mediaType) { var response = await this.businessLogic.GetApplicationClients(resourceParameter, mediaType); if (!response.IsSuccessful) { return(BadRequest(new { message = response.ErrorMessage })); } if (response.Result == null) { return(NotFound()); } Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(response.Result.PaginationMetaData)); return(Ok( new { value = response.Result.Value, links = response.Result.Links } )); }
private string CreateApplicationClientResourceUri( ApplicationClientResourceParameter resourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(url.Link("GetApplicationClients", new { fields = resourceParameters.Fields, orderBy = resourceParameters.OrderBy, pageNumber = resourceParameters.PageNumber - 1, pageSize = resourceParameters.PageSize, isEnabled = resourceParameters.IsEnabled, searchQuery = resourceParameters.SearchQuery })); case ResourceUriType.NextPage: return(url.Link("GetApplicationClients", new { fields = resourceParameters.Fields, orderBy = resourceParameters.OrderBy, pageNumber = resourceParameters.PageNumber + 1, pageSize = resourceParameters.PageSize, isEnabled = resourceParameters.IsEnabled, searchQuery = resourceParameters.SearchQuery })); case ResourceUriType.Current: default: return(url.Link("GetApplicationClients", new { fields = resourceParameters.Fields, orderBy = resourceParameters.OrderBy, pageNumber = resourceParameters.PageNumber, pageSize = resourceParameters.PageSize, isEnabled = resourceParameters.IsEnabled, searchQuery = resourceParameters.SearchQuery })); } }
public async void FindAll_3RecordsPageSizeIs2AndPageNumber2_Returns1ApplicationClients() { //Arrange var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase($"DatabaseForTesting{Guid.NewGuid()}") .Options; var resourceParameter = new ApplicationClientResourceParameter() { PageSize = 2, PageNumber = 2 }; using (var context = new ApplicationDbContext(options)) { context.ApplicationClient.Add(new ApplicationClient() { Name = "1", JWTAudienceCategory = "1.com" }); context.ApplicationClient.Add(new ApplicationClient() { Name = "1", JWTAudienceCategory = "1.com" }); context.ApplicationClient.Add(new ApplicationClient() { Name = "1", JWTAudienceCategory = "1.com" }); context.SaveChanges(); } using (var context = new ApplicationDbContext(options)) { var repository = new ApplicationClientRepository(context); // Act var clients = await repository.FindAllAsync(resourceParameter); // Assert Assert.Single(clients); }; }
public async Task <GenericResponse <ShapedDataWithLinks> > GetApplicationClients(ApplicationClientResourceParameter resourceParameter, string mediaType) { try { if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType)) { return(GenericResponseBuilder.NoSuccess <ShapedDataWithLinks>(null, "Wrong media type")); } if (!propertyMappingService.ValidMappingExistsFor <ApplicationClientDto, ApplicationClient>(resourceParameter.OrderBy)) { return(GenericResponseBuilder.NoSuccess <ShapedDataWithLinks>(null, "Wrong OrderBy entered")); } if (!propertyCheckerService.TypeHasProperties <ApplicationClientDto>(resourceParameter.Fields, true)) { return(GenericResponseBuilder.NoSuccess <ShapedDataWithLinks>(null, "Wrong fields entered")); } var applicationClients = await this.dataService.ApplicationClients.FindAllAsync(resourceParameter); if (applicationClients.Count == 0) { return(GenericResponseBuilder.Success <ShapedDataWithLinks>(null)); } var paginationMetaData = new PaginationMetaData() { TotalCount = applicationClients.TotalCount, PageSize = applicationClients.PageSize, CurrentPage = applicationClients.CurrentPage, TotalPages = applicationClients.TotalPages }; var links = CreateLinksForApplicationClients(resourceParameter, applicationClients.HasNext, applicationClients.HasPrevious); var shapedClientsToReturn = this.mapper .Map <IEnumerable <ApplicationClientDto> >(applicationClients) .ShapeData(resourceParameter.Fields); var includeLinks = parsedMediaType.SubTypeWithoutSuffix .EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase); var shapedClientsToReturnWithLinks = shapedClientsToReturn.Select(client => { var userAsDictionary = client as IDictionary <string, object>; if (includeLinks) { var userLinks = CreateLinksForApplicationClient((Guid)userAsDictionary["Id"], resourceParameter.Fields); userAsDictionary.Add("links", userLinks); } return(userAsDictionary); }); var applicationClientsToReturn = new ShapedDataWithLinks() { Value = shapedClientsToReturnWithLinks, Links = links, PaginationMetaData = paginationMetaData }; return(GenericResponseBuilder.Success <ShapedDataWithLinks>(applicationClientsToReturn)); } catch (Exception ex) { logger.LogError(string.Format("Error in BLL - GetApplicationClients. {0}", ex.Message)); return(GenericResponseBuilder.NoSuccess <ShapedDataWithLinks>(null, ex.Message.ToString())); } }