Пример #1
0
        private async static void RunEmbeddedOperations()
        {
            Console.WriteLine("intializing cosmos db...");
            CosmosDbRepository <EmbeddedBlog> dbRepository = new CosmosDbRepository <EmbeddedBlog>("FamilyDatabase", "EmbeddedBlogs");

            Console.WriteLine("connected");
            Console.WriteLine();
            EmbeddedOperations operations = new EmbeddedOperations(dbRepository);

            Console.WriteLine("creating new blog with comment");
            EmbeddedBlog blog = await operations.CreateBlog();

            Console.WriteLine("Fetching blog with ID..");
            operations.Get(blog.Id, blog.Type);
            Console.WriteLine("updating the blog");
            operations.Update(blog);
            Console.WriteLine("updating the comment");
            operations.UpdateComment(blog);
            Console.WriteLine("Fetching blog with query..");
            operations.QueryBlog(blog.Id);
            //Console.WriteLine("removing the blog");
            //operations.Remove(blog);
            //Console.WriteLine("removing the comment");
            //operations.RemoveComment(blog);
        }
        public async void RemoveComment(EmbeddedBlog blog)
        {
            blog.Comments.Remove(blog.Comments[0]);
            await _blogsRepository.AddOrUpdateAsync(blog, blog.Type);

            // Console.WriteLine();
        }
        public async void UpdateComment(EmbeddedBlog blog)
        {
            blog.Comments[0].Details = "this is a updated comment";
            await _blogsRepository.AddOrUpdateAsync(blog, blog.Type);

            // Console.WriteLine();
        }
        public async Task <EmbeddedBlog> CreateBlog()
        {
            string       blogId = Guid.NewGuid().ToString();
            EmbeddedBlog blog   = new EmbeddedBlog
            {
                BlogId   = blogId,
                Id       = blogId,
                Type     = "Post",
                Content  = TextHelper.OneMBText,
                Comments = new List <Comment>()
            };
            //for(int i = 1; i < 11; i++)
            //{
            //    blog.Comments.Add(new Comment
            //    {
            //        CommentId = i.ToString(),
            //        Id = i.ToString(),
            //        Likes = i,
            //        Details = " this is comment "+ i
            //    });
            //}
            await _blogsRepository.AddAsync(blog);

            // Console.WriteLine();
            return(blog);
        }
        public async void Update(EmbeddedBlog blog)
        {
            string commentId = Guid.NewGuid().ToString();

            blog.Comments.Add(new Comment
            {
                CommentId = commentId,
                Id        = commentId,
                Details   = "never read a post like this",
                Likes     = 1
            });
            await _blogsRepository.AddOrUpdateAsync(blog, blog.Type);

            // Console.WriteLine();
        }
        public async void Remove(EmbeddedBlog blog)
        {
            await _blogsRepository.RemoveAsync(blog.Id, blog.Type);

            // Console.WriteLine();
        }