1class ProjectsController < ApplicationController
  • remove trailing whitespace » rails_best_practices
2    
 
3    before_action :authenticate
 
4    skip_before_action :verify_authenticity_token, :only => [
 
5        :index,
 
6        :create,
 
7        :addUsers,
 
8        :detail,
 
 9        :update,
 
10        :deleteUsers
 
11    ]
 
 
13    #=============================================>user_idsession.
 
14    def index
 
15    #Todo
 
16    uid = session[:user_id]
 
17    data=[]
 
18    Projects_user.where(:user_id => uid).each do |i|
 
19        pid=i.project_id
 
20        p=Project.find_by(id:pid)
 
21        mc = Mission.where(:project_id => pid).size
 
22        uc = Projects_user.where(:project_id => pid).size
 
23        sc = Share.where(:project_id => pid).size
 
24        data+=[{
 
25            :id => pid,
 
26            :name => p.name,
 
27            :content => p.content,
 
28            :mission_count => mc,
 
29            :users_count => uc,
 
30            :shares_count => sc
 
31        }]
 
32    end
 
33    render :json => {
 
34        :code => 0,
 
35        :data => data
 
36    }
 
37    end
 
 
39    #=============================================>
 
 
41    def create
 
42        uid = session[:user_id]
 
43        project = Project.new(project_params)
 
44        if project.save
 
45            Projects_user.create(project_id:project.id,user_id:uid)
 
46            render status: :created, nothing: true
 
47        else
 
48            render json: project.errors, status: :unprocessable_entity
 
49        end
 
50     end
 
 
 
53    #=============================================>
 
 
55    def addUsers
 
56        uid = User.find_by(email:params[:email]).id# 
 
57        register = session[:user_id]# 
 
58        pid = params[:project_id]
 
59        # 
 
60        pu_exist=Projects_user.find_by(project_id:pid,user_id:uid)
 
61        if pu_exist.nil?# 
 
62            params_pu = {project_id: pid,user_id: uid}
 
63            pu = Projects_user.new(params_pu)
 
64            pu.save
 
 
66            content = "<a>"+User.find_by(id: register).name + "</a>  <a>" + Project.find_by(id: pid).name+"</a>"
 
67            puts content
 
68            params_note = {user_id: uid,content: content,category: 2,project_id: pid}
 
69            note = Note.new(params_note)
 
70            note.save!
 
71            render :json => {:code => 0}
 
72        else# 
 
73            render json:current_user.errors, status: :unprocessable_entity
 
74        end
 
75    end
 
 
77    #=============================================>
 
78    def deleteUsers
 
79        uid = params[:user_id]# 
 
80        register = session[:user_id]# 
 
81        pid = params[:project_id]
 
82        # 
 
83        projects_user=Projects_user.find_by(project_id:pid,user_id:uid)
 
84        if projects_user.nil?# 
 
85            render json:current_user.errors, status: :unprocessable_entity
 
86        end
 
87        # 
 
88        puts projects_user.user_id
 
89        puts projects_user.project_id
 
90        Projects_user.where(project_id:pid,user_id:uid).delete_all
 
 
92        content ="<a>"+ User.find_by(id: register).name + "</a>  <a>" + Project.find_by(id: pid).name+"</a> "
 
93        puts content
 
94        params_note = {user_id: uid,content: content,category: 2,project_id: pid}
 
95        note = Note.new(params_note)
 
96        note.save!
 
 
98        render :json => {:code => 0}
 
 99    end
 
 
101    #=============================================>
 
 
103    def detail
 
104        #Todo    
 
105        pid = params[:id]
 
106        project = Project.find_by(id: pid)
 
107        #uid = Projects_user.where(:project_id => pid)
 
108        users = []
 
109        projects_users = Projects_user.where("project_id = ?",pid).all
 
 
111        projects_users.each do |projects_user|
 
112            uid = projects_user.user_id
 
113            i = User.find_by(id: uid)
 
114            users += [{
 
115                :name => i.name,
 
116                :id => i.id
 
117            }]
 
118        end
 
119        #puts users.inspect 
 
120        # Users.find(uid).each do |i|
 
121        #     users += [{
 
122        #         :nickname => i.name,
 
123        #         :id => i.id
 
124        #     }]
 
125        # end
 
126        render :json => {
 
127            :code => 0,
 
128            :data => {
 
129                :name => project.name,
 
130                :content => project.content,
 
131                :users => users
 
132            }
 
133        }
 
134    end
 
 
136    #=============================================>
 
137    def update
 
138        project = Project.find_by(:id => params[:project][:id])
 
139        if project.update(project_params)
 
140            render :json => {:code => 0}
 
141        else
 
142          render json:current_user.errors, status: :unprocessable_entity
 
143        end
 
144    end
 
 
146    private
 
147        def project_params
 
148          params.require(:project).permit(:name,:content,:id)
 
149        end
 
150end