Skip to content

Instantly share code, notes, and snippets.

@dontpaniclabsgists
Created August 24, 2017 18:18
Show Gist options
  • Save dontpaniclabsgists/b77347732dd24cac39443f508ea1d90d to your computer and use it in GitHub Desktop.
Save dontpaniclabsgists/b77347732dd24cac39443f508ea1d90d to your computer and use it in GitHub Desktop.
cosmos3_5
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CosmosTest
{
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 int Count()
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentCollectionUri(_databaseId, CollectionId);
var count = client.CreateDocumentQuery<Note>(
link,
new FeedOptions()
{
EnableCrossPartitionQuery = true
})
.Count();
return count;
}
}
public int CountSql()
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentCollectionUri(_databaseId, CollectionId);
var count = client.CreateDocumentQuery<int>(
link,
"select value count(1) from notes",
new FeedOptions()
{
EnableCrossPartitionQuery = true
})
.AsEnumerable()
.First();
return count;
}
}
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[]> AllSql()
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentCollectionUri(_databaseId, CollectionId);
var query = client.CreateDocumentQuery<Note>(link,
"select * from notes",
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 class PageResult
{
public Note[] Notes { get; set; }
public string Token { get; set; }
public bool HasMore { get; set; }
}
public async Task<PageResult> Page(string token, int pageSize, string customerId)
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentCollectionUri(_databaseId, CollectionId);
var querySetup = client.CreateDocumentQuery<Note>(
link,
new FeedOptions()
{
MaxItemCount = pageSize,
RequestContinuation = token,
PartitionKey = new Microsoft.Azure.Documents.PartitionKey(customerId)
});
var query = querySetup.AsDocumentQuery();
var result = new PageResult()
{ HasMore = false };
var list = new List<Note>();
if (query.HasMoreResults)
{
var note = await query.ExecuteNextAsync<Note>();
result.Token = note.ResponseContinuation;
if (!string.IsNullOrWhiteSpace(result.Token))
result.HasMore = true;
list.AddRange(note);
}
result.Notes = list.ToArray();
return result;
}
}
public async Task<Note> ReadOne(string id, string customerId)
{
using (var client = new DocumentClient(new Uri(_endpoint), _key))
{
var link = UriFactory.CreateDocumentUri(_databaseId, CollectionId, id);
var note = await client.ReadDocumentAsync<Note>(link,
new RequestOptions()
{
PartitionKey = new Microsoft.Azure.Documents.PartitionKey(customerId)
});
return note;
}
}
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