ProjectController 测试完成
This commit is contained in:
parent
bf4a695d76
commit
bdcac94050
|
@ -3,17 +3,24 @@ class ProjectsController < ApplicationController
|
|||
before_action :authenticate
|
||||
skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail]
|
||||
|
||||
#获取我的项目列表,user_id保存在session中.
|
||||
#=============================================>获取我的项目列表,user_id保存在session中.
|
||||
|
||||
def index
|
||||
#Todo
|
||||
uid = session[:user_id]
|
||||
pid = Projects_user.find_by(project_id: uid)
|
||||
#pid = Projects_user.find_by(project_id: uid)
|
||||
pids = []
|
||||
projects = Projects_user.where("user_id = ?",uid).all
|
||||
projects.each do |projects_user|
|
||||
pids << projects_user.project_id
|
||||
end
|
||||
data = []
|
||||
pids.each do |pid|
|
||||
mc = Mission.where(:project_id => pid).size
|
||||
uc = Projects_user.where(:project_id => pid).size
|
||||
sc = Share.where(:project_id => pid).size
|
||||
data = []
|
||||
Project.find(pid) do |i|
|
||||
puts pid
|
||||
i = Project.find_by(id: pid)
|
||||
data += [{
|
||||
:id => i.id,
|
||||
:name => i.name,
|
||||
|
@ -29,62 +36,76 @@ class ProjectsController < ApplicationController
|
|||
}
|
||||
end
|
||||
|
||||
#创建项目
|
||||
#=============================================>创建项目
|
||||
|
||||
def create
|
||||
#Todo
|
||||
project = Project.new()
|
||||
project.content = params[:content]
|
||||
project.name = params[:name]
|
||||
now = Time.new
|
||||
project.created_at = now
|
||||
project.updated_at = now
|
||||
project.save!
|
||||
# 将自己加入项目
|
||||
pu = Project_user.new()
|
||||
pu.project_id = project.id
|
||||
pu.user_id = session[:user_id].to_i
|
||||
pu.save!
|
||||
user_id = session[:user_id]
|
||||
user = User.find_by(id: user_id)
|
||||
params_project = {content: params[:content],name: params[:name]}
|
||||
project = Project.new(params_project)
|
||||
project.users << user
|
||||
project.save
|
||||
|
||||
#project save的时候会保存 projects_users
|
||||
|
||||
# params_pu = {project_id: project.id,user_id: user.id}
|
||||
# puts params_pu
|
||||
# pu = Projects_user.new(params_pu)
|
||||
# pu.save
|
||||
render :json => {:code => 0}
|
||||
end
|
||||
|
||||
#项目添加用户
|
||||
|
||||
#=============================================>项目添加用户
|
||||
|
||||
def addUsers
|
||||
#Todo
|
||||
uid = params[:user_id].to_i
|
||||
pid = params[:project_id].to_i
|
||||
pu = Project_user.new()
|
||||
pu.project_id = pid
|
||||
pu.user_id = uid
|
||||
pu.save!
|
||||
note = Note.new
|
||||
note.user_id = uid
|
||||
note.project_id = pid
|
||||
note.category = 2
|
||||
note.content = User.find(uid).name + "邀请您进入项目" +
|
||||
Project.find(pid).name
|
||||
now = Datetime.now
|
||||
note.created_at = now
|
||||
note.updated_at = now
|
||||
uid = params[:user_id]
|
||||
register = session[:user_id]
|
||||
#username = params[:username]
|
||||
pid = params[:project_id]
|
||||
params_pu = {project_id: pid,user_id: uid}
|
||||
pu = Projects_user.new(params_pu)
|
||||
pu.save
|
||||
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!
|
||||
#puts Note.find_by(id: note.id).inspect
|
||||
# now = Datetime.now
|
||||
# note.created_at = now
|
||||
# note.updated_at = now
|
||||
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)
|
||||
#uid = Projects_user.where(:project_id => pid)
|
||||
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 += [{
|
||||
:nickname => 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 => {
|
||||
|
|
|
@ -5,6 +5,8 @@ class SessionsController < ApplicationController
|
|||
skip_before_action :verify_authenticity_token, :only => [:create,:destroy,:show]
|
||||
|
||||
|
||||
# 用户登录
|
||||
|
||||
def create
|
||||
user = User.find_by(email: params[:email])
|
||||
if user && user.authenticate(params[:password])
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
class Project < ActiveRecord::Base
|
||||
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
|
||||
|
||||
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