controller 测试version1.0

This commit is contained in:
HuJiaxuan 2017-01-02 02:57:57 +00:00
parent 774c7a5fa8
commit 1670e845d7
11 changed files with 332 additions and 155 deletions

View File

@ -14,7 +14,7 @@ class MissionsController < ApplicationController
missions.each do |mission| missions.each do |mission|
puts mission.inspect #puts mission.inspect
details += [{ details += [{
:id => mission.id, :id => mission.id,
@ -41,19 +41,28 @@ class MissionsController < ApplicationController
def detail def detail
#Todo #Todo
username = []
comments_s = []
mission_id = params[:id] mission_id = params[:id]
mission = Mission.find_by(id: mission_id) mission = Mission.find_by(id: mission_id)
userid = Mission_user.find_by_name("mission_id") m_us = Missions_user.where("mission_id = ?",mission_id).all
username = User.find(userid).pluck("name") #puts m_us.inspect
comments = [] m_us.each do |m_u|
Comments.where("mission_id" => id).each do |i| user_id = m_u.user_id
name = User.find(i.user_id).name user = User.find_by(id: user_id)
comments += [{ #puts user.inspect
:nickname => name, username << user.name
:content => i.content, end
:time => i.created_at comments = Comment.where("mission_id = ?",mission_id).all
comments.each do |c|
uname = User.find_by(id: c.user_id).name
comments_s += [{
:nickname => uname,
:content => c.content,
:time => c.created_at
}] }]
end end
#puts comments_s.inspect
render :json => { render :json => {
:code => 0, :code => 0,
:data => { :data => {
@ -63,41 +72,60 @@ class MissionsController < ApplicationController
:status => mission.status, :status => mission.status,
:deadline => mission.deadline, :deadline => mission.deadline,
:users => username, :users => username,
:comments => comments :comments => comments_s
} }
} }
end
#======================================>创建任务
def create
#Todo
params_mission = {name: params[:name],project_id: params[:project_id],content: params[:content],deadline: params[:deadline],priority: params[:priority],status: params[:status]}
mission = Mission.new(params_mission)
mission.save
#puts mission.inspect
#puts mission.id
usernames = params[:users]
usernames.each do |name|
user_enties = User.find_by(name: name)
#puts user_enties.inspect
params_mu = {mission_id: mission.id,user_id: user_enties.id}
missions_user = Missions_user.new(params_mu)
missions_user.save
content = "有新任务:"+mission.name
params_note = {content: content,user_id: user_enties.id,category: 3,project_id: mission.project_id}
note = Note.new(params_note)
note.save
end
render :json => {:code => 0}
end end
#======================================>修改任务信息 #======================================>修改任务信息
def update def update
#Todo #Todo
id = params[:id] id = params[:id]
now = Datetime.now #now = Datetime.now
mission = Mission.find(id) mission = Mission.find_by(id: id)
mission.name = params[:name] params_mission = {name: params[:name],content: params[:content],deadline: params[:deadline],priority: params[:priority],status: params[:status]}
mission.content = params[:content] #puts params_mission
mission.priority = params[:priority] mission.update(params_mission)
mission.status = params[:status] mission.save
mission.deadline = params[:deadline] Missions_user.delete_all(["mission_id = ?",id])
mission.updated_at = now usernames = params[:users]
mission.save! usernames.each do |name|
pid = mission.project_id user_enties = User.find_by(name: name)
Mission_user.destory_all(:mission_id => id) params = {mission_id: id,user_id: user_enties.id}
params[:users].each do |i| missions_user = Missions_user.new(params)
uid = User.where(:email => i).id missions_user.save
mu = Mission_user.new content = mission.name + "——任务信息发生了变化"
mu.mission_id = id
mu.user_id = uid params_note = {content: content,user_id: user_enties.id,category: 3,project_id: mission.project_id}
mu.save! note = Note.new(params_note)
note = Note.new note.save
note.project_id = pid
note.user_id = uid
note.category = 3
note.created_at = now
note.updated_at = now
note.content = "任务信息发生了变化"
note.save!
end end
render :json => {:code => 0} render :json => {:code => 0}
end end
@ -106,22 +134,15 @@ class MissionsController < ApplicationController
def commentPublish def commentPublish
#Todo #Todo
puts "I'm in"
content = params[:content]
mission_id = params[:mission_id] mission_id = params[:mission_id]
content = params[:content]
user_id = session[:user_id] user_id = session[:user_id]
puts user_id params = {content: content, mission_id: mission_id, user_id: user_id}
params_comment = {content: content,mission_id: mission_id,user_id: user_id} comment = Comment.new(params)
puts params_comment comment.save
comment = Comments.new(params_comment)
# now = Datetime.now
# put.created_at = now
# put.updated_at = now
comment.save!
render :json => {:code => 0} render :json => {:code => 0}
end end
end end

View File

@ -8,23 +8,24 @@ class NotesController < ApplicationController
#Todo #Todo
pid = params[:project_id] pid = params[:project_id]
uid = session[:user_id] uid = session[:user_id]
note = {} notes = Note.where("user_id = ? AND project_id = ? ",uid,pid).order(:created_at).all
Note.where(:user_id => uid).where(:project_id => pid).each do |i| note = Hash.new do |h,k|
date = i.created_at.to_date.to_s h[k] = []
if not note[date] then end
note[date] = [] notes.each do |n|
end date = n.created_at.to_s[/([\d\-]*)\s(.*)/,1]
note[:date] += [{ time = n.created_at.to_s[/(.*)\s([\d\:]*)/,2]
:content => i.content, note[date] << {
:time => i.created_at.to_time, :content => n.content,
:type => i.category :time => time,
}] :type => n.category
}
end end
data = [] data = []
note.each do |date, list| note.each do |k,v|
data += [{ data += [{
:time => date, :time => k,
:notes => list :notes => v
}] }]
end end
render :json => { render :json => {

View File

@ -32,15 +32,12 @@ class ProjectsController < ApplicationController
#=============================================>创建项目 #=============================================>创建项目
def create def create
#Todo
user_id = session[:user_id] user_id = session[:user_id]
user = User.find_by(id: user_id)
params_project = {content: params[:content],name: params[:name]} params_project = {content: params[:content],name: params[:name]}
project = Project.new(params_project) project = Project.new(params_project)
project.users << user
project.save
if project.save if project.save
Projects_user.create(project_id:project.id,user_id:uid) Projects_user.create(project_id:project.id,user_id:user_id)
render status: :created, nothing: true render status: :created, nothing: true
else else
render json: project.errors, status: :unprocessable_entity render json: project.errors, status: :unprocessable_entity
@ -52,7 +49,7 @@ class ProjectsController < ApplicationController
#=============================================>项目添加用户 #=============================================>项目添加用户
def addUsers def addUsers
#Todo
uid = params[:user_id] uid = params[:user_id]
register = session[:user_id] register = session[:user_id]
#username = params[:username] #username = params[:username]

View File

@ -6,26 +6,18 @@ class SharesController < ApplicationController
def create def create
#Todo #Todo
uid = session[:user_id].to_i uid = session[:user_id]
pid = params[:project_id].to_i pid = params[:project_id]
now = Datetime.now
put = Share.new() params_share = {content: params[:content],project_id: params[:project_id],user_id: uid}
put.content = params[:content] share = Share.new(params_share)
put.project_id = pid share.save
put.user_id = uid note_content = User.find_by(id: uid).name + "分享了一些事"
put.created_at = now
put.updated_at = now Projects_user.where(:project_id => pid).all.each do |pu|
put.save! params_note = {content: note_content,category: 1,project_id: pu.project_id,user_id: pu.user_id}
content = User.find(uid).name + "分享了一些事" note = Note.new(params_note)
Project_user.where(:project_id => pid).each do |tuid| note.save
note = Note.new
note.user_id = tuid
note.project_id = pid
note.category = 1
note.content = content
note.created_at = now
note.updated_at = now
note.save!
end end
render :json => {:code => 0} render :json => {:code => 0}
end end
@ -35,12 +27,16 @@ class SharesController < ApplicationController
def getShareList def getShareList
#Todo #Todo
pid = params[:project_id] pid = params[:project_id]
data = [] data = []
Share.where(:project_id => pid) do |i|
name = User.find(i.user_id).name Share.where(:project_id => pid).all.each do |i|
name = User.find_by(id: i.user_id).name
date = i.created_at.to_s[/([\d\-]*)\s(.*)/,1]
data += [{ data += [{
:nickname => name, :nickname => name,
:time => i.created_at, :time => date,
:content => i.content :content => i.content
}] }]
end end

View File

@ -1,11 +1,12 @@
class Comment < ActiveRecord:: Base class Comment < ActiveRecord::Base
belongs_to :mission belongs_to :mission
validates :content, presence: true validates :content, presence: true
validates :mission_id, presence: true validates :mission_id, presence: true
validates :user_id, presence: true validates :user_id, presence: true
validate :comment_has_atleast_one_character, on: :create
validate :user_id_exist, on: :create validate :user_id_exist, on: :create
private private

View File

@ -1,6 +1,6 @@
class Missions_user <ActiveRecord:: Base class Missions_user <ActiveRecord:: Base
validates :mission_id, presence: true, uniqueness: true validates :mission_id, presence: true
validates :user_id, presence: true validates :user_id, presence: true
validate :mission_exist, on: :create validate :mission_exist, on: :create

View File

@ -1,12 +1,8 @@
class Project < ActiveRecord::Base class Project < ActiveRecord::Base
has_and_belongs_to_many :users has_and_belongs_to_many :users
<<<<<<< HEAD
validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户 validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户
=======
validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户
>>>>>>> 27ebee595014926419b6ccba922ad39450b73f6b
validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
private private

View File

@ -64,15 +64,19 @@ Rails.application.routes.draw do
get 'session' => 'sessions#show' get 'session' => 'sessions#show'
post 'shares' => 'shares#create' post 'shares' => 'shares#create'
get 'shares' => 'share#getShareList' get 'shares' => 'shares#getShareList'
get 'projects' => 'projects#index' get 'projects' => 'projects#index'
post 'projects' => 'projects#create' post 'projects' => 'projects#create'
post 'projects/users' => 'projects#addUsers' post 'projects/users' => 'projects#addUsers'
get 'projects/detail' => 'projects#detail' get 'projects/detail' => 'projects#detail'
get 'notes/list' => 'notes#getNotesList'
get 'missions/project/status' => 'missions#getlist' get 'missions/project/status' => 'missions#getlist'
get 'missions/detail' => 'missions#detail' get 'missions/detail' => 'missions#detail'
patch 'missions/detail' => 'missions#update'
post 'missions/comments' => 'missions#commentPublish' post 'missions/comments' => 'missions#commentPublish'
post 'missions/create' => 'missions#create'
patch 'missions/detail' => 'missions#update'
end end

View File

@ -2,62 +2,14 @@ require 'rails_helper'
RSpec.describe MissionsController, type: :controller do RSpec.describe MissionsController, type: :controller do
# describe "GET #getlist" do describe "GET #getlist" do
# it "return a valid json format of missions" do it "return a valid json format of missions" 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
# params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id}
# params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id}
# mission = Mission.new(params_mission)
# mission2 = Mission.new(params_mission2)
# mission.save
# mission2.save
# get :getlist,project_id: project.id,status_type: 1
# puts response.body
# end
# end
# describe "GET #detail" do
# it "return a correct json format detail" do
# register = create(:user)
# params = {name: "project addUsers test",content: "addUsers test"}
# project = Project.new(params)
# project.users << register
# project.save
# params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id}
# params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id}
# mission = Mission.new(params_mission)
# mission.save
# get :detail,id: mission.id
# end
# end
describe "POST #comments" do
it "can publish comments successfully" do
register = create(:user) register = create(:user)
user = User.find_by(email: register.email)
session[:user_id] = user.id
params = {name: "project addUsers test",content: "addUsers test"} params = {name: "project addUsers test",content: "addUsers test"}
project = Project.new(params) project = Project.new(params)
project.users << register project.users << register
@ -66,14 +18,133 @@ RSpec.describe MissionsController, type: :controller do
params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id} params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id}
params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id} params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id}
mission = Mission.new(params_mission) mission = Mission.new(params_mission)
mission2 = Mission.new(params_mission2)
mission.save
mission2.save
get :getlist,project_id: project.id,status_type: 1
puts response.body
end
end
describe "GET #detail" do
it "return a correct json format detail" do
register = create(:user)
session[:user_id] = register.id
params = {name: "project addUsers test",content: "addUsers test"}
project = Project.new(params)
project.users << register
project.save
params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id}
#params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id}
mission = Mission.new(params_mission)
mission.save mission.save
post :comments , mission_id: mission.id,content: "用户评论" post :commentPublish,content: "评论1",mission_id: mission.id
post :commentPublish,content: "评论2",mission_id: mission.id
get :detail,id: mission.id
puts "hello world"
puts response.body puts response.body
end end
end end
describe "POST #comments" do
it "can publish comments successfully" do
register = create(:user)
params = {name: "project addUsers test",content: "addUsers test"}
session[:user_id] = register.id
project = Project.new(params)
project.users << register
project.save
params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id}
mission = Mission.new(params_mission)
mission.save
post :commentPublish,mission_id: mission.id,content: "asda"
puts response.body
end
end
describe "POST #update" do
it "can update missions successfully" do
register = create(:user)
user = User.find_by(email: register.email)
session[:user_id] = user.id
params = {name: "project mission update test",content: "update test"}
project = Project.new(params)
project.users << register
project.save
user1 = create(:user,name: "yangzhao")
user2 = create(:user,name: "chenyi")
user3 = create(:user,name: "yangzhao")
user4 = create(:user,name: "chenyi")
puts user1.inspect
params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id}
mission = Mission.new(params_mission)
mission.save
post :create,project_id: project.id,name: "前端原型开发",content:"开发出前端原型",priority:1,status:1,deadline:"2017-01-02 14:14:54",users: [user1.name,user2.name]
patch :update,name: "前端原型开发",id: mission.id,content:"完善前端原型",priority:1,status:1,deadline:"2017-01-03 14:14:54",users: [user3.name,user4.name]
post :detail,id: 1
puts response.body
end
end
describe "POST #create" do
it "can create missions successfully" do
register = create(:user)
user = User.find_by(email: register.email)
session[:user_id] = user.id
params = {name: "project mission update test",content: "update test"}
project = Project.new(params)
project.users << register
project.save
user1 = create(:user)
user2 = create(:user)
project.users << user1
project.users << user2
#params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id}
post :create,project_id: project.id,name: "前端原型开发",content:"开发出前端原型",priority:1,status:1,deadline:"2017-01-02 10:00:00",users: [user1.name,user2.name]
post :detail,id: 1
puts response.body
end
end
end end

