Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/controllers/api/v1/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Api::V1::CommentsController < ApiController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_comment, only: [:show]

def index
@comments = Comment.where(parent_id: nil).order(created_at: :desc)

render json: { comments: @comments }
end

def show; end

private

def comment_params
params.permit(:text)
end

def set_comment
@comment = Comment.find_by_id params[:id]
end
end
9 changes: 9 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ApiController < ApplicationController
before_action :set_default_request_format

private

def set_default_request_format
request.format = :json
end
end
4 changes: 4 additions & 0 deletions app/views/api/v1/comments/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.id @comment.id
json.text @comment.text
json.parent_id @comment.parent_id
json.user_id @comment.user_id
9 changes: 9 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Rails.application.routes.draw do
# for graphql
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end
post "/graphql", to: "graphql#execute"

# default root route must exist for devise
root 'comments#index'

Expand All @@ -17,4 +19,11 @@
end

devise_for :users

# api's
namespace :api do
namespace :v1 do
resources :comments
end
end
end