ProjectController 测试完成
This commit is contained in:
parent
bf4a695d76
commit
bdcac94050
|
@ -3,18 +3,25 @@ class ProjectsController < ApplicationController
|
||||||
before_action :authenticate
|
before_action :authenticate
|
||||||
skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail]
|
skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail]
|
||||||
|
|
||||||
#获取我的项目列表,user_id保存在session中.
|
#=============================================>获取我的项目列表,user_id保存在session中.
|
||||||
|
|
||||||
def index
|
def index
|
||||||
#Todo
|
#Todo
|
||||||
uid = session[:user_id]
|
uid = session[:user_id]
|
||||||
pid = Projects_user.find_by(project_id: uid)
|
#pid = Projects_user.find_by(project_id: uid)
|
||||||
mc = Mission.where(:project_id => pid).size
|
pids = []
|
||||||
uc = Projects_user.where(:project_id => pid).size
|
projects = Projects_user.where("user_id = ?",uid).all
|
||||||
sc = Share.where(:project_id => pid).size
|
projects.each do |projects_user|
|
||||||
|
pids << projects_user.project_id
|
||||||
|
end
|
||||||
data = []
|
data = []
|
||||||
Project.find(pid) do |i|
|
pids.each do |pid|
|
||||||
data += [{
|
mc = Mission.where(:project_id => pid).size
|
||||||
|
uc = Projects_user.where(:project_id => pid).size
|
||||||
|
sc = Share.where(:project_id => pid).size
|
||||||
|
puts pid
|
||||||
|
i = Project.find_by(id: pid)
|
||||||
|
data += [{
|
||||||
:id => i.id,
|
:id => i.id,
|
||||||
:name => i.name,
|
:name => i.name,
|
||||||
:content => i.content,
|
:content => i.content,
|
||||||
|
@ -29,62 +36,76 @@ class ProjectsController < ApplicationController
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
#创建项目
|
#=============================================>创建项目
|
||||||
|
|
||||||
def create
|
def create
|
||||||
#Todo
|
#Todo
|
||||||
project = Project.new()
|
user_id = session[:user_id]
|
||||||
project.content = params[:content]
|
user = User.find_by(id: user_id)
|
||||||
project.name = params[:name]
|
params_project = {content: params[:content],name: params[:name]}
|
||||||
now = Time.new
|
project = Project.new(params_project)
|
||||||
project.created_at = now
|
project.users << user
|
||||||
project.updated_at = now
|
project.save
|
||||||
project.save!
|
|
||||||
# 将自己加入项目
|
#project save的时候会保存 projects_users
|
||||||
pu = Project_user.new()
|
|
||||||
pu.project_id = project.id
|
# params_pu = {project_id: project.id,user_id: user.id}
|
||||||
pu.user_id = session[:user_id].to_i
|
# puts params_pu
|
||||||
pu.save!
|
# pu = Projects_user.new(params_pu)
|
||||||
|
# pu.save
|
||||||
render :json => {:code => 0}
|
render :json => {:code => 0}
|
||||||
end
|
end
|
||||||
|
|
||||||
#项目添加用户
|
|
||||||
|
#=============================================>项目添加用户
|
||||||
|
|
||||||
def addUsers
|
def addUsers
|
||||||
#Todo
|
#Todo
|
||||||
uid = params[:user_id].to_i
|
uid = params[:user_id]
|
||||||
pid = params[:project_id].to_i
|
register = session[:user_id]
|
||||||
pu = Project_user.new()
|
#username = params[:username]
|
||||||
pu.project_id = pid
|
pid = params[:project_id]
|
||||||
pu.user_id = uid
|
params_pu = {project_id: pid,user_id: uid}
|
||||||
pu.save!
|
pu = Projects_user.new(params_pu)
|
||||||
note = Note.new
|
pu.save
|
||||||
note.user_id = uid
|
content = User.find_by(id: register).name + "邀请您进入项目" + Project.find_by(id: pid).name
|
||||||
note.project_id = pid
|
puts content
|
||||||
note.category = 2
|
params_note = {user_id: uid,content: content,category: 2,project_id: pid}
|
||||||
note.content = User.find(uid).name + "邀请您进入项目" +
|
note = Note.new(params_note)
|
||||||
Project.find(pid).name
|
note.save!
|
||||||
now = Datetime.now
|
#puts Note.find_by(id: note.id).inspect
|
||||||
note.created_at = now
|
# now = Datetime.now
|
||||||
note.updated_at = now
|
# note.created_at = now
|
||||||
|
# note.updated_at = now
|
||||||
note.save!
|
note.save!
|
||||||
render :json => {:code => 0}
|
render :json => {:code => 0}
|
||||||
end
|
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 = []
|
||||||
Users.find(uid).each do |i|
|
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 += [{
|
users += [{
|
||||||
:nickname => i.name,
|
:nickname => i.name,
|
||||||
:id => i.id
|
:id => i.id
|
||||||
}]
|
}]
|
||||||
end
|
end
|
||||||
|
#puts users.inspect
|
||||||
|
# Users.find(uid).each do |i|
|
||||||
|
# users += [{
|
||||||
|
# :nickname => i.name,
|
||||||
|
# :id => i.id
|
||||||
|
# }]
|
||||||
|
# end
|
||||||
render :json => {
|
render :json => {
|
||||||
:code => 0,
|
:code => 0,
|
||||||
:data => {
|
:data => {
|
||||||
|
|
|
@ -4,7 +4,9 @@ class SessionsController < ApplicationController
|
||||||
before_action :authenticate, except: [ :create ]
|
before_action :authenticate, except: [ :create ]
|
||||||
skip_before_action :verify_authenticity_token, :only => [:create,:destroy,:show]
|
skip_before_action :verify_authenticity_token, :only => [:create,:destroy,:show]
|
||||||
|
|
||||||
|
|
||||||
|
# 用户登录
|
||||||
|
|
||||||
def create
|
def create
|
||||||
user = User.find_by(email: params[:email])
|
user = User.find_by(email: params[:email])
|
||||||
if user && user.authenticate(params[:password])
|
if user && user.authenticate(params[:password])
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
class Project < ActiveRecord::Base
|
class Project < ActiveRecord::Base
|
||||||
has_and_belongs_to_many :users
|
has_and_belongs_to_many :users
|
||||||
|
|
||||||
validate :require_at_least_on_user,on:addUsers # 项目中最少要有一个用户
|
validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户
|
||||||
|
|
||||||
validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
|
validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe MissionsController, type: :controller do
|
||||||
|
|
||||||
|
describe ""
|
||||||
|
|
||||||
|
end
|
|
@ -0,0 +1,83 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe ProjectsController, type: :controller do
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
describe "GET #index " do
|
||||||
|
|
||||||
|
it "return valid projects json" do
|
||||||
|
|
||||||
|
register = create(:user)
|
||||||
|
user = User.find_by(email: register.email)
|
||||||
|
session[:user_id] = user.id
|
||||||
|
|
||||||
|
post :create,content: "project content 1",name: "project1"
|
||||||
|
post :create,content: "project content 2", name: "project2"
|
||||||
|
|
||||||
|
get :index
|
||||||
|
|
||||||
|
puts response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "POST #create" do
|
||||||
|
|
||||||
|
it "can create a valid project" do
|
||||||
|
|
||||||
|
register = create(:user)
|
||||||
|
user = User.find_by(email: register.email)
|
||||||
|
#puts user.inspect
|
||||||
|
session[:user_id] = user.id
|
||||||
|
post :create,content: "project content 1",name: "project1"
|
||||||
|
#puts response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "POST #addUsers" do
|
||||||
|
|
||||||
|
it "can addUsers correctly" do
|
||||||
|
|
||||||
|
register = create(:user)
|
||||||
|
user = User.find_by(email: register.email)
|
||||||
|
session[:user_id] = user.id
|
||||||
|
|
||||||
|
#user1 = create(:user)
|
||||||
|
user2 = create(:user)
|
||||||
|
params = {name: "project addUsers test",content: "addUsers test"}
|
||||||
|
project = Project.new(params)
|
||||||
|
project.users << register
|
||||||
|
project.save
|
||||||
|
#post :create,content: "project content 3",name: "project3"
|
||||||
|
|
||||||
|
post :addUsers,user_id: user2.id,project_id: project.id
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "GET #detail" do
|
||||||
|
|
||||||
|
it "can return correct json format of project " do
|
||||||
|
|
||||||
|
register = create(:user)
|
||||||
|
user = User.find_by(email: register.email)
|
||||||
|
session[:user_id] = user.id
|
||||||
|
|
||||||
|
user1 = create(:user)
|
||||||
|
user2 = create(:user)
|
||||||
|
params = {name: "project addUsers test",content: "addUsers test"}
|
||||||
|
project = Project.new(params)
|
||||||
|
project.users << register
|
||||||
|
project.save
|
||||||
|
post :addUsers,user_id: user2.id,project_id: project.id
|
||||||
|
post :addUsers,user_id: user1.id,project_id: project.id
|
||||||
|
post :detail,id: project.id
|
||||||
|
puts response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -0,0 +1,6 @@
|
||||||
|
require 'faker'
|
||||||
|
FactoryGirl.define do
|
||||||
|
factory :projects_user do
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue