154 lines
4.5 KiB
Ruby
154 lines
4.5 KiB
Ruby
class MissionsController < ApplicationController
|
|
|
|
before_action :authenticate
|
|
skip_before_action :verify_authenticity_token, :only => [
|
|
:create,
|
|
:getlist,
|
|
:detail,
|
|
:update,
|
|
:commentPublish
|
|
]
|
|
#======================================>获取当前项目某状态的任务列表
|
|
|
|
def getlist
|
|
#Todo
|
|
pid = params[:project_id]
|
|
stu = params[:status_type]
|
|
#missions = []
|
|
details = []
|
|
missions = Mission.where("project_id = ? AND status = ?",pid,stu).all
|
|
|
|
missions.each do |mission|
|
|
|
|
#puts mission.inspect
|
|
|
|
details += [{
|
|
:id => mission.id,
|
|
:name => mission.name,
|
|
:content => mission.content
|
|
}]
|
|
|
|
end
|
|
|
|
# Mission.where(:project_id => pid).where(:status => stu) do |i|
|
|
# missions += [{
|
|
# :id => i.id,
|
|
# :name => i.name,
|
|
# :content => i.content
|
|
# }]
|
|
# end
|
|
render :json => {
|
|
:code => 0,
|
|
:data => missions
|
|
}
|
|
end
|
|
|
|
#======================================>获取任务详细信息
|
|
|
|
def detail
|
|
#Todo
|
|
username = []
|
|
comments_s = []
|
|
mission_id = params[:id]
|
|
mission = Mission.find_by(id: mission_id)
|
|
m_us = Missions_user.where("mission_id = ?",mission_id).all
|
|
#puts m_us.inspect
|
|
m_us.each do |m_u|
|
|
user_id = m_u.user_id
|
|
user = User.find_by(id: user_id)
|
|
#puts user.inspect
|
|
username << user.name
|
|
end
|
|
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
|
|
#puts comments_s.inspect
|
|
render :json => {
|
|
:code => 0,
|
|
:data => {
|
|
:name => mission.name,
|
|
:content => mission.content,
|
|
:priority => mission.priority,
|
|
:status => mission.status,
|
|
:deadline => mission.deadline,
|
|
:users => username,
|
|
: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)
|
|
if mission.save
|
|
render :json => {:code => 0}
|
|
else
|
|
render json: mission.errors, status: :unprocessable_entity
|
|
end
|
|
userids = params[:users]
|
|
userids.each do |id|
|
|
user_enties = User.find_by(id: id)
|
|
#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 = "有新任务 <a>"+mission.name+"</a>"
|
|
|
|
params_note = {content: content,user_id: user_enties.id,category: 3,project_id: mission.project_id}
|
|
note = Note.new(params_note)
|
|
note.save
|
|
end
|
|
end
|
|
|
|
#======================================>修改任务信息
|
|
|
|
def update
|
|
#Todo
|
|
|
|
id = params[:id]
|
|
#now = Datetime.now
|
|
mission = Mission.find_by(id: id)
|
|
params_mission = {name: params[:name],content: params[:content],deadline: params[:deadline],priority: params[:priority]}
|
|
#puts params_mission
|
|
mission.update(params_mission)
|
|
mission.save
|
|
Missions_user.delete_all(["mission_id = ?",id])
|
|
userids = params[:users]
|
|
userids.each do |name|
|
|
user_enties = User.find_by(id: id)
|
|
params = {mission_id: id,user_id: user_enties.id}
|
|
missions_user = Missions_user.new(params)
|
|
missions_user.save
|
|
content = "<a>"+mission.name + "</a> 任务信息发生了变化"
|
|
|
|
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
|
|
|
|
#======================================>当前用户发表评论
|
|
|
|
def commentPublish
|
|
#Todo
|
|
mission_id = params[:mission_id]
|
|
content = params[:content]
|
|
user_id = session[:user_id]
|
|
params = {content: content, mission_id: mission_id, user_id: user_id}
|
|
comment = Comment.new(params)
|
|
if comment.save
|
|
render :json => {:code => 0}
|
|
else
|
|
render json: mission.errors, status: :unprocessable_entity
|
|
end
|
|
end
|
|
end |