#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) if project[:status] == 9 {status:-1, message: '该项目不存在或已被删除啦' } else present :data, project, with: Mobile::Entities::Project,user: current_user present :status, 0 end end end desc "获取项目动态" params do requires :id, type: Integer requires :token, type: String end post 'activities' do authenticate! user = current_user 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') 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 end desc "获取项目成员" params do requires :id, type: Integer requires :token, type: String end post 'members' do authenticate! 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 master_members.each do |m| if m.user_id == project.user_id master_members.delete(m) master_members.insert(0,m) break end end 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 end desc "获取项目某成员角色信息" params do requires :id, type: Integer requires :token, type: String requires :user_id, type: Integer end post 'get_member_info' do authenticate! project = Project.find("#{params[:id]}") my_member = project.member_principals.where("users.id=#{params[:user_id]}").first 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 present :status, 0 else present :status, -1 end end desc "修改项目某成员角色信息" params do requires :id, type: Integer requires :token, type: String requires :user_id, type: Integer requires :role_id, type: Integer end post 'edit_member_role' do authenticate! project = Project.find("#{params[:id]}") my_member = project.member_principals.where("users.id=#{current_user.id}").first #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 else ps = ProjectsService.new status = ps.modify_user_project_role params present :status, status end end desc "新建项目" params do requires :token, type: String requires :name, type: String, desc: '项目名称' end post 'create' do authenticate! ps = ProjectsService.new status = ps.createNewProject params,current_user present :status, 0 end desc "加入项目" params do requires :token, type: String requires :invite_code, type: String, desc: '邀请码' end post "join" do authenticate! # ps = ProjectsService.new # status = ps.join_project({role: "5", openid: params[:openid], invite_code: params[:invite_code]}, current_user) # # present :status, status {status:-1, message: '该功能将在近日开放,敬请期待!' } end end end end end