public IHALModelConfig GetConfig(object model) { var type = model.GetType(); // is it worth caching this? var classAttributes = type.GetTypeInfo().GetCustomAttributes(); foreach (var attribute in classAttributes) { var modelAttribute = attribute as HalModelAttribute; if (modelAttribute != null) { if (modelAttribute.ForceHal.HasValue || modelAttribute.LinkBase != null) { var config = new HALModelConfig(); if (modelAttribute.ForceHal.HasValue) { config.ForceHAL = modelAttribute.ForceHal.Value; } if (modelAttribute.LinkBase != null) { config.LinkBase = modelAttribute.LinkBase; } return(config); } } } return(null); }
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> GetRegistrations() { _logger.LogInformation("Vis registreringer"); var email = HttpContext.User.Claims.FirstOrDefault(c => c.Type.EndsWith("emailaddress"))?.Value; var user = await _userManager.FindByNameAsync(email); var data = _registrationService.GetAllByIdAsync(user.Id).ToList(); var result = data.Select(s => new RegistrationsModel { Id = s.Id, Date = s.Date, StartTime = s.StartTime, EndTime = s.EndTime.GetValueOrDefault() }); var responseConfig = new HALModelConfig { LinkBase = $"{Request.Scheme}://{Request.Host.ToString()}", ForceHAL = Request.ContentType == "application/hal+json" }; var response = new HALResponse(responseConfig); response.AddEmbeddedCollection("registrations", result); response.AddLinks( new Link("self", "/api/registration/", 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)); }
private static HALResponse ConvertInstance(object model, HttpContext context, HalcyonConventionOptions options) { //If this is called for a collection it will scan all the links for each item, but //each one needs to be customized to work anyway. //If the options provide a model, use that, otherwise get it from the resolver. IHALModelConfig halConfig; if (options.BaseUrl != null) { var pathBaseValue = ""; var pathBase = context.Request.PathBase; if (pathBase.HasValue) { //If we have a value, use that as the pathBaseValue, otherwise stick with the empty string. pathBaseValue = pathBase.Value; } var currentUri = new Uri(context.Request.GetDisplayUrl()); var host = $"{currentUri.Scheme}://{currentUri.Authority}{pathBaseValue}"; halConfig = new HALModelConfig() { LinkBase = options.BaseUrl.Replace(HalcyonConventionOptions.HostVariable, host), ForceHAL = false }; } else { halConfig = CustomHALAttributeResolver.GetConfig(model); } var response = new HALResponse(model, halConfig); response.AddLinks(CustomHALAttributeResolver.GetUserLinks(model, context, options.HalDocEndpointInfo)); var embeddedCollections = CustomHALAttributeResolver.GetEmbeddedCollectionValues(model) .Select(i => new KeyValuePair <String, IEnumerable <HALResponse> >(i.Key, GetEmbeddedResponses(i.Value, context, options))); response.AddEmbeddedCollections(embeddedCollections); return(response); }
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 IHALModelConfig GetConfig(object model) { var type = model.GetType(); // is it worth caching this? var classAttributes = type.GetTypeInfo().GetCustomAttributes(); foreach(var attribute in classAttributes) { var modelAttribute = attribute as HalModelAttribute; if(modelAttribute != null) { if(modelAttribute.ForceHal.HasValue || modelAttribute.LinkBase != null) { var config = new HALModelConfig(); if(modelAttribute.ForceHal.HasValue) { config.ForceHAL = modelAttribute.ForceHal.Value; } if(modelAttribute.LinkBase != null) { config.LinkBase = modelAttribute.LinkBase; } return config; } } } return null; }