示例#1
0
        public CommentDetailModel Create(CommentDetailModel commentModel, UserDetailModel authorModel, PostDetailModel postModel)
        {
            using (var dbContext = _dbContextFactory.CreateTeamChatDbContext())
            {
                commentModel.BelongsTo    = PostMapper.DetailToListModel(postModel);
                commentModel.Author       = UserMapper.DetailToListModel(authorModel);
                commentModel.CreationTime = DateTime.Now;

                var userEntity = dbContext.Users
                                 .Include(a => a.Activities)
                                 .First(u => u.Id == commentModel.Author.Id);
                var commentEntity = CommentMapper.MapToEntity(commentModel);
                userEntity.Activities.Add(commentEntity);
                dbContext.Users.Update(userEntity);
                dbContext.Comments.Update(commentEntity);
                dbContext.SaveChanges();

                return(CommentMapper.MapToDetailModel(commentEntity));
            }
        }