#14 ぼちぼち
This commit is contained in:
@@ -32,12 +32,30 @@ class PostsController < ApplicationController
|
||||
|
||||
# POST /posts
|
||||
def create
|
||||
@post = Post.new(post_params)
|
||||
# TODO: current_user.role が 'admin' もしくは 'member' でなければ 403
|
||||
|
||||
if @post.save
|
||||
render json: @post, status: :created, location: @post
|
||||
title = params[:title]
|
||||
unless title.present?
|
||||
# TODO:
|
||||
# 既知サイトなら決まったフォーマットで,
|
||||
# 未知サイトならページ名をセットする.
|
||||
end
|
||||
post = Post.new(title: title, url: params[:url], thumbnail_base: '', uploaded_user: current_user)
|
||||
if params[:thumbnail].present?
|
||||
post.thumbnail.attach(params[:thumbnail])
|
||||
else
|
||||
render json: @post.errors, status: :unprocessable_entity
|
||||
# TODO:
|
||||
# 既知ドメインであれば,指定のアドレスからサムネール取得,
|
||||
# それ以外なら URL のスクショ・イメージをサムネールに登録.
|
||||
end
|
||||
if post.save
|
||||
if params[:tags].present?
|
||||
tag_ids = JSON.parse(params[:tags])
|
||||
post.tags = Tag.where(id: tag_ids)
|
||||
end
|
||||
render json: post, status: :created
|
||||
else
|
||||
render json: { errors: post.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddTitleToPosts < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_column :posts, :title, :string, after: :id, null: false
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user