Last active
April 27, 2018 15:58
-
-
Save paladique/825db5f023def41f48b10ba68e8def0f to your computer and use it in GitHub Desktop.
CosmosDB Blog Code Snippets
This file contains 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 static async void CreateDocument<T>(T item, string collectionName) where T : Item | |
{ | |
await client.CreateDocumentAsync((GetCollectionUri(collectionName)), item); | |
} |
This file contains 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 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")) }); | |
} |
This file contains 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 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) }); | |
} |
This file contains 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 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); | |
} |
This file contains 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 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; | |
} |
This file contains 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 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; | |
} |
This file contains 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 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