View File

@ -0,0 +1,34 @@
require 'rails_helper'
RSpec.describe NotesController,type: :controller do
describe "GET #getNotesList" do
it "return correct json format of noteslist" do
register = create(:user)
user = User.find_by(email: register.email)
session[:user_id] = user.id
params = {name: "project mission update test",content: "update test"}
project = Project.new(params)
project.users << register
project.save
params_note = {content: "note content 1",user_id: user.id,category: 2,project_id: project.id}
note = Note.new(params_note)
note.save
params_note2 = {content: "note content 2",user_id: user.id,category: 1,project_id: project.id}
note2 = Note.new(params_note2)
note2.save
post :getNotesList,project_id: project.id
puts response.body
end
end
end

View File

@ -0,0 +1,56 @@
require 'rails_helper'
RSpec.describe SharesController,type: :controller do
describe "POST #create" do
it "create a correct share" do
register = create(:user)
user = User.find_by(email: register.email)
session[:user_id] = user.id
params = {name: "project addUsers test",content: "addUsers test"}
project = Project.new(params)
project.users << register
project.save
post :create,project_id: project.id,content: "苟利国家生死以,岂因福祸避趋之"
end
end
describe "GET #getShareList" do
it "return correct share list" do
register = create(:user)
user = User.find_by(email: register.email)
session[:user_id] = user.id
params = {name: "project test",content: " test"}
project = Project.new(params)
project.users << register
project.save
post :create,project_id: project.id,content: "苟利国家生死以,岂因福祸避趋之"
post :create,project_id: project.id,content: "too young too simple"
post :create,project_id: project.id,content: "too young too naive"
get :getShareList,project_id: project.id
puts response.body
end
end
end