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 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)); } }