2016-07-25 15:20:01 +08:00
#coding=utf-8
module Mobile
module Apis
class Projects < Grape :: API
resources :projects do
desc " 获取项目列表 "
params do
requires :token , type : String
end
get do
authenticate!
ps = ProjectsService . new
projects = ps . user_projects ( current_user )
present :data , projects , with : Mobile :: Entities :: Project , user : current_user
present :status , 0
end
desc " 返回单个项目 "
params do
requires :id , type : Integer
requires :token , type : String
end
route_param :id do
get do
# course = Course.find(params[:id])
ps = ProjectsService . new
project = ps . show_project ( params , current_user )
2016-07-25 17:02:42 +08:00
if project [ :status ] == 9
2016-07-25 15:20:01 +08:00
{ status : - 1 , message : '该项目不存在或已被删除啦' }
else
present :data , project , with : Mobile :: Entities :: Project , user : current_user
present :status , 0
end
end
end
desc " 获取项目动态 "
params do
2016-07-25 17:02:42 +08:00
requires :id , type : Integer
2016-07-25 15:20:01 +08:00
requires :token , type : String
end
2016-07-25 17:02:42 +08:00
post 'activities' do
2016-07-25 15:20:01 +08:00
authenticate!
2016-07-25 17:02:42 +08:00
user = current_user
2016-07-25 15:20:01 +08:00
2016-07-25 17:02:42 +08:00
project_types = " ('Message','Issue','Project') "
activities = UserActivity . where ( " (container_type = 'Project' and container_id = #{ params [ :id ] } and act_type in #{ project_types } ) " ) . order ( 'updated_at desc' )
2016-07-25 15:20:01 +08:00
2016-07-25 17:02:42 +08:00
page = params [ :page ] ? params [ :page ] : 0
all_count = activities . count
activities = activities . limit ( 10 ) . offset ( page * 10 )
count = activities . count
present :data , activities , with : Mobile :: Entities :: Activity , user : user
present :all_count , all_count
present :count , count
present :page , page
present :status , 0
2016-07-25 15:20:01 +08:00
end
2016-07-27 14:30:19 +08:00
desc " 获取项目成员 "
2016-07-25 15:20:01 +08:00
params do
2016-07-27 14:30:19 +08:00
requires :id , type : Integer
2016-07-25 15:20:01 +08:00
requires :token , type : String
end
2016-07-27 14:30:19 +08:00
post 'members' do
2016-07-25 15:20:01 +08:00
authenticate!
2016-07-27 14:30:19 +08:00
project = Project . find ( " #{ params [ :id ] } " )
members = project . member_principals
master_members = project . member_principals . includes ( :roles , :principal ) . where ( " member_roles.role_id=3 " ) . all . sort
2016-07-25 15:20:01 +08:00
2016-07-27 14:30:19 +08:00
master_members . each do | m |
if m . user_id == project . user_id
master_members . delete ( m )
master_members . insert ( 0 , m )
break
end
2016-07-25 15:20:01 +08:00
end
2016-07-27 14:30:19 +08:00
develop_members = project . member_principals . includes ( :roles , :principal ) . where ( " member_roles.role_id=4 " ) . all . sort
report_members = project . member_principals . includes ( :roles , :principal ) . where ( " member_roles.role_id=5 " ) . all . sort
present :master_members , master_members , with : Mobile :: Entities :: ProjectMember
present :develop_members , develop_members , with : Mobile :: Entities :: ProjectMember
present :report_members , report_members , with : Mobile :: Entities :: ProjectMember
present :status , 0
2016-07-25 15:20:01 +08:00
end
2016-07-27 14:30:19 +08:00
desc " 获取项目某成员角色信息 "
2016-07-25 15:20:01 +08:00
params do
2016-07-27 14:30:19 +08:00
requires :id , type : Integer
2016-07-25 15:20:01 +08:00
requires :token , type : String
2016-07-27 14:30:19 +08:00
requires :user_id , type : Integer
2016-07-25 15:20:01 +08:00
end
2016-07-27 14:30:19 +08:00
post 'get_member_info' do
2016-07-25 15:20:01 +08:00
authenticate!
2016-07-27 14:30:19 +08:00
project = Project . find ( " #{ params [ :id ] } " )
2016-07-25 15:20:01 +08:00
2016-07-27 14:30:19 +08:00
my_member = project . member_principals . where ( " users.id= #{ params [ :user_id ] } " ) . first
2016-07-25 15:20:01 +08:00
2016-07-27 14:30:19 +08:00
if my_member && my_member . roles [ 0 ]
present :project_id , params [ :id ]
present :user_id , params [ :user_id ]
present :member_info , my_member , with : Mobile :: Entities :: ProjectMember
2016-07-25 15:20:01 +08:00
present :status , 0
2016-07-27 14:30:19 +08:00
else
present :status , - 1
2016-07-25 15:20:01 +08:00
end
end
2016-07-27 14:30:19 +08:00
desc " 修改项目某成员角色信息 "
2016-07-25 15:20:01 +08:00
params do
2016-07-27 14:30:19 +08:00
requires :id , type : Integer
2016-07-25 15:20:01 +08:00
requires :token , type : String
2016-07-27 14:30:19 +08:00
requires :user_id , type : Integer
requires :role_id , type : Integer
2016-07-25 15:20:01 +08:00
end
2016-07-27 14:30:19 +08:00
post 'edit_member_role' do
2016-07-25 15:20:01 +08:00
authenticate!
2016-07-27 14:30:19 +08:00
project = Project . find ( " #{ params [ :id ] } " )
2016-07-25 15:20:01 +08:00
2016-07-27 14:30:19 +08:00
my_member = project . member_principals . where ( " users.id= #{ current_user . id } " ) . first
2016-07-25 15:20:01 +08:00
2016-07-27 14:30:19 +08:00
#3管理 4开发 5报告
if ! ( my_member && my_member . roles [ 0 ] && my_member . roles [ 0 ] . id == 3 ) || project . user_id == params [ :user_id ] || ! ( params [ :role_id ] == 3 || params [ :role_id ] == 4 || params [ :role_id ] == 5 )
present :status , - 1
2016-07-25 15:20:01 +08:00
else
2016-07-28 14:49:45 +08:00
ps = ProjectsService . new
2016-07-27 14:30:19 +08:00
2016-07-28 14:49:45 +08:00
status = ps . modify_user_project_role params
present :status , status
2016-07-25 15:20:01 +08:00
end
end
2016-07-27 14:30:19 +08:00
desc " 新建项目 "
params do
requires :token , type : String
requires :name , type : String , desc : '项目名称'
end
post 'create' do
authenticate!
2016-07-28 14:49:45 +08:00
ps = ProjectsService . new
status = ps . createNewProject params , current_user
2016-07-27 14:30:19 +08:00
present :status , 0
end
desc " 加入项目 "
params do
requires :token , type : String
requires :invite_code , type : String , desc : '邀请码'
end
2016-07-28 14:49:45 +08:00
post " join " do
2016-07-27 14:30:19 +08:00
authenticate!
2016-07-28 14:49:45 +08:00
# ps = ProjectsService.new
# status = ps.join_project({role: "5", openid: params[:openid], invite_code: params[:invite_code]}, current_user)
#
# present :status, status
2016-07-27 14:30:19 +08:00
2016-07-28 17:26:57 +08:00
{ status : - 1 , message : '该功能将在近日开放,敬请期待!' }
2016-07-27 14:30:19 +08:00
end
end
2016-07-25 15:20:01 +08:00
end
end
end