public PostDetailModel GetById(Guid id)
 {
     using (var dbContext = _dbContextFactory.CreateTeamChatDbContext())
     {
         return(PostMapper.MapToDetailModel(dbContext.Posts
                                            .Include(a => a.Author)
                                            .Include(c => c.Comments)
                                            .ThenInclude(ca => ca.Author)
                                            .First(e => e.Id == id)
                                            ));
     }
 }
 public PostDetailModel UpdateContent(PostDetailModel postModel, string content)
 {
     using (var dbContext = _dbContextFactory.CreateTeamChatDbContext())
     {
         var postEntity = dbContext.Posts
                          .Include(a => a.Author)
                          .Include(c => c.Comments)
                          .ThenInclude(c => c.Author)
                          .First(p => p.Id == postModel.Id);
         postEntity.Content = content;
         dbContext.Posts.Update(postEntity);
         dbContext.SaveChanges();
         return(PostMapper.MapToDetailModel(postEntity));
     }
 }
        public PostDetailModel Create(PostDetailModel postModel, UserDetailModel authorModel)
        {
            using (var dbContext = _dbContextFactory.CreateTeamChatDbContext())
            {
                postModel.CreationTime = DateTime.Now;
                postModel.Author       = UserMapper.DetailToListModel(authorModel);
                var postEntity = PostMapper.MapDetailModelToEntity(postModel);
                postEntity.Author = UserMapper.MapToEntity(authorModel);

                var userEntity = dbContext.Users
                                 .First(u => u.Id == authorModel.Id);

                userEntity.Activities.Add(postEntity);
                dbContext.Users.Update(userEntity);
                dbContext.Posts.Update(postEntity);
                dbContext.SaveChanges();
                return(PostMapper.MapToDetailModel(postEntity));
            }
        }
        public PostDetailModel RemoveComment(PostDetailModel postModel, CommentDetailModel commentModel)
        {
            using (var dbContext = _dbContextFactory.CreateTeamChatDbContext())
            {
                var postEntity = dbContext.Posts
                                 .Include(c => c.Comments)
                                 .ThenInclude(a => a.Author)
                                 .Include(a => a.Author)
                                 .First(p => p.Id == postModel.Id);

                var commentEntity = dbContext.Comments
                                    .Include(a => a.Author)
                                    .First(c => c.Id == commentModel.Id);

                postEntity.Comments.Remove(commentEntity);
                commentEntity.BelongsTo = null;
                dbContext.Posts.Update(postEntity);
                dbContext.SaveChanges();

                return(PostMapper.MapToDetailModel(postEntity));
            }
        }