2019-09-02 14:32:57 +02:00
|
|
|
class PostsController < ApplicationController
|
2019-09-04 15:24:15 +02:00
|
|
|
before_action :authenticate_user!, only: [:create]
|
2019-09-02 19:26:34 +02:00
|
|
|
|
2019-09-04 17:37:08 +02:00
|
|
|
def index
|
2019-09-02 19:26:34 +02:00
|
|
|
posts = Post
|
2019-09-11 18:30:59 +02:00
|
|
|
.select(:title, :description, :post_status_id)
|
2019-09-03 12:58:44 +02:00
|
|
|
.where(filter_params)
|
2019-09-09 16:50:33 +02:00
|
|
|
.search_by_name_or_description(params[:search])
|
2019-09-04 21:12:07 +02:00
|
|
|
.page(params[:page])
|
2019-09-02 19:26:34 +02:00
|
|
|
|
2019-09-04 15:24:15 +02:00
|
|
|
render json: posts
|
2019-09-02 19:26:34 +02:00
|
|
|
end
|
2019-09-02 14:32:57 +02:00
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
post = Post.new(post_params)
|
|
|
|
|
|
|
|
|
|
if post.save
|
2019-09-04 15:24:15 +02:00
|
|
|
render json: post, status: :no_content
|
2019-09-02 14:32:57 +02:00
|
|
|
else
|
2019-09-04 15:24:15 +02:00
|
|
|
render json: {
|
|
|
|
|
error: I18n.t('errors.post.create', message: post.errors.full_messages)
|
|
|
|
|
}, status: :unprocessable_entity
|
2019-09-02 14:32:57 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
2019-09-03 12:58:44 +02:00
|
|
|
|
|
|
|
|
def filter_params
|
2019-09-05 17:11:07 +02:00
|
|
|
defaults = { board_id: Board.first.id }
|
2019-09-04 17:37:08 +02:00
|
|
|
|
|
|
|
|
params
|
2019-09-09 16:50:33 +02:00
|
|
|
.permit(:board_id, :post_status_id)
|
2019-09-04 17:37:08 +02:00
|
|
|
.with_defaults(defaults)
|
2019-09-03 12:58:44 +02:00
|
|
|
end
|
|
|
|
|
|
2019-09-02 14:32:57 +02:00
|
|
|
def post_params
|
2019-09-04 15:24:15 +02:00
|
|
|
params
|
|
|
|
|
.require(:post)
|
|
|
|
|
.permit(:title, :description, :board_id)
|
|
|
|
|
.merge(user_id: current_user.id)
|
2019-09-02 14:32:57 +02:00
|
|
|
end
|
|
|
|
|
end
|