コード例 #1
0
        public async Task <IActionResult> PostComment(string content, int?threadID)
        {
            if (content == null || threadID == null)
            {
                return(Redirect("/"));
            }

            // Retrieves user
            User user = await _repository.GetUserAsync(User);

            // Create and add comment
            Comment comment = new Comment()
            {
                Content    = content,
                DatePosted = DateTime.Now,
                ThreadID   = (int)threadID,
                UserID     = user.UserID
            };
            Result result = await _repository.PostCommentAsync(comment);

            // Saves changes and redirects if successful, otherwise return error
            if (result.Success)
            {
                await _repository.SaveChangesAsync();

                return(Redirect($"/Thread?id={threadID}#{comment.CommentID}"));
            }
            return(StatusCode(result.Code));
        }
コード例 #2
0
        public async Task <IActionResult> PostComment(int id, PostCommentRequest request)
        {
            // Returns error if comment is null
            if (request.Content == null)
            {
                return(BadRequest("Comment cannot be empty"));
            }

            // Retrieves user and returns error if muted
            User user = await _repository.GetUserAsync(User);

            if (user.Muted)
            {
                return(Unauthorized("Your account is muted, you cannot post comments or create threads"));
            }

            // Retrieves thread and returns error if locked or not found / deleted
            Thread thread = await _repository.GetThreadAsync(id);

            if (thread == null || thread.Deleted)
            {
                return(NotFound("Thread not found"));
            }
            if (thread.Locked)
            {
                return(Forbid("Cannot reply, thread is locked"));
            }

            // Creates and adds comment to database
            Comment comment = new Comment()
            {
                Content    = request.Content,
                DatePosted = DateTime.Now,
                ThreadID   = id
            };
            await _repository.PostCommentAsync(comment);

            await _repository.SaveChangesAsync();

            // Returns JSON response
            return(Json(new ApiComment(comment)));
        }