From db4559287ff056d54ac4cb98b3a1c3975aecc975 Mon Sep 17 00:00:00 2001 From: Junv Zhao Date: Tue, 5 Jun 2018 07:51:48 +1000 Subject: [PATCH] Add ability to create post with images --- app/controllers/api/v1/posts_controller.rb | 24 ++++++++++------------ app/models/image.rb | 4 ++++ app/models/post.rb | 2 ++ config/database.yml | 2 +- db/migrate/20180604111225_create_images.rb | 13 ++++++++++++ db/schema.rb | 16 ++++++++++++++- docker-compose.yml | 9 ++------ 7 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 app/models/image.rb create mode 100644 db/migrate/20180604111225_create_images.rb diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb index 9cc268b..8d012f9 100644 --- a/app/controllers/api/v1/posts_controller.rb +++ b/app/controllers/api/v1/posts_controller.rb @@ -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 diff --git a/app/models/image.rb b/app/models/image.rb new file mode 100644 index 0000000..26cafde --- /dev/null +++ b/app/models/image.rb @@ -0,0 +1,4 @@ +class Image < ApplicationRecord + belongs_to :post + belongs_to :user +end diff --git a/app/models/post.rb b/app/models/post.rb index 80fb4d5..b26e01c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/config/database.yml b/config/database.yml index d1ae2d0..fbdc814 100644 --- a/config/database.yml +++ b/config/database.yml @@ -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 diff --git a/db/migrate/20180604111225_create_images.rb b/db/migrate/20180604111225_create_images.rb new file mode 100644 index 0000000..3aeaff7 --- /dev/null +++ b/db/migrate/20180604111225_create_images.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 47ceee5..31ac938 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index 4ecbf95..dc208dc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: