Ejemplo n.º 1
0
        public async Task <IActionResult> ReplyToComment(
            [FromBody] CommentReplyPost reply)
        {
            ArtefactComment replyingTo = _context.ArtefactComments
                                         .SingleOrDefault(c => c.Id == reply.ParentCommentId);

            if (replyingTo == null)
            {
                return(NotFound());
            }

            var curUser = await _userService.GetCurUser(HttpContext);

            var newCommentReply = new ArtefactComment
            {
                Body            = reply.Body,
                ArtefactId      = replyingTo.ArtefactId,
                AuthorId        = curUser.Id,
                Author          = curUser,
                ParentCommentId = replyingTo.Id,
                CreatedAt       = System.DateTime.UtcNow,
            };

            // no need to catch the fk error - already checked for 404
            await _context.AddAsync(newCommentReply);

            await _context.SaveChangesAsync();

            return(new JsonResult(_converter.ToJson(newCommentReply)));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddComment(
            [FromBody] CommentPost newComment)
        {
            var curUser = await _userService.GetCurUser(HttpContext);

            var createdComment = new ArtefactComment
            {
                ArtefactId = newComment.ArtefactId,
                AuthorId   = curUser.Id,
                Author     = curUser,
                Body       = newComment.Body,
                CreatedAt  = System.DateTime.UtcNow,
            };

            try
            {
                await _context.AddAsync(createdComment);

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                var sqlError = Helpers.GetSqlError((SqlException)e.GetBaseException());
                switch (sqlError.Number)
                {
                case 547:     // fk violation
                    return(NotFound($"Artefact '{newComment.ArtefactId}' does not exist."));

                default:
                    throw;
                }
            }

            return(new JsonResult(_converter.ToJson(createdComment)));
        }