mirror of
https://github.com/wahyd4/libr-2.git
synced 2026-08-09 05:15:53 +10:00
add post like and unlike action
This commit is contained in:
@@ -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/
|
||||
@@ -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/
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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!
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
module Api::V1::LikesHelper
|
||||
end
|
||||
+5
-6
@@ -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
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddLikesCountToPost < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :posts, :likes_count, :integer, default: 0
|
||||
end
|
||||
end
|
||||
+2
-1
@@ -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|
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::V1::LikesController, type: :controller do
|
||||
|
||||
end
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user