Skip to content

Instantly share code, notes, and snippets.

@paladique
Last active April 27, 2018 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paladique/825db5f023def41f48b10ba68e8def0f to your computer and use it in GitHub Desktop.
Save paladique/825db5f023def41f48b10ba68e8def0f to your computer and use it in GitHub Desktop.
CosmosDB Blog Code Snippets
public static async void CreateDocument<T>(T item, string collectionName) where T : Item
{
await client.CreateDocumentAsync((GetCollectionUri(collectionName)), item);
}
public static async void EditDocument(Item item, string collectionName)
{
var doc = GetDocument(item.Id, collectionName);
doc.SetPropertyValue("notes", item.Notes);
doc.SetPropertyValue("tutorial", item.IsTutorial);
Document updated = await client.ReplaceDocumentAsync(doc, new RequestOptions { PartitionKey = new PartitionKey(doc.GetPropertyValue<string>("name")) });
}
public static async void EditVideoDocument(Item item, string collectionName)
{
var video = GetVideoDocument(item.Id, collectionName);
video.Notes = item.Notes;
video.IsTutorial = item.IsTutorial;
Document updated = await client.ReplaceDocumentAsync(video, new RequestOptions { PartitionKey = new PartitionKey(video.Name) });
}
public static async Task<string> GetAllDocs<T>(string collectionName)
{
var q = client.CreateDocumentQuery<T>(GetCollectionUri(collectionName), new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
.Select(item => item)
.AsDocumentQuery();
List<T> results = new List<T>();
while (q.HasMoreResults)
{
results.AddRange(await q.ExecuteNextAsync<T>());
}
return JsonConvert.SerializeObject(results);
}
public static Document GetDocument(string id, string collectionName)
{
var doc = client.CreateDocumentQuery<Document>(GetCollectionUri(collectionName), new FeedOptions { EnableCrossPartitionQuery = true })
.Where(r => r.Id == id)
.AsEnumerable()
.SingleOrDefault();
return doc;
}
public static Video GetVideoDocument(string id, string collectionName)
{
var video = client.CreateDocumentQuery<Video>(UriFactory.CreateDocumentCollectionUri(config["database"], collectionName), new FeedOptions { EnableCrossPartitionQuery = true })
.Where(r => r.Id == id && r.Keywords.FindIndex(word => word.name.Contains("Azure") != -1)
.AsEnumerable()
.SingleOrDefault();
return video;
}
public static string Search<T>(string collectionName, string searchTerms, string searchText)
{
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true };
var terms = searchTerms.Trim().Split(' ');
var contains = terms.Select(x => string.Format("CONTAINS({0}, @{1})", x == "keywords" ? "k.name" : "item." + x, x)).ToArray();
var q = new SqlQuerySpec
{
QueryText = "SELECT VALUE item FROM item JOIN k IN item.keywords WHERE " + contains[0],
Parameters = new SqlParameterCollection()
{
new SqlParameter("@" + terms[0].ToLower(), searchText)
}
};
if (terms.Length > 2 && contains.Length > 2)
{
q.Parameters.Add(new SqlParameter("@" + terms[1], searchText));
q.QueryText += " OR " + contains[1];
}
List<T> queryResult = (client.CreateDocumentQuery<T>(
GetCollectionUri(collectionName),
q,
queryOptions)).ToList();
return JsonConvert.SerializeObject(queryResult);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment