This commit is contained in:
2025-07-09 06:54:19 +09:00
parent 57f3cf83ae
commit 9be4bb1532
7 changed files with 200 additions and 201 deletions
+35 -12
View File
@@ -53,19 +53,30 @@ class PostsController < ApplicationController
# POST /posts
def create
return head :unauthorized unless current_user
return head :forbidden unless ['admin', 'member'].include?(current_user.role)
return head :forbidden unless current_user.member?
# TODO: URL が正規のものがチェック,不正ならエラー
# TODO: title、URL は必須にする.
# TODO: サイトに応じて thumbnail_base 設定
title = params[:title]
post = Post.new(title:, url: params[:url], thumbnail_base: '', uploaded_user: current_user)
post.thumbnail.attach(params[:thumbnail])
url = params[:url]
thumbnail = params[:thumbnail]
tag_names = params[:tags].to_s.split(' ')
if tag_names.size < 20 && tag_names.none?('タグ希望')
tag_names << 'タグ希望'
end
post = Post.new(title:, url:, thumbnail_base: '', uploaded_user: current_user)
post.thumbnail.attach(thumbnail)
if post.save
post.resized_thumbnail!
if params[:tags].present?
tag_ids = JSON.parse(params[:tags])
post.tags = Tag.where(id: tag_ids)
end
render json: post, status: :created
# TODO: 接頭辞に応じて category 変へる
post.tags = tag_names.map { |name| Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'general'
} }
render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
status: :created
else
render json: { errors: post.errors.full_messages }, status: :unprocessable_entity
end
@@ -88,11 +99,23 @@ class PostsController < ApplicationController
# PATCH/PUT /posts/1
def update
return head :unauthorized unless current_user
return head :forbidden unless ['admin', 'member'].include?(current_user.role)
return head :forbidden unless current_user.member?
post = Post.find(params[:id])
tag_ids = JSON.parse(params[:tags])
if post.update(title: params[:title], tags: Tag.where(id: tag_ids))
title = params[:title]
tag_names = params[:tags].to_s.split(' ')
if tag_names.size < 20 && tag_names.none?('タグ希望')
tag_names << 'タグ希望'
end
post = Post.find(params[:id].to_i)
tags = post.tags.where(category: 'nico').to_a
tag_names.each do |name|
# TODO: 接頭辞に応じて category 変へる
tags << Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'general'
}
end
if post.update(title:, tags:)
render({ json: (post
.as_json(include: { tags: { only: [:id, :name, :category] } })),
status: :created })