Beispiel #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            // Check the EndpointURL
            try
            {
                var endpoint = ConfigurationManager.AppSettings["EndpointURL"];
                var uri      = new Uri(endpoint);
            }
            catch (Exception ex)
            {
                throw new Exception("Invalid EndpointURL. Check Web.config or your App Settings.");
            }

            try
            {
                // Init must be called before using the DbHelper
                await CosmosDbHelper.InitAsync();

                // Create the database and collection if it doesn't already exist
                Trace.WriteLine($"Creating database {CosmosDbHelper.DatabaseId} (if not already exists)");
                await CosmosDbHelper.CreateDatabaseAsync();

                await CosmosDbHelper.CreateCollectionAsync(Article.CollectionId, Article.PartitionKey);

                // If the database if empty, insert some sample articles
                if (await Article.GetNumberOfArticles() == 0)
                {
                    Trace.WriteLine("Creating sample articles");
                    await Article.Create(await Article.GetSampleArticles());
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to init database.", ex);
            }
        }
Beispiel #2
0
        public async static Task <long> GetNumberOfArticles()
        {
            var articleCount = await CosmosDbHelper.ExecuteScalarQueryAsync <dynamic>("SELECT VALUE COUNT(1) FROM articles", Article.CollectionId, true);

            return(articleCount);
        }
Beispiel #3
0
 public async Task Create()
 {
     await CosmosDbHelper.CreateDocumentAsync(this, CollectionId);
 }
Beispiel #4
0
        public static async Task <Article[]> SearchByTag(string tag)
        {
            var articles = await CosmosDbHelper.ExecuteQueryAsync <Article>($"SELECT * FROM articles AS a WHERE ARRAY_CONTAINS(a.tags, '{tag}')", CollectionId, true);

            return(articles);
        }
Beispiel #5
0
        public static async Task <Article[]> SearchByFreetext(string freetext)
        {
            var articles = await CosmosDbHelper.ExecuteQueryAsync <Article>($"SELECT * FROM articles AS a WHERE CONTAINS(UPPER(a.body), '{freetext.ToUpper()}')", CollectionId, true);

            return(articles);
        }
Beispiel #6
0
        public static async Task <Article[]> SearchByAuthor(string author)
        {
            var articles = await CosmosDbHelper.ExecuteQueryAsync <Article>($"SELECT * FROM articles AS a WHERE a.author = '{author}'", CollectionId, true);

            return(articles);
        }
Beispiel #7
0
        public static async Task <Article[]> GetAll()
        {
            var articles = await CosmosDbHelper.ExecuteQueryAsync <Article>("SELECT * FROM articles", CollectionId, true);

            return(articles);
        }
Beispiel #8
0
        public static async Task <Article> Read(Guid articleId, string author)
        {
            var article = await CosmosDbHelper.GetDocumentAsync <Article>(articleId.ToString(), author, CollectionId);

            return(article);
        }
Beispiel #9
0
 public async Task Delete()
 {
     await CosmosDbHelper.DeleteDocumentAsync(ArticleId.ToString(), Author, CollectionId);
 }
Beispiel #10
0
 public async Task Upsert()
 {
     await CosmosDbHelper.UpsertDocumentAsync(this, CollectionId);
 }