public async Task <IActionResult> Get([FromQuery] int pageSize = 10, [FromQuery] int pageIndex = 0) { var result = await _itemService.GetItemsAsync(); var totalItems = result.Count(); var itemsOnPage = result.OrderBy(c => c.Name) .Skip(pageSize * pageIndex) .Take(pageSize); var hateoasResults = new List <ItemHateoasResponse>(); foreach (var itemResponse in itemsOnPage) { var hateoasResult = new ItemHateoasResponse { Data = itemResponse }; await _linksService.AddLinksAsync(hateoasResult); hateoasResults.Add(hateoasResult); } var model = new PaginatedItemResponseModel <ItemHateoasResponse>( pageIndex, pageSize, totalItems, hateoasResults); return(Ok(model)); }
public async Task <TResource> CreateEmptyResourceAsync <TResource>(Action <TResource> beforeMap = null) where TResource : Resource, new() { var resource = new TResource(); beforeMap?.Invoke(resource); await _linksService.AddLinksAsync(resource); return(resource); }
public async Task <IEnumerable <Player> > Get() { var players = _allPlayers; foreach (var player in players) { await _linksService.AddLinksAsync(player); } return(players); }
public async Task <ItemsLinkContainer <ValueInfo> > Get() { var values = await GetAllValuesWithLinksAsync(); var result = new ItemsLinkContainer <ValueInfo>() { Items = values }; await linksService.AddLinksAsync(result); return(result); }
public async Task <IActionResult> GetById(Guid id) { ItemResponse result = await _itemService.GetItemAsync(id); HateoasResponse <ItemResponse> hateoasResult = new HateoasResponse <ItemResponse> { Data = result }; await _linksService.AddLinksAsync(new HateoasResponse <ItemResponse> { Data = result }); return(Ok(hateoasResult)); }
protected async Task AddLinksToCollectionAsync <T>(IEnumerable <T> linkContainers) where T : ILinkContainer { foreach (var linkContainer in linkContainers) { await _linksService.AddLinksAsync(linkContainer); } }
public static async Task <T> AddLinks <T>(this T linkContainer, ILinksService linksService) where T : ILinkContainer { await linksService.AddLinksAsync(linkContainer); return(linkContainer); }
public async Task <ApiResult <HateoasResponse <ItemResponse> > > Handle(GetAllItemsHateoasQuery request, CancellationToken cancellationToken) { IEnumerable <ItemResponse> itemList = await _itemService.GetItemsQuery().ToListAsync(); List <HateoasResponse <ItemResponse> > hateoasResults = new List <HateoasResponse <ItemResponse> >(); foreach (ItemResponse itemResponse in itemList) { HateoasResponse <ItemResponse> hateoasResult = new HateoasResponse <ItemResponse> { Data = itemResponse }; await _linksService.AddLinksAsync(hateoasResult); hateoasResults.Add(hateoasResult); } IQueryable <HateoasResponse <ItemResponse> > test = hateoasResults.AsQueryable(); ApiResult <HateoasResponse <ItemResponse> > pagedItems = await ApiResult <HateoasResponse <ItemResponse> > .CreateAsync( test, request.Data.PageIndex, request.Data.PageSize, request.Data.SortColumn, request.Data.SortOrder, request.Data.FilterColumn, request.Data.FilterQuery); return(pagedItems); }
/// <summary> /// Converts model to resource and adds links to it. /// This should give complete control of /// which links are applied at any point within your api code. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResource"></typeparam> /// <param name="source"></param> /// <returns></returns> /// <exception cref="InvalidOperationException"> /// </exception> public async Task <TResource> MapAsync <T, TResource>(T source) where TResource : Resource { try { var resource = _mapper.Map <TResource>(source); await _linksService.AddLinksAsync(resource); return(resource); } catch (Exception e) { throw new InvalidOperationException( "A problem occured while trying to convert a type to a resource. " + "Make sure that you have set up AutoMapper properly and your link policies don't " + "contain any unexisting routes.", e); } }
public async Task <ApiInfo> GetApiInfo() { var info = new ApiInfo() { Version = "v1.0" }; await linksService.AddLinksAsync(info); return(info); }
public static async Task <IEnumerable <T> > AddLinks <T>(this IEnumerable <T> enumerable, ILinksService linksService) where T : ILinkContainer { enumerable = enumerable.ToList(); foreach (var it in enumerable) { await linksService.AddLinksAsync(it); } return(enumerable); }
public async Task <ActionResult <LinkDto> > RequestPrimeCalculation(CheckNumberDto checkNumberDto) { _loggingManager.LogInformation($"RequestPrimeCalculation endpoint started. number: {checkNumberDto.Number}"); await Task.Run(() => { var command = _mediator.Send(new StartPrimeCalculationCommand { Number = checkNumberDto.Number }); }); var linkDto = new LinkDto { Id = checkNumberDto.Number.ToString() }; await _linksService.AddLinksAsync(linkDto); return(Ok(linkDto)); }
public static Task AddLinksAsync <T>(this ILinksService service, T linkContainer, ILinksPolicy /*<T>*/ policy) where T : ILinkContainer { if (service == null) { throw new ArgumentNullException(nameof(service)); } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } return(service.AddLinksAsync(linkContainer, policy.Requirements)); }
public static Task AddLinksAsync <T>(this ILinksService service, T linkContainer, ILinksRequirement requirement) where T : ILinkContainer { if (service == null) { throw new ArgumentNullException(nameof(service)); } if (requirement == null) { throw new ArgumentNullException(nameof(requirement)); } return(service.AddLinksAsync(linkContainer, new[] { requirement })); }
public async Task <ActionResult <Location> > GetAsync(int id) { try { Location location = new Location(); location.Id = id; await _linksService.AddLinksAsync(location); return(location); } catch (Exception ex) { throw ex; } }
[Links(Policy = "FullInfoPolicy")] //if you want to add parent with same service needs to have same policy name public async Task <Order> Get([FromRoute] Guid customerId, Guid id) { Fixture f = new Fixture(); var order = f.Build <Order>() .With(a => a.Id, id) .With(a => a.ForCustomerId, customerId) .Without(w => w.Links) .Create(); var customer = new Customer { Id = customerId }; await _linkService.AddLinksAsync(order); await _linkService.AddLinksAsync(customer); //if you add policy with the same name in this case FullInfoPolicy, //then this use self from order, so stupid its made for simple crud behaviour out of the box //but if you are explicit in the full info policy it all works nuts order.AddLink("parent", customer.Links["self"]); return(order); }
[HttpGet(Name = "GetAllValuesRoute")] //this can be done via range header as well public async Task <ItemsLinkContainer <Customer> > Get([FromQuery] int skip = 0) { const int pageSize = 5; var f = new Fixture(); var cust = f.Build <Customer>() .Without(w => w.Links) .CreateMany(56) .ToList(); foreach (var customer in cust) { await _linkService.AddLinksAsync(customer); } var result = new ItemsLinkContainer <Customer> { Items = cust.Skip(skip).Take(pageSize).ToList() }; await _linkService.AddLinksAsync(result); var self = result.Links["self"]; if (skip != 0) { result.AddLink("first", new Link { Href = $"{self.Href}", Rel = self.Rel, Method = HttpMethod.Get.ToString() }); } if (skip + pageSize > cust.Count) { result.AddLink("next", new Link { Href = $"{self.Href}?skip={skip + pageSize}", Rel = self.Rel, Method = HttpMethod.Get.ToString() }); } if (skip >= pageSize * 2) { result.AddLink("previous", new Link { Href = $"{self.Href}?skip={skip - pageSize}", Rel = self.Rel, Method = HttpMethod.Get.ToString() }); } result.AddLink("last", new Link { Href = $"{self.Href}?skip={cust.Count - (cust.Count % pageSize) }", Rel = self.Rel, Method = HttpMethod.Get.ToString() }); return(result); }