项目删除用户功能完成;增加项目删除用户接口;修改创建项目接口bug;修改项目列表bug

This commit is contained in:
Yang Zhao 2017-01-01 15:07:16 +00:00
parent b206bfa9b2
commit 273234ddaa
4 changed files with 84 additions and 62 deletions

View File

@ -1,7 +1,14 @@
class ProjectsController < ApplicationController class ProjectsController < ApplicationController
before_action :authenticate before_action :authenticate
skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail,:update] skip_before_action :verify_authenticity_token, :only => [
:index,
:create,
:addUsers,
:detail,
:update,
:deleteUsers
]
#=============================================>获取我的项目列表user_id保存在session中. #=============================================>获取我的项目列表user_id保存在session中.
def index def index
@ -32,21 +39,15 @@ class ProjectsController < ApplicationController
#=============================================>创建项目 #=============================================>创建项目
def create def create
#Todo uid = session[:user_id]
user_id = session[:user_id] project = Project.new(project_params)
user = User.find_by(id: user_id) if project.save
params_project = {content: params[:content],name: params[:name]} Projects_user.create(project_id:project.id,user_id:uid)
project = Project.new(params_project) render status: :created, nothing: true
project.users << user else
project.save render json: project.errors, status: :unprocessable_entity
if project.save end
Projects_user.create(project_id:project.id,user_id:uid) end
render status: :created, nothing: true
else
render json: project.errors, status: :unprocessable_entity
end
#render :json => {:code => 0}
end
#=============================================>项目添加用户 #=============================================>项目添加用户
@ -61,6 +62,7 @@ class ProjectsController < ApplicationController
params_pu = {project_id: pid,user_id: uid} params_pu = {project_id: pid,user_id: uid}
pu = Projects_user.new(params_pu) pu = Projects_user.new(params_pu)
pu.save pu.save
content = User.find_by(id: register).name + "邀请您进入项目" + Project.find_by(id: pid).name content = User.find_by(id: register).name + "邀请您进入项目" + Project.find_by(id: pid).name
puts content puts content
params_note = {user_id: uid,content: content,category: 2,project_id: pid} params_note = {user_id: uid,content: content,category: 2,project_id: pid}
@ -72,39 +74,63 @@ class ProjectsController < ApplicationController
end end
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 = User.find_by(id: register).name + "把您从项目" + Project.find_by(id: pid).name+"中删除"
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 def detail
#Todo #Todo
pid = params[:id] pid = params[:id]
project = Project.find_by(id: pid) project = Project.find_by(id: pid)
#uid = Projects_user.where(:project_id => pid) #uid = Projects_user.where(:project_id => pid)
users = [] users = []
projects_users = Projects_user.where("project_id = ?",pid).all projects_users = Projects_user.where("project_id = ?",pid).all
projects_users.each do |projects_user| projects_users.each do |projects_user|
uid = projects_user.user_id uid = projects_user.user_id
i = User.find_by(id: uid) i = User.find_by(id: uid)
users += [{ users += [{
:nickname => i.name, :nickname => i.name,
:id => i.id :id => i.id
}] }]
end end
#puts users.inspect #puts users.inspect
# Users.find(uid).each do |i| # Users.find(uid).each do |i|
# users += [{ # users += [{
# :nickname => i.name, # :nickname => i.name,
# :id => i.id # :id => i.id
# }] # }]
# end # end
render :json => { render :json => {
:code => 0, :code => 0,
:data => { :data => {
:name => project.name, :name => project.name,
:content => project.content, :content => project.content,
:users => users :users => users
}
} }
}
end end
#=============================================>项目修改 #=============================================>项目修改

View File

@ -70,6 +70,7 @@ Rails.application.routes.draw do
post 'projects' => 'projects#create' post 'projects' => 'projects#create'
patch 'projects' => 'projects#update' patch 'projects' => 'projects#update'
post 'projects/users' => 'projects#addUsers' post 'projects/users' => 'projects#addUsers'
delete 'projects/users' => 'projects#deleteUsers'
get 'projects/detail' => 'projects#detail' get 'projects/detail' => 'projects#detail'
get 'missions/project/status' => 'missions#getlist' get 'missions/project/status' => 'missions#getlist'

View File

@ -163,8 +163,10 @@
var tduser = $("<td></td>").text(users[i].nickname); var tduser = $("<td></td>").text(users[i].nickname);
var btn = $("<button></button>").text("删除"); var btn = $("<button></button>").text("删除");
btn.attr("class","btn btn-primary btn-danger"); btn.attr("class","btn btn-primary btn-danger");
btn.attr("onclick","cy_deleteUser("+users[i].id+")"); btn.attr("onclick","deleteUser("+users[i].id+")");
var tdbtn = $("<td></td>").append(btn); if( users[i].id!=parent.LoginUserInfo.id ){// 不能将自己从项目中删除
var tdbtn = $("<td></td>").append(btn);
}
$("#cy_user_lists").children("tbody").append($("<tr></tr>").append(tduser,tdbtn)); $("#cy_user_lists").children("tbody").append($("<tr></tr>").append(tduser,tdbtn));
} }
} }
@ -233,31 +235,23 @@
}else{ }else{
alert("请输入完整!"); alert("请输入完整!");
} }
// 添加用户
} }
function deleteUser(id){ function deleteUser(id){
return;
$.ajax({ $.ajax({
type:"delete", type:"delete",
dataType:"json", dataType:"json",
url:"/projects.json", url:"/projects/users.json",
data:{ data:{
"project_id":parent.LoginUserInfo.selectProject, "project_id":parent.LoginUserInfo.selectProject,
"user_id":id "user_id":id
//todo (write todo for find, need to be edited)
}, },
statusCode: { statusCode: {
200: function(rpstr) {// 请求成功 200: function(response) {// 请求成功
var robj = eval('('+rpstr+')'); alert("删除成功!");
if(robj["code"]==0){ window.location.reload();
window.location.reload();
}
else{
//todo
}
}, },
401:function(){// 未授权 422:function(){
alert("用户不在项目中!")
} }
} }
}); });

View File

@ -188,6 +188,7 @@
var jstr = htmlobj.responseText; var jstr = htmlobj.responseText;
var robj = eval('('+jstr+')'); var robj = eval('('+jstr+')');
console.log(jstr); console.log(jstr);
var count=0;
if(robj["code"]==0){ if(robj["code"]==0){
var data = robj["data"]; var data = robj["data"];
for(var i=data.length-1;i>=0;--i){ for(var i=data.length-1;i>=0;--i){
@ -208,7 +209,7 @@
tp.find("#cy_template_shares_count").attr("id","cy_shares_count_"+id); tp.find("#cy_template_shares_count").attr("id","cy_shares_count_"+id);
tp.find("#cy_template_enterbtn").attr("id","cy_enter_"+id); tp.find("#cy_template_enterbtn").attr("id","cy_enter_"+id);
tp.find("#cy_template_bg").attr("class","widget-user-header "+bg_color[Math.floor(Math.random()*4)]); tp.find("#cy_template_bg").attr("class","widget-user-header "+bg_color[(count++)%4]);
tp.find("#cy_template_bg").attr("id","cy_bg_"+id); tp.find("#cy_template_bg").attr("id","cy_bg_"+id);