Skip to content

Instantly share code, notes, and snippets.

@dontpaniclabsgists
Created August 23, 2017 15:17
Show Gist options
  • Save dontpaniclabsgists/62fb1c04402e90430657e929a1744d8e to your computer and use it in GitHub Desktop.
Save dontpaniclabsgists/62fb1c04402e90430657e929a1744d8e to your computer and use it in GitHub Desktop.
Cosmos1_10
class NotesAccessor
{
const string CollectionId = "notes";
private string _endpoint;
private string _key;
private string _databaseId;
public NotesAccessor(string endpoint, string key, string databaseId)
{
_endpoint = endpoint;
_key = key;
_databaseId = databaseId;
}
public async Task<int> Count()
{
return (await All()).Length;
}
public async Task<Note[]> All()
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentCollectionUri(_databaseId, CollectionId);
var query = client.CreateDocumentQuery<Note>(
link,
new FeedOptions()
{
EnableCrossPartitionQuery = true
})
.AsDocumentQuery();
var results = new List<Note>();
while (query.HasMoreResults)
{
results.AddRange(await query.ExecuteNextAsync<Note>());
}
return results.OrderBy(x => x.Id).ToArray();
}
}
public async Task<Note> Create(Note note)
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentCollectionUri(_databaseId, CollectionId);
await client.CreateDocumentAsync(link, note);
return note;
}
}
public async Task<Note> Update(Note note)
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentUri(_databaseId, CollectionId, note.Id);
await client.ReplaceDocumentAsync(link, note);
return note;
}
}
public async Task Delete(string id, string customerId)
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentUri(_databaseId, CollectionId, id);
await client.DeleteDocumentAsync(link, new RequestOptions()
{ PartitionKey = new Microsoft.Azure.Documents.PartitionKey(customerId)});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment