Updated articles and comments endpoints
This commit is contained in:
parent
7551a03e6e
commit
ab22669b02
2 changed files with 9 additions and 8 deletions
|
|
@ -15,7 +15,7 @@ class V1::ArticlesController < ApplicationController
|
||||||
"next_page": "#{current_page.to_i < total_pages.to_i ? (current_page.to_i+1) : (current_page)}",
|
"next_page": "#{current_page.to_i < total_pages.to_i ? (current_page.to_i+1) : (current_page)}",
|
||||||
"prev_page": "#{current_page.to_i > 1 ? (current_page.to_i-1) : (current_page)}"
|
"prev_page": "#{current_page.to_i > 1 ? (current_page.to_i-1) : (current_page)}"
|
||||||
}
|
}
|
||||||
json_response({articles: @article.as_json(include: [:user, :comments]), pagination: pagination})
|
json_response({articles: @article.as_json(include: :user), pagination: pagination})
|
||||||
end
|
end
|
||||||
|
|
||||||
# POST /articles
|
# POST /articles
|
||||||
|
|
@ -26,7 +26,7 @@ class V1::ArticlesController < ApplicationController
|
||||||
|
|
||||||
# GET /articles/:id
|
# GET /articles/:id
|
||||||
def show
|
def show
|
||||||
json_response(@article.to_json(include: :user))
|
json_response(@article.to_json(include: [:user, :comments]))
|
||||||
end
|
end
|
||||||
|
|
||||||
# PUT /articles/:id
|
# PUT /articles/:id
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
class CommentsController < ApplicationController
|
class V1::CommentsController < ApplicationController
|
||||||
before_action :set_comment, only: [:show, :update, :destroy]
|
before_action :set_comment, only: [:show, :update, :destroy]
|
||||||
|
skip_before_action :authorize_request, only: [:index, :show]
|
||||||
|
|
||||||
# GET /comments
|
# GET /comments
|
||||||
def index
|
def index
|
||||||
@comments = Comment.all
|
# @comments = Comment.find_by(article_id: params[:article_id]).as_json(include: :user)
|
||||||
|
@comments = Article.find(params[:article_id]).comments.as_json(include: :user)
|
||||||
render json: @comments
|
json_response(@comments)
|
||||||
end
|
end
|
||||||
|
|
||||||
# GET /comments/1
|
# GET /comments/1
|
||||||
|
|
@ -15,7 +16,7 @@ class CommentsController < ApplicationController
|
||||||
|
|
||||||
# POST /comments
|
# POST /comments
|
||||||
def create
|
def create
|
||||||
@comment = Comment.new(comment_params)
|
@comment = current_user.comments.create!(content: params[:content], article_id: params[:article_id]) # Comment.new(comment_params)
|
||||||
|
|
||||||
if @comment.save
|
if @comment.save
|
||||||
render json: @comment, status: :created, location: @comment
|
render json: @comment, status: :created, location: @comment
|
||||||
|
|
@ -46,6 +47,6 @@ class CommentsController < ApplicationController
|
||||||
|
|
||||||
# Only allow a trusted parameter "white list" through.
|
# Only allow a trusted parameter "white list" through.
|
||||||
def comment_params
|
def comment_params
|
||||||
params.require(:comment).permit(:content, :reference, :reference)
|
params.require(:comment).permit(:content, :article_id, :user_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Loading…
Add table
Reference in a new issue