@@ -0,0 +1,2 @@
|
||||
class ApplicationController < ActionController::API
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
class PostsController < ApplicationController
|
||||
before_action :set_post, only: [:good, :bad, :destroy]
|
||||
|
||||
# POST /posts/:id/good
|
||||
def good
|
||||
@post.increment!(:good)
|
||||
head :no_content
|
||||
end
|
||||
|
||||
# POST /posts/:id/bad
|
||||
def bad
|
||||
@post.increment!(:bad)
|
||||
head :no_content
|
||||
end
|
||||
|
||||
# DELETE /posts/:id
|
||||
def destroy
|
||||
@post.update!(deleted_at: Time.current)
|
||||
head :no_content
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_post
|
||||
@post = Post.active.find(params[:id])
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
class ThreadPostsController < ApplicationController
|
||||
before_action :set_thread
|
||||
|
||||
# GET /api/threads/:thread_id/posts
|
||||
def index
|
||||
sort = params[:sort].presence_in(['created_at', 'score']) || 'created_at'
|
||||
order = params[:order].presence_in(['asc', 'desc']) || 'desc'
|
||||
|
||||
posts = @thread.posts
|
||||
.select('posts.*, (good - bad) AS score')
|
||||
.order("#{ sort } #{ order }")
|
||||
|
||||
render json: posts
|
||||
end
|
||||
|
||||
# POST /api/threads/:thread_id/posts
|
||||
def create
|
||||
post = @thread.posts.new(post_params)
|
||||
if post.save
|
||||
render json: post, status: :created
|
||||
else
|
||||
render json: { errors: post.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_thread
|
||||
@thread = Topic.find(params[:thread_id])
|
||||
end
|
||||
|
||||
def post_params
|
||||
params.require(:post).permit(:name, :body, :password)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
class ThreadsController < ApplicationController
|
||||
# GET /api/threads
|
||||
def index
|
||||
threads = Topic.order(updated_at: :desc)
|
||||
render json: threads
|
||||
end
|
||||
|
||||
# GET /api/threads/:id
|
||||
def show
|
||||
thread = Topic.find(params[:id])
|
||||
render json: thread
|
||||
end
|
||||
|
||||
# POST /api/threads
|
||||
def create
|
||||
thread = Topic.new(thread_params)
|
||||
if thread.save
|
||||
render json: thread, status: :created
|
||||
else
|
||||
render json: { errors: thread.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def thread_params
|
||||
params.require(:thread).permit(:title, :description)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user