public async Task <IActionResult> GetRegistration([Required] int id) { _logger.LogInformation($"Vis detaljer for registrering {id}"); var data = await _registrationService.GetByIdAsync(id); var result = new RegistrationsModel { Id = data.Id, Date = data.Date, StartTime = data.StartTime, EndTime = data.EndTime.GetValueOrDefault() }; var responseConfig = new HALModelConfig { LinkBase = $"{Request.Scheme}://{Request.Host.ToString()}", ForceHAL = Request.ContentType == "application/hal+json" }; var response = new HALResponse(responseConfig); response.AddEmbeddedResource("registration", result); response.AddLinks( new Link("self", $"/api/registration/{id}", null, "GET") ); return(this.HAL(response)); }
public async Task <IActionResult> Exchange([FromBody] ExchangeRequest request) { _logger.LogInformation( $"Omregn beløbet {request.Amount} med fra valuta \"{request.FromCurrencyCode}\" til valuta \"{request.ToCurrencyCode}\""); if (!ModelState.IsValid) { return(BadRequest()); } var result = await _exchangeService.Exchange(request.Amount, request.FromCurrencyCode, request.ToCurrencyCode); var responseConfig = new HALModelConfig { LinkBase = $"{Request.Scheme}://{Request.Host.ToString()}", ForceHAL = Request.ContentType == "application/hal+json" }; var response = new HALResponse(responseConfig); response.AddEmbeddedResource("result", result); response.AddLinks( new Link("self", "/api/Exchange", null, "POST"), new Link("fromCurrency", $"/api/CurrencyRates/{request.FromCurrencyCode}"), new Link("toCurrency", $"/api/CurrencyRates/{request.ToCurrencyCode}") ); return(this.HAL(response)); }
public static HALResponse AddEmbeddedResource <T>( this HALResponse self, string resourceName, T model, Link link) { return(self.AddEmbeddedResource(resourceName, model, new[] { link })); }
public async Task <IActionResult> Get() { var invitations = await _gameInvitationService.All(); var responseConfig = new HALModelConfig { LinkBase = $"{Request.Scheme}://{Request.Host.ToString()}", ForceHAL = Request.ContentType == "application/hal+json" ? true : false }; var response = new HALResponse(responseConfig); response.AddLinks(new Link("self", "/GameInvitation"), new Link("confirm", "/GameInvitation/{id}/Confirm")); List <HALResponse> invitationsResponses = new List <HALResponse>(); foreach (var invitation in invitations) { var rInv = new HALResponse(invitation, responseConfig); rInv.AddLinks(new Link("self", "/GameInvitation/" + invitation.Id)); rInv.AddLinks(new Link("confirm", $"/GameInvitation/{invitation.Id}/confirm")); var invitedPlayer = _userService.GetUserByEmail(invitation.EmailTo); rInv.AddEmbeddedResource("invitedPlayer", invitedPlayer, new Link[] { new Link("self", $"/User/{invitedPlayer.Id}") }); var invitedBy = _userService.GetUserByEmail(invitation.InvitedBy); rInv.AddEmbeddedResource("invitedBy", invitedBy, new Link[] { new Link("self", $"/User/{invitedBy.Id}") }); invitationsResponses.Add(rInv); } response.AddEmbeddedCollection("invitations", invitationsResponses); return(this.HAL(response)); }
public void AddEmbeddedResources(HALResponse response, object modelValue, IHALModelConfig config) { response.AddLinks(this.GetLinks(modelValue)); var embeddedCollections = this.GetEmbeddedCollections(modelValue, config); foreach (var embedded in embeddedCollections) { if (embedded.IsCollection) { response.AddEmbeddedCollection(embedded.ResourceName, embedded.HALResponses); } else { response.AddEmbeddedResource(embedded.ResourceName, embedded.HALResponses.Single()); } } }