Пример #1
0
        /// <summary>
        /// Adds a given comment to a given post
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> AddCommentToPostAsync(ActualRequest actualRequest)
        {
            CommentForPost comment =
                JsonSerializer.Deserialize <CommentForPost>(actualRequest.Request.Argument.ToString());
            int createdCommentId = await postRepo.AddCommentToPost(comment);

            Request responseRequest = new Request
            {
                ActionType = ActionType.POST_ADD_COMMENT.ToString(),
                Argument   = JsonSerializer.Serialize(createdCommentId)
            };

            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }
Пример #2
0
        public async Task <int> AddCommentToPost(CommentForPost comment)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                Post post = await ctx.Posts.Where(p => p.Id == comment.PostId)
                            .Include(p => p.Comments).FirstAsync();

                User owner = await ctx.Users.FirstAsync(u => u.Id == comment.OwnerId);

                Comment commentDb = new Comment
                {
                    Content   = comment.Content,
                    TimeStamp = DateTime.Now,
                    Owner     = owner
                };
                post.Comments.Add(commentDb);
                ctx.Posts.Update(post);
                await ctx.Comment.AddAsync(commentDb);

                await ctx.SaveChangesAsync();

                return(ctx.Comment.Max(c => c.Id));
            }
        }