Add ability to create post with images

This commit is contained in:
2018-06-05 07:51:48 +10:00
parent 383f527502
commit db4559287f
7 changed files with 48 additions and 22 deletions
+11 -13
View File
@@ -1,6 +1,6 @@
class Api::V1::PostsController < Api::V1::ApplicationController
before_action :permit_params, :check_url, :map_post_type, :get_post, only: [:create]
before_action :permit_params, :get_post, only: [:create]
before_action :load_post, only: [:add_viewed_times, :show]
skip_before_action :authenticate_user_from_token, only: [:posts, :show, :liked_posts, :bookmarked_posts, :random]
@@ -9,7 +9,7 @@ class Api::V1::PostsController < Api::V1::ApplicationController
before_action :load_target_user, only: [:posts, :liked_posts, :bookmarked_posts]
after_action :process_post, only: :create
# after_action :process_post, only: :create
POST_TYPE = {
:ARTICLE => 'article',
@@ -17,13 +17,10 @@ class Api::V1::PostsController < Api::V1::ApplicationController
}
def create
if @current_user.share_instances.where(sharable: @post).empty?
share_instance = @current_user.share_instances.create! sharable: @post, description: params[:description]
@current_user.timeline_sources.create! post_id: @post.id, action_type: 'share'
render json: share_instance.to_json(include: :sharable)
else
render json: {message: '请勿重复分享同一资源'}, status: 409
end
share_instance = @current_user.share_instances.create! sharable: @post, description: params[:description]
@current_user.timeline_sources.create! post_id: @post.id, action_type: 'share'
@post.images.create permit_params[:images]
render json: share_instance.to_json(include: :sharable)
end
def show
@@ -67,14 +64,15 @@ class Api::V1::PostsController < Api::V1::ApplicationController
private
def permit_params
params.permit(:url, :type, :description)
params.permit(:url, :type, :description, :title, :images => [:url, :title, :key])
end
def get_post
begin
@post = Post.find_or_create_by!(url: params[:url], type: @type)
rescue ActiveRecord::RecordInvalid
return render json: {msg: '缺少参数,校验失败'}, status: 400
@post = Post.find_or_create_by!(title: params[:title], url: 'NONE', type: 'Article')
rescue ActiveRecord::RecordInvalid => e
Rails.logger.warn e
return render json: (msg e), status: 400
end
end
+4
View File
@@ -0,0 +1,4 @@
class Image < ApplicationRecord
belongs_to :post
belongs_to :user
end
+2
View File
@@ -6,6 +6,8 @@ class Post < ActiveRecord::Base
has_many :share_instances, as: :sharable, dependent: :destroy
has_many :timeline_sources
has_many :images
has_one :thumbnail
validates_presence_of :url, :type
+1 -1
View File
@@ -18,7 +18,7 @@ default: &default
reconnect: true
adapter: postgresql
encoding: unicode
host: postgres
host: localhost
username: postgres
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
@@ -0,0 +1,13 @@
class CreateImages < ActiveRecord::Migration[5.2]
def change
create_table :images do |t|
t.string :key
t.string :url
t.string :title
t.references :post, foreign_key: true
t.references :user, foreign_key: true
t.timestamps
end
end
end
+15 -1
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2015_11_25_153022) do
ActiveRecord::Schema.define(version: 2018_06_04_111225) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -95,6 +95,18 @@ ActiveRecord::Schema.define(version: 2015_11_25_153022) do
t.index ["user_id"], name: "index_identities_on_user_id"
end
create_table "images", force: :cascade do |t|
t.string "key"
t.string "url"
t.string "title"
t.bigint "post_id"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["post_id"], name: "index_images_on_post_id"
t.index ["user_id"], name: "index_images_on_user_id"
end
create_table "likes", force: :cascade do |t|
t.integer "user_id"
t.integer "likable_id"
@@ -176,4 +188,6 @@ ActiveRecord::Schema.define(version: 2015_11_25_153022) do
end
add_foreign_key "identities", "users"
add_foreign_key "images", "posts"
add_foreign_key "images", "users"
end
+2 -7
View File
@@ -14,14 +14,12 @@ services:
- "3000:3000"
links:
- postgres
networks:
- dev
entrypoint: [./entrypoint.sh]
command: bundle exec puma -t 5:5 -p 3000
postgres:
image: postgres:latest
networks:
- dev
ports:
- "5432:5432"
environment:
POSTGRES_DB: tuxue
volumes:
@@ -29,6 +27,3 @@ services:
volumes:
gems_cache:
networks:
dev: