change watchers controller and related routes

This commit is contained in:
sylor_huang@126.com 2020-05-18 14:28:45 +08:00
parent 3a14106cfd
commit a8aae3618b
2 changed files with 36 additions and 17 deletions

View File

@ -1,17 +1,18 @@
class WatchersController < ApplicationController class WatchersController < ApplicationController
before_action :require_login, except: %i[index] before_action :require_login, except: %i[index]
before_action :find_project_with_id # before_action :find_project_with_id
before_action :get_target
def index def index
scope = @project.watchers.includes(:user) scope = @target.watchers.includes(:user)
@watchers = paginate(scope) @watchers = paginate(scope)
end end
def unfollow def unfollow
begin begin
return normal_status(2, "你还没有关注哦") unless current_user.watched?(@project) return normal_status(2, "你还没有关注哦") unless current_user.watched?(@target)
current_user.unwatch!(@project) current_user.unwatch!(@target)
render_ok({watchers_count: @project.watchers_count, watched: current_user.watched?(@project)}) render_ok({watchers_count: @target.watchers_count, watched: false})
rescue Exception => e rescue Exception => e
uid_logger_error(e.message) uid_logger_error(e.message)
tip_exception(e.message) tip_exception(e.message)
@ -21,9 +22,9 @@ class WatchersController < ApplicationController
def follow def follow
begin begin
return normal_status(2, "你已关注了") if current_user.watched?(@project) return normal_status(2, "你已关注了") if current_user.watched?(@target)
current_user.watch!(@project) current_user.watch!(@target)
render_ok({watchers_count: @project.watchers_count, watched: current_user.watched?(@project)}) render_ok({watchers_count: @target.watchers_count, watched: true})
rescue Exception => e rescue Exception => e
uid_logger_error(e.message) uid_logger_error(e.message)
tip_exception(e.message) tip_exception(e.message)
@ -32,11 +33,28 @@ class WatchersController < ApplicationController
end end
def check_watch def check_watch
is_watch = current_user.watched?(@project) is_watch = current_user.watched?(@target)
render_result(is_watch ? 1 : 0) render_result(is_watch ? 1 : 0)
rescue Exception => e rescue Exception => e
uid_logger_error(e.message) uid_logger_error(e.message)
tip_exception(e.message) tip_exception(e.message)
end end
private
def get_target
target_type = params[:target_type].to_s
case target_type
when "project"
@target = target_type.capitalize.constantize.find_by(id: params[:id])
else
@target = target_type.capitalize.constantize.find_by(login: params[:id]) #用户
end
unless @target.present?
normal_status(-1, "目标不存在")
end
end
end end

View File

@ -45,6 +45,13 @@ Rails.application.routes.draw do
resources :ignores, only: [:index, :show] resources :ignores, only: [:index, :show]
resources :licenses, only: [:index, :show] resources :licenses, only: [:index, :show]
resources :watchers, only: [:index] do
collection do
post :follow
delete :unfollow
get :check_watch
end
end
resources :projects do resources :projects do
resources :pull_requests, except: [:destroy] do resources :pull_requests, except: [:destroy] do
member do member do
@ -75,13 +82,7 @@ Rails.application.routes.draw do
post :update_status post :update_status
end end
end end
resources :watchers, only: [:index] do
collection do
post :follow
delete :unfollow
get :check_watch
end
end
resources :praise_tread, only: [:index] do resources :praise_tread, only: [:index] do
collection do collection do
post :like post :like