-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create api endpoints to list all posts for a user and list all commen…
…ts for user post
- Loading branch information
1 parent
2766d3e
commit 31a6924
Showing
9 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Api | ||
module V1 | ||
class PostCommentsController < ApplicationController | ||
skip_before_action :authenticate_user! | ||
before_action :set_post | ||
skip_before_action :verify_authenticity_token, only: [:create] | ||
|
||
def index | ||
comments = @post.comments | ||
render json: comments | ||
end | ||
|
||
private | ||
|
||
def set_post | ||
@post = Post.find_by(id: params[:post_id]) | ||
|
||
return if @post | ||
|
||
render json: { error: 'Post not found' }, status: :not_found | ||
end | ||
|
||
def comment_params | ||
params.require(:comment).permit(:text) | ||
end | ||
|
||
# def current_user | ||
# @current_user ||= User.find_by(confirmation_token: request.headers['Authorization']&.split(' ')&.last) | ||
# end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Api | ||
module V1 | ||
class UserPostsController < ApplicationController | ||
skip_before_action :authenticate_user! | ||
def index | ||
user = User.find(params[:user_id]) | ||
posts = user.posts | ||
render json: posts | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module Api::V1::PostCommentsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Api | ||
module V1 | ||
module UserPostsHelper | ||
# Your helper methods here | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails_helper' | ||
|
||
# Specs in this file have access to a helper object that includes | ||
# the Api::V1::PostCommentsHelper. For example: | ||
# | ||
# describe Api::V1::PostCommentsHelper do | ||
# describe "string concat" do | ||
# it "concats two strings with spaces" do | ||
# expect(helper.concat_strings("this","that")).to eq("this that") | ||
# end | ||
# end | ||
# end | ||
RSpec.describe Api::V1::PostCommentsHelper, type: :helper do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails_helper' | ||
|
||
# Specs in this file have access to a helper object that includes | ||
# the Api::V1::UserPosts~Helper. For example: | ||
# | ||
# describe Api::V1::UserPosts~Helper do | ||
# describe "string concat" do | ||
# it "concats two strings with spaces" do | ||
# expect(helper.concat_strings("this","that")).to eq("this that") | ||
# end | ||
# end | ||
# end | ||
RSpec.describe Api::V1::UserPosts ~Helper, type: :helper do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Api::V1::PostComments', type: :request do | ||
describe 'GET /index' do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Api::V1::UserPosts~s', type: :request do | ||
describe 'GET /index' do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
end | ||
end |