public async Task <ActionResult <IEnumerable <Post> > > ReadLatestPosts(string category, int count)
 {
     using var dbContext = new PostServiceContext(GetConnectionString(category));
     return(await dbContext.Post
            .OrderByDescending(p => p.PostId)
            .Where(p => p.CategoryId == category)
            .Take(count)
            .Include(x => x.User)
            .ToListAsync());
 }
 public void InitDatabase(int countUsers, int countCategories)
 {
     foreach (var connectionString in _connectionStrings)
     {
         using var dbContext = new PostServiceContext(connectionString);
         dbContext.Database.EnsureDeleted();
         dbContext.Database.EnsureCreated();
         for (int i = 1; i <= countUsers; i++)
         {
             dbContext.User.Add(new User {
                 Name = "User" + i, Version = 1
             });
             dbContext.SaveChanges();
         }
         for (int i = 1; i <= countCategories; i++)
         {
             dbContext.Category.Add(new Category {
                 CategoryId = "Category" + i
             });
             dbContext.SaveChanges();
         }
     }
 }
 public async Task <int> CreatePost(Post post)
 {
     using var dbContext = new PostServiceContext(GetConnectionString(post.CategoryId));
     dbContext.Post.Add(post);
     return(await dbContext.SaveChangesAsync());
 }