Skip to content

Commit

Permalink
create api endpoints to list all posts for a user and list all commen…
Browse files Browse the repository at this point in the history
…ts for user post
  • Loading branch information
zunairkhan811 committed Dec 15, 2023
1 parent 2766d3e commit 31a6924
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 4 deletions.
32 changes: 32 additions & 0 deletions app/controllers/api/v1/post_comments_controller.rb
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
12 changes: 12 additions & 0 deletions app/controllers/api/v1/user_posts_controller.rb
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
2 changes: 2 additions & 0 deletions app/helpers/api/v1/post_comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Api::V1::PostCommentsHelper
end
7 changes: 7 additions & 0 deletions app/helpers/api/v1/user_posts_helper.rb
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
18 changes: 14 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users do
resources :user_posts, only: [:index]
end

resources :posts do
resources :post_comments, only: [:index, :create]
end
end
end



devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations'
}
# get 'users/:user_id/posts' => 'posts#index', as: 'user_posts'
# get 'users/:user_id/posts/:id' => 'posts#show', as: 'user_post'
# get 'users' => 'users#index', as: 'users'
# get 'users/:id' => 'users#show', as: 'user'
root 'users#index'
resources :users do
resources :posts do
Expand Down
15 changes: 15 additions & 0 deletions spec/helpers/api/v1/post_comments_helper_spec.rb
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
15 changes: 15 additions & 0 deletions spec/helpers/api/v1/user_posts_helper_spec.rb
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
7 changes: 7 additions & 0 deletions spec/requests/api/v1/post_comments_spec.rb
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
7 changes: 7 additions & 0 deletions spec/requests/api/v1/user_posts_spec.rb
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

0 comments on commit 31a6924

Please sign in to comment.