150 lines
4.8 KiB
Ruby
150 lines
4.8 KiB
Ruby
class ProjectsController < ApplicationController
|
||
|
||
before_action :authenticate
|
||
skip_before_action :verify_authenticity_token, :only => [
|
||
:index,
|
||
:create,
|
||
:addUsers,
|
||
:detail,
|
||
:update,
|
||
:deleteUsers
|
||
]
|
||
|
||
#=============================================>获取我的项目列表,user_id保存在session中.
|
||
def index
|
||
#Todo
|
||
uid = session[:user_id]
|
||
data=[]
|
||
Projects_user.where(:user_id => uid).each do |i|
|
||
pid=i.project_id
|
||
p=Project.find_by(id:pid)
|
||
mc = Mission.where(:project_id => pid).size
|
||
uc = Projects_user.where(:project_id => pid).size
|
||
sc = Share.where(:project_id => pid).size
|
||
data+=[{
|
||
:id => pid,
|
||
:name => p.name,
|
||
:content => p.content,
|
||
:mission_count => mc,
|
||
:users_count => uc,
|
||
:shares_count => sc
|
||
}]
|
||
end
|
||
render :json => {
|
||
:code => 0,
|
||
:data => data
|
||
}
|
||
end
|
||
|
||
#=============================================>创建项目
|
||
|
||
def create
|
||
uid = session[:user_id]
|
||
project = Project.new(project_params)
|
||
if project.save
|
||
Projects_user.create(project_id:project.id,user_id:uid)
|
||
render status: :created, nothing: true
|
||
else
|
||
render json: project.errors, status: :unprocessable_entity
|
||
end
|
||
end
|
||
|
||
|
||
#=============================================>项目添加用户
|
||
|
||
def addUsers
|
||
uid = User.find_by(email:params[:email]).id# 需要添加的用户
|
||
register = session[:user_id]# 当前用户
|
||
pid = params[:project_id]
|
||
# 检查该项目中是否已经有要添加的用户
|
||
pu_exist=Projects_user.find_by(project_id:pid,user_id:uid)
|
||
if pu_exist.nil?# 不存在
|
||
params_pu = {project_id: pid,user_id: uid}
|
||
pu = Projects_user.new(params_pu)
|
||
pu.save
|
||
|
||
content = "<a>"+User.find_by(id: register).name + "</a> 邀请您进入项目 <a>" + Project.find_by(id: pid).name+"</a>"
|
||
puts content
|
||
params_note = {user_id: uid,content: content,category: 2,project_id: pid}
|
||
note = Note.new(params_note)
|
||
note.save!
|
||
render :json => {:code => 0}
|
||
else# 存在
|
||
render json:current_user.errors, status: :unprocessable_entity
|
||
end
|
||
end
|
||
|
||
#=============================================>项目删除用户
|
||
def deleteUsers
|
||
uid = params[:user_id]# 需要添加的用户
|
||
register = session[:user_id]# 当前用户
|
||
pid = params[:project_id]
|
||
# 检查该项目中是否已经有要添加的用户
|
||
projects_user=Projects_user.find_by(project_id:pid,user_id:uid)
|
||
if projects_user.nil?# 不存在
|
||
render json:current_user.errors, status: :unprocessable_entity
|
||
end
|
||
# 存在
|
||
puts projects_user.user_id
|
||
puts projects_user.project_id
|
||
Projects_user.where(project_id:pid,user_id:uid).delete_all
|
||
|
||
content ="<a>"+ User.find_by(id: register).name + "</a> 把您从项目 <a>" + Project.find_by(id: pid).name+"</a> 中删除"
|
||
puts content
|
||
params_note = {user_id: uid,content: content,category: 2,project_id: pid}
|
||
note = Note.new(params_note)
|
||
note.save!
|
||
|
||
render :json => {:code => 0}
|
||
end
|
||
|
||
#=============================================>项目详情
|
||
|
||
def detail
|
||
#Todo
|
||
pid = params[:id]
|
||
project = Project.find_by(id: pid)
|
||
#uid = Projects_user.where(:project_id => pid)
|
||
users = []
|
||
projects_users = Projects_user.where("project_id = ?",pid).all
|
||
|
||
projects_users.each do |projects_user|
|
||
uid = projects_user.user_id
|
||
i = User.find_by(id: uid)
|
||
users += [{
|
||
:name => i.name,
|
||
:id => i.id
|
||
}]
|
||
end
|
||
#puts users.inspect
|
||
# Users.find(uid).each do |i|
|
||
# users += [{
|
||
# :nickname => i.name,
|
||
# :id => i.id
|
||
# }]
|
||
# end
|
||
render :json => {
|
||
:code => 0,
|
||
:data => {
|
||
:name => project.name,
|
||
:content => project.content,
|
||
:users => users
|
||
}
|
||
}
|
||
end
|
||
|
||
#=============================================>项目修改
|
||
def update
|
||
project = Project.find_by(:id => params[:project][:id])
|
||
if project.update(project_params)
|
||
render :json => {:code => 0}
|
||
else
|
||
render json:current_user.errors, status: :unprocessable_entity
|
||
end
|
||
end
|
||
|
||
private
|
||
def project_params
|
||
params.require(:project).permit(:name,:content,:id)
|
||
end
|
||
end |