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
|
|
|
|
|
|
|
|
def index_by_board_id
|
|
|
|
|
board_id = params[:board_id] || 1
|
2019-09-03 12:58:44 +02:00
|
|
|
|
2019-09-02 19:26:34 +02:00
|
|
|
|
|
|
|
|
posts = Post
|
|
|
|
|
.left_outer_joins(:post_status)
|
|
|
|
|
.select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color')
|
2019-09-03 12:58:44 +02:00
|
|
|
.where(filter_params)
|
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
|
|
|
|
|
params.permit(:board_id, :post_status_id)
|
|
|
|
|
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
|
2019-09-03 12:58:44 +02:00
|
|
|
|
2019-09-02 14:32:57 +02:00
|
|
|
end
|