diff --git a/app/assets/javascripts/api/v1/likes.coffee b/app/assets/javascripts/api/v1/likes.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/api/v1/likes.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/api/v1/likes.scss b/app/assets/stylesheets/api/v1/likes.scss new file mode 100644 index 0000000..9baed4d --- /dev/null +++ b/app/assets/stylesheets/api/v1/likes.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the api/v1/likes controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/api/v1/application_controller.rb b/app/controllers/api/v1/application_controller.rb index ca76932..efab867 100644 --- a/app/controllers/api/v1/application_controller.rb +++ b/app/controllers/api/v1/application_controller.rb @@ -4,9 +4,8 @@ class Api::V1::ApplicationController < ActionController::Base skip_before_action :verify_authenticity_token serialization_scope :current_user! - protect_from_forgery with: :exception - before_action :current_user! + before_action :authenticate_user_from_token def current_user! @current_user = nil @@ -34,4 +33,16 @@ class Api::V1::ApplicationController < ActionController::Base end end + private + + def load_target_user + @target_user = User.find_by id: params[:user_id] + render json: {msg: '不能找到该用户'}, status: 404 unless @target_user + end + + def load_post + @target_post = Post.find_by id: params[:post_id] + render json: {msg: '不能找到该文章'}, status: 404 unless @target_post + end + end diff --git a/app/controllers/api/v1/follow_controller.rb b/app/controllers/api/v1/follow_controller.rb index 727ef50..ddfa6ea 100644 --- a/app/controllers/api/v1/follow_controller.rb +++ b/app/controllers/api/v1/follow_controller.rb @@ -1,7 +1,7 @@ class Api::V1::FollowController < Api::V1::ApplicationController - skip_before_action :verify_authenticity_token - before_action :authenticate_user_from_token, :load_target_user + + before_action :load_target_user def create if @current_user.stars.include? @target_user @@ -21,11 +21,4 @@ class Api::V1::FollowController < Api::V1::ApplicationController end end - private - - def load_target_user - @target_user = User.find_by id: params[:user_id] - render json: {msg: '不能找到该用户'}, status: 404 unless @target_user - end - end diff --git a/app/controllers/api/v1/likes_controller.rb b/app/controllers/api/v1/likes_controller.rb new file mode 100644 index 0000000..a4e41fc --- /dev/null +++ b/app/controllers/api/v1/likes_controller.rb @@ -0,0 +1,24 @@ +class Api::V1::LikesController < Api::V1::ApplicationController + + before_action :load_post + + def like + if @current_user.likes.where(likable_id: @target_post).present? + render json: {msg: '你已经喜欢过该文章了'}, status: 400 + else + @current_user.likes.create likable: @target_post + render json:{msg: 'done', likes_count: @target_post.likes_count} + end + end + + def unlike + like = @current_user.likes.find_by(likable_id: @target_post) + if like.present? + like.destroy! + render json:{msg: 'done', likes_count: @target_post.likes_count} + else + render json: {msg: '不能进行该操作'}, status: 400 + end + end + +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0111fb7..7015a8c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,11 +2,7 @@ class ApplicationController < ActionController::Base include ActionController::Serialization serialization_scope :current_user! - # Prevent CSRF attacks by raising an exception. - # For APIs, you may want to use :null_session instead. - # protect_from_forgery with: :exception - protect_from_forgery with: :exception - + skip_before_action :verify_authenticity_token before_action :current_user! def current_user! diff --git a/app/helpers/api/v1/likes_helper.rb b/app/helpers/api/v1/likes_helper.rb new file mode 100644 index 0000000..9b2e137 --- /dev/null +++ b/app/helpers/api/v1/likes_helper.rb @@ -0,0 +1,2 @@ +module Api::V1::LikesHelper +end diff --git a/config/routes.rb b/config/routes.rb index c4800e2..5b2bd99 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,12 +1,6 @@ require 'sidekiq/web' Rails.application.routes.draw do - namespace :api do - namespace :v1 do - resources :posts, only: [:create, :index, :show] - end - end - devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) devise_for :users @@ -81,6 +75,11 @@ Rails.application.routes.draw do post :unfollow, to: 'follow#destroy' end + resources :posts, only: [:create, :index, :show] do + post :like, to: 'likes#like' + post :unlike, to: 'likes#unlike' + end + end end end diff --git a/db/migrate/20150812132728_add_likes_count_to_post.rb b/db/migrate/20150812132728_add_likes_count_to_post.rb new file mode 100644 index 0000000..22b2b58 --- /dev/null +++ b/db/migrate/20150812132728_add_likes_count_to_post.rb @@ -0,0 +1,5 @@ +class AddLikesCountToPost < ActiveRecord::Migration + def change + add_column :posts, :likes_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 7280d91..21162de 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150807130906) do +ActiveRecord::Schema.define(version: 20150812132728) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -100,6 +100,7 @@ ActiveRecord::Schema.define(version: 20150807130906) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "content" + t.integer "likes_count", default: 0 end create_table "share_instances", force: :cascade do |t| diff --git a/spec/controllers/api/v1/likes_controller_spec.rb b/spec/controllers/api/v1/likes_controller_spec.rb new file mode 100644 index 0000000..feb7932 --- /dev/null +++ b/spec/controllers/api/v1/likes_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Api::V1::LikesController, type: :controller do + +end diff --git a/spec/helpers/api/v1/likes_helper_spec.rb b/spec/helpers/api/v1/likes_helper_spec.rb new file mode 100644 index 0000000..6174f45 --- /dev/null +++ b/spec/helpers/api/v1/likes_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Api::V1::LikesHelper. For example: +# +# describe Api::V1::LikesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Api::V1::LikesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end