public async Task <IEnumerable <NaturalEvent> > GetAsync(NaturalEventsQuery query) { var client = httpClientFactory.CreateClient(HttpClientNames.NaturalEventsClient); var response = await client.GetAsync($"events?{query.ToQueryString()}"); var data = await JsonSerializer.DeserializeAsync <NaturalEventsCollection>(await response.Content.ReadAsStreamAsync()); IEnumerable <NaturalEvent> result; if (!string.IsNullOrEmpty(query.Sort)) { result = query.Sort == NaturalEventsQuery.OrderByAsc ? data.NaturalEvents.OrderBy(x => x.Title) : data.NaturalEvents.OrderByDescending(x => x.Title); } else { result = data.NaturalEvents; } return(result); }
public static string ToQueryString(this NaturalEventsQuery query) { var nv = HttpUtility.ParseQueryString(string.Empty); var properties = query.GetType().GetProperties().Where(x => x.GetMethod.IsPublic); foreach (var property in properties) { var customNameAttr = property.GetCustomAttribute(typeof(QueryPropertyNameAttribute)) as QueryPropertyNameAttribute; if (customNameAttr == null) { continue; } string name = customNameAttr?.Name ?? property.Name; var propValue = property.GetValue(query); if (propValue == null) { continue; } var value = ValueAccessors.TryGetValue(property.PropertyType, out Func <object, string> valueAccessor) ? valueAccessor(propValue) : propValue.ToString(); nv.Add(name, value); } return(nv.ToString()); }
public async Task <IActionResult> Get([FromQuery] NaturalEventsQuery query) { var data = await naturalEventsProvider.GetAsync(query); return(Ok(data)); }