예제 #1
0
        public async Task <IActionResult> PostUserComment(string content, int userPageID)
        {
            // Redirects if content is null
            if (content == null)
            {
                return(Redirect("/"));
            }

            // Creates and posts comment
            UserComment comment = new UserComment()
            {
                Content    = content,
                DatePosted = DateTime.Now,
                UserID     = Tools.GetUserID(User),
                UserPageID = userPageID,
            };
            Result result = await _repository.PostUserCommentAsync(comment);

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

                return(Redirect("/User?id=" + userPageID));
            }
            return(StatusCode(result.Code));
        }
예제 #2
0
        public async Task <IActionResult> PostComment(int id, PostCommentRequest request)
        {
            // Returns error if comment is empty
            if (request.Content == null)
            {
                return(BadRequest("Comment cannot be null"));
            }

            // Retrieves user and returns error if user is not found, deleted or locked
            User currentUser = await _repository.GetUserAsync(User);

            User profileUser = await _repository.GetUserAsync(id);

            if (profileUser == null || profileUser.Deleted)
            {
                return(NotFound("Requested user not found"));
            }
            if (profileUser.CommentsLocked)
            {
                return(Forbid("The user's comments are locked"));
            }

            // Creates user comment and adds it to database
            UserComment userComment = new UserComment()
            {
                Content    = request.Content,
                DatePosted = DateTime.Now,
                UserID     = currentUser.UserID,
                UserPageID = profileUser.UserID
            };
            Result <UserComment> result = await _repository.PostUserCommentAsync(userComment);

            if (result.Failure)
            {
                return(StatusCode(result.Code, result.Error));
            }
            await _repository.SaveChangesAsync();

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