Created
August 25, 2017 16:02
-
-
Save dontpaniclabsgists/eb19be187ca05a3042b8a4a4bc00c50b to your computer and use it in GitHub Desktop.
azure_functions_4_3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Shirt | |
{ | |
[JsonProperty(PropertyName = "num")] | |
public string Num { get; set; } | |
[JsonProperty(PropertyName = "imageurl")] | |
public string ImageUrl { get; set; } | |
} | |
public class Pant | |
{ | |
[JsonProperty(PropertyName = "num")] | |
public string Num { get; set; } | |
[JsonProperty(PropertyName = "imageurl")] | |
public string ImageUrl { get; set; } | |
} | |
public class Wardrobe | |
{ | |
[JsonProperty(PropertyName = "id")] | |
public string Id { get; set; } | |
[JsonProperty(PropertyName = "season")] | |
public string Season { get; set; } | |
[JsonProperty(PropertyName = "shirts")] | |
public Shirt[] Shirts { get; set; } | |
[JsonProperty(PropertyName = "pants")] | |
public Pant[] Pants { get; set; } | |
} | |
class WardrobeAccessor : IWardrobeAccessor | |
{ | |
const string DatabaseId = "whateverbillhasdb"; | |
const string CollectionId = "wardrobe"; | |
private string _endpoint; | |
private string _key; | |
public WardrobeAccessor(string endpoint, string key) | |
{ | |
_endpoint = endpoint; | |
_key = key; | |
} | |
public async Task<int> Count() | |
{ | |
return (await Wardrobes()).Length; | |
} | |
public async Task<Wardrobe[]> Wardrobes() | |
{ | |
using (var client = new DocumentClient(new Uri(_endpoint), _key)) | |
{ | |
var query = client.CreateDocumentQuery<Wardrobe>( | |
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), | |
new FeedOptions() { MaxItemCount = 100, EnableCrossPartitionQuery = true }) | |
.AsDocumentQuery(); | |
var results = new List<Wardrobe>(); | |
while (query.HasMoreResults) | |
{ | |
results.AddRange(await query.ExecuteNextAsync<Wardrobe>()); | |
} | |
return results.OrderBy(x => x.Id).ToArray(); | |
} | |
} | |
public async Task<Wardrobe> CurrentWardrobe() | |
{ | |
var items = await Wardrobes(); | |
return items.First(); | |
} | |
public async Task<Wardrobe> AddWardrobe(Wardrobe wardrobe) | |
{ | |
using (var client = new DocumentClient(new Uri(_endpoint), _key)) | |
{ | |
await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), wardrobe); | |
var items = await Wardrobes(); | |
return items.First(x => x.Id == wardrobe.Id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment