1class MissionsController < ApplicationController
  • remove trailing whitespace » rails_best_practices
2    
 
3    before_action :authenticate
 
4    skip_before_action :verify_authenticity_token, :only => [
 
5        :create,
 
6        :getlist,
 
7        :detail,
 
8        :update,
 
 9        :commentPublish
 
10    ]
 
11    #======================================>
 
 
13    def getlist
 
14    #Todo
 
15    pid = params[:project_id]
 
16    stu = params[:status_type]
 
17    #missions = []
 
18    details = []
 
19    missions = Mission.where("project_id = ? AND status = ?",pid,stu).all
 
 
21    missions.each do |mission|
 
 
23        #puts mission.inspect
 
 
25        details += [{
 
26            :id => mission.id,
 
27            :name => mission.name,
 
28            :content => mission.content
 
29        }]
 
 
31    end
 
 
33    # Mission.where(:project_id => pid).where(:status => stu) do |i|
 
34    #     missions += [{
 
35    #         :id => i.id,
 
36    #         :name => i.name,
 
37    #         :content => i.content
 
38    #     }]
 
39    # end
 
40    render :json => {
 
41        :code => 0,
 
42        :data => missions
 
43    }
 
44    end
 
 
46    #======================================>
 
  • move model logic into model (mission use_count > 4) » rails_best_practices
48    def detail
 
49    #Todo
 
50    username = []
 
51    comments_s = []
 
52    mission_id = params[:id]
 
53    mission = Mission.find_by(id: mission_id)
 
54    m_us = Missions_user.where("mission_id = ?",mission_id).all
 
55    #puts m_us.inspect
 
56    m_us.each do |m_u|
 
57        user_id = m_u.user_id
 
58        user = User.find_by(id: user_id)
 
59        #puts user.inspect
 
60        username << user.name
 
61    end
 
62    comments = Comment.where("mission_id = ?",mission_id).all
 
63    comments.each do |c|
 
64        uname = User.find_by(id: c.user_id).name
 
65        comments_s += [{
 
66            :nickname => uname,
 
67            :content => c.content,
 
68            :time => c.created_at
 
69        }]
 
70    end
 
71    #puts comments_s.inspect
 
72    render :json => {
 
73        :code => 0,
 
74        :data => {
 
75            :name => mission.name,
 
76            :content => mission.content,
 
77            :priority => mission.priority,
 
78            :status => mission.status,
 
79            :deadline => mission.deadline,
 
80            :users => username,
 
81            :comments => comments_s
 
82        }
 
83    }
 
84    end
 
85     #======================================>
 
  • move model logic into model (mission use_count > 4) » rails_best_practices
87    def create
 
88    #Todo
 
89    params_mission = {name: params[:name],project_id: params[:project_id],content: params[:content],deadline: params[:deadline],priority: params[:priority],status: params[:status]}
 
90    mission = Mission.new(params_mission)
 
91    if mission.save
 
92        render :json => {:code => 0}
 
93    else
 
94        render json: mission.errors, status: :unprocessable_entity
 
95    end
 
96    userids = params[:users]
 
97    userids.each do |id|
 
98        user_enties = User.find_by(id: id)
 
 99        #puts user_enties.inspect
 
100        params_mu = {mission_id: mission.id,user_id: user_enties.id}
 
101        missions_user = Missions_user.new(params_mu)
 
102        missions_user.save
 
103        content = " <a>"+mission.name+"</a>"
 
 
105        params_note = {content: content,user_id: user_enties.id,category: 3,project_id: mission.project_id}
 
106        note = Note.new(params_note)
 
107        note.save
 
108    end
 
109    end
 
 
111    #======================================>
 
  • move model logic into model (mission use_count > 4) » rails_best_practices
113    def update
 
114    #Todo
 
 
116    id = params[:id]
 
117    #now = Datetime.now
 
118    mission = Mission.find_by(id: id)
 
119    params_mission = {name: params[:name],content: params[:content],deadline: params[:deadline],priority: params[:priority]}
 
120    #puts params_mission
 
121    mission.update(params_mission)
 
122    mission.save
 
123    Missions_user.delete_all(["mission_id = ?",id])
 
124    userids = params[:users]
 
125    userids.each do |name|
 
126        user_enties = User.find_by(id: id)
 
127        params = {mission_id: id,user_id: user_enties.id}
 
128        missions_user = Missions_user.new(params)
 
129        missions_user.save
 
130        content = "<a>"+mission.name + "</a> "
 
 
132        params_note = {content: content,user_id: user_enties.id,category: 3,project_id: mission.project_id}
 
133        note = Note.new(params_note)
 
134        note.save
 
135    end
 
136    render :json => {:code => 0}
 
137    end
 
 
139    #======================================>
 
 
141    def commentPublish
 
142    #Todo
 
143    mission_id = params[:mission_id]
 
144    content = params[:content]
 
145    user_id = session[:user_id]
 
146    params = {content: content, mission_id: mission_id, user_id: user_id}
 
147    comment = Comment.new(params)
 
148    if comment.save
 
149        render :json => {:code => 0}
 
150    else
 
151        render json: mission.errors, status: :unprocessable_entity
 
152    end
 
153    end
 
154end