-
Notifications
You must be signed in to change notification settings - Fork 0
/
CelestialObjectsFunction.cs
63 lines (56 loc) · 2.56 KB
/
CelestialObjectsFunction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Cosmos;
using StarLog.Entities;
using System.Collections.Generic;
using StarLog.Models;
using AutoMapper;
using System;
using System.Threading.Tasks;
namespace StarLog.Function
{
public static class CelestialObjectsFunction
{
private static string URL = Environment.GetEnvironmentVariable("AzureCosmosEndpoint");
private static string KEY = Environment.GetEnvironmentVariable("AzureCosmosKey");
private static MapperConfiguration _mapperConfig = new MapperConfiguration(cfg =>
cfg.CreateMap<CelestialObject, CelestialObjectModel>()
.ForMember(dest => dest.Declination, opts => opts.MapFrom(src => src.Dec))
.ForMember(dest => dest.RightAscension, opts => opts.MapFrom(src => src.RA))
.ForMember(dest => dest.CommonNames, opts =>
opts.MapFrom(src =>
src.CommonNames.Length > 0
? src.CommonNames.Split(",", System.StringSplitOptions.None)
: null as string[]
)));
[FunctionName("CelestialObjects")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
ILogger log)
{
string searchTerm = ((string)req.Query["search"])?.ToUpper();
string queryString = "SELECT TOP 5 * FROM c WHERE CONTAINS(UPPER(c.Name), @term) OR CONTAINS(UPPER(c[\"Common names\"]), @term)";
QueryDefinition queryDef = new QueryDefinition(queryString)
.WithParameter("@term", searchTerm);
CosmosClient cosmosClient = new CosmosClient(URL, KEY);
var iterator = cosmosClient
.GetDatabase("StarLog")
.GetContainer("CelestialObjects")
.GetItemQueryIterator<CelestialObject>(queryDef);
List<CelestialObjectModel> results = new List<CelestialObjectModel>();
while(iterator.HasMoreResults)
{
var result = await iterator.ReadNextAsync();
var mapper = _mapperConfig.CreateMapper();
foreach(var item in result.Resource)
{
results.Add(mapper.Map<CelestialObjectModel>(item));
}
}
return new OkObjectResult(results);
}
}
}