From 1670e845d7624ef769ec52c84022c919508684f9 Mon Sep 17 00:00:00 2001 From: HuJiaxuan Date: Mon, 2 Jan 2017 02:57:57 +0000 Subject: [PATCH] =?UTF-8?q?controller=20=E6=B5=8B=E8=AF=95version1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/missions_controller.rb | 117 +++++++----- app/controllers/notes_controller.rb | 29 +-- app/controllers/projects_controller.rb | 9 +- app/controllers/shares_controller.rb | 42 ++--- app/models/comment.rb | 5 +- app/models/missions_user.rb | 2 +- app/models/project.rb | 4 - config/routes.rb | 8 +- spec/controllers/missions_controller_spec.rb | 181 +++++++++++++------ spec/controllers/notes_controller_spec.rb | 34 ++++ spec/controllers/shares_controller_spec.rb | 56 ++++++ 11 files changed, 332 insertions(+), 155 deletions(-) create mode 100644 spec/controllers/notes_controller_spec.rb create mode 100644 spec/controllers/shares_controller_spec.rb diff --git a/app/controllers/missions_controller.rb b/app/controllers/missions_controller.rb index 9a57667..ac9ca3d 100644 --- a/app/controllers/missions_controller.rb +++ b/app/controllers/missions_controller.rb @@ -14,7 +14,7 @@ class MissionsController < ApplicationController missions.each do |mission| - puts mission.inspect + #puts mission.inspect details += [{ :id => mission.id, @@ -41,19 +41,28 @@ class MissionsController < ApplicationController def detail #Todo + username = [] + comments_s = [] mission_id = params[:id] mission = Mission.find_by(id: mission_id) - userid = Mission_user.find_by_name("mission_id") - username = User.find(userid).pluck("name") - comments = [] - Comments.where("mission_id" => id).each do |i| - name = User.find(i.user_id).name - comments += [{ - :nickname => name, - :content => i.content, - :time => i.created_at + 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 => { @@ -63,41 +72,60 @@ class MissionsController < ApplicationController :status => mission.status, :deadline => mission.deadline, :users => username, - :comments => comments + :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) + mission.save + #puts mission.inspect + #puts mission.id + usernames = params[:users] + usernames.each do |name| + + user_enties = User.find_by(name: name) + #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 = "有新任务:"+mission.name + + 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 update #Todo + id = params[:id] - now = Datetime.now - mission = Mission.find(id) - mission.name = params[:name] - mission.content = params[:content] - mission.priority = params[:priority] - mission.status = params[:status] - mission.deadline = params[:deadline] - mission.updated_at = now - mission.save! - pid = mission.project_id - Mission_user.destory_all(:mission_id => id) - params[:users].each do |i| - uid = User.where(:email => i).id - mu = Mission_user.new - mu.mission_id = id - mu.user_id = uid - mu.save! - note = Note.new - note.project_id = pid - note.user_id = uid - note.category = 3 - note.created_at = now - note.updated_at = now - note.content = "任务信息发生了变化" - note.save! + #now = Datetime.now + mission = Mission.find_by(id: id) + params_mission = {name: params[:name],content: params[:content],deadline: params[:deadline],priority: params[:priority],status: params[:status]} + #puts params_mission + mission.update(params_mission) + mission.save + Missions_user.delete_all(["mission_id = ?",id]) + usernames = params[:users] + usernames.each do |name| + user_enties = User.find_by(name: name) + params = {mission_id: id,user_id: user_enties.id} + missions_user = Missions_user.new(params) + missions_user.save + content = mission.name + "——任务信息发生了变化" + + 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 @@ -106,22 +134,15 @@ class MissionsController < ApplicationController def commentPublish #Todo - puts "I'm in" - content = params[:content] mission_id = params[:mission_id] + content = params[:content] user_id = session[:user_id] - puts user_id - params_comment = {content: content,mission_id: mission_id,user_id: user_id} - puts params_comment - comment = Comments.new(params_comment) - - # now = Datetime.now - # put.created_at = now - # put.updated_at = now - comment.save! - + params = {content: content, mission_id: mission_id, user_id: user_id} + comment = Comment.new(params) + comment.save render :json => {:code => 0} end + end \ No newline at end of file diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 7519daf..3e0f0c0 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -8,23 +8,24 @@ class NotesController < ApplicationController #Todo pid = params[:project_id] uid = session[:user_id] - note = {} - Note.where(:user_id => uid).where(:project_id => pid).each do |i| - date = i.created_at.to_date.to_s - if not note[date] then - note[date] = [] - end - note[:date] += [{ - :content => i.content, - :time => i.created_at.to_time, - :type => i.category - }] + notes = Note.where("user_id = ? AND project_id = ? ",uid,pid).order(:created_at).all + note = Hash.new do |h,k| + h[k] = [] + end + notes.each do |n| + date = n.created_at.to_s[/([\d\-]*)\s(.*)/,1] + time = n.created_at.to_s[/(.*)\s([\d\:]*)/,2] + note[date] << { + :content => n.content, + :time => time, + :type => n.category + } end data = [] - note.each do |date, list| + note.each do |k,v| data += [{ - :time => date, - :notes => list + :time => k, + :notes => v }] end render :json => { diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index b324444..f37d06f 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -32,15 +32,12 @@ class ProjectsController < ApplicationController #=============================================>创建项目 def create - #Todo + user_id = session[:user_id] - user = User.find_by(id: user_id) params_project = {content: params[:content],name: params[:name]} project = Project.new(params_project) - project.users << user - project.save if project.save - Projects_user.create(project_id:project.id,user_id:uid) + Projects_user.create(project_id:project.id,user_id:user_id) render status: :created, nothing: true else render json: project.errors, status: :unprocessable_entity @@ -52,7 +49,7 @@ class ProjectsController < ApplicationController #=============================================>项目添加用户 def addUsers - #Todo + uid = params[:user_id] register = session[:user_id] #username = params[:username] diff --git a/app/controllers/shares_controller.rb b/app/controllers/shares_controller.rb index 7af353c..bac8a89 100644 --- a/app/controllers/shares_controller.rb +++ b/app/controllers/shares_controller.rb @@ -6,26 +6,18 @@ class SharesController < ApplicationController def create #Todo - uid = session[:user_id].to_i - pid = params[:project_id].to_i - now = Datetime.now - put = Share.new() - put.content = params[:content] - put.project_id = pid - put.user_id = uid - put.created_at = now - put.updated_at = now - put.save! - content = User.find(uid).name + "分享了一些事" - Project_user.where(:project_id => pid).each do |tuid| - note = Note.new - note.user_id = tuid - note.project_id = pid - note.category = 1 - note.content = content - note.created_at = now - note.updated_at = now - note.save! + uid = session[:user_id] + pid = params[:project_id] + + params_share = {content: params[:content],project_id: params[:project_id],user_id: uid} + share = Share.new(params_share) + share.save + note_content = User.find_by(id: uid).name + "分享了一些事" + + Projects_user.where(:project_id => pid).all.each do |pu| + params_note = {content: note_content,category: 1,project_id: pu.project_id,user_id: pu.user_id} + note = Note.new(params_note) + note.save end render :json => {:code => 0} end @@ -35,12 +27,16 @@ class SharesController < ApplicationController def getShareList #Todo pid = params[:project_id] + data = [] - Share.where(:project_id => pid) do |i| - name = User.find(i.user_id).name + + Share.where(:project_id => pid).all.each do |i| + + name = User.find_by(id: i.user_id).name + date = i.created_at.to_s[/([\d\-]*)\s(.*)/,1] data += [{ :nickname => name, - :time => i.created_at, + :time => date, :content => i.content }] end diff --git a/app/models/comment.rb b/app/models/comment.rb index 0a01d72..322eccc 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,11 +1,12 @@ -class Comment < ActiveRecord:: Base +class Comment < ActiveRecord::Base belongs_to :mission + validates :content, presence: true validates :mission_id, presence: true validates :user_id, presence: true - + validate :comment_has_atleast_one_character, on: :create validate :user_id_exist, on: :create private diff --git a/app/models/missions_user.rb b/app/models/missions_user.rb index fc5da78..d18eb70 100644 --- a/app/models/missions_user.rb +++ b/app/models/missions_user.rb @@ -1,6 +1,6 @@ class Missions_user >>>>>> 27ebee595014926419b6ccba922ad39450b73f6b validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true private diff --git a/config/routes.rb b/config/routes.rb index 0c11c60..466397d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,15 +64,19 @@ Rails.application.routes.draw do get 'session' => 'sessions#show' post 'shares' => 'shares#create' - get 'shares' => 'share#getShareList' + get 'shares' => 'shares#getShareList' get 'projects' => 'projects#index' post 'projects' => 'projects#create' post 'projects/users' => 'projects#addUsers' get 'projects/detail' => 'projects#detail' + get 'notes/list' => 'notes#getNotesList' + get 'missions/project/status' => 'missions#getlist' get 'missions/detail' => 'missions#detail' - patch 'missions/detail' => 'missions#update' post 'missions/comments' => 'missions#commentPublish' + post 'missions/create' => 'missions#create' + patch 'missions/detail' => 'missions#update' + end diff --git a/spec/controllers/missions_controller_spec.rb b/spec/controllers/missions_controller_spec.rb index 94ec07c..8805988 100644 --- a/spec/controllers/missions_controller_spec.rb +++ b/spec/controllers/missions_controller_spec.rb @@ -2,62 +2,14 @@ require 'rails_helper' RSpec.describe MissionsController, type: :controller do - # describe "GET #getlist" do + describe "GET #getlist" do - # it "return a valid json format of missions" do + it "return a valid json format of missions" do - # register = create(:user) - # user = User.find_by(email: register.email) - # session[:user_id] = user.id - - # user1 = create(:user) - # user2 = create(:user) - # params = {name: "project addUsers test",content: "addUsers test"} - # project = Project.new(params) - # project.users << register - # project.save - - # params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id} - # params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id} - # mission = Mission.new(params_mission) - # mission2 = Mission.new(params_mission2) - # mission.save - # mission2.save - # get :getlist,project_id: project.id,status_type: 1 - - # puts response.body - - - # end - - # end - - # describe "GET #detail" do - - # it "return a correct json format detail" do - - # register = create(:user) - # params = {name: "project addUsers test",content: "addUsers test"} - # project = Project.new(params) - # project.users << register - # project.save - - # params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id} - # params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id} - # mission = Mission.new(params_mission) - # mission.save - # get :detail,id: mission.id - - # end - - # end - - describe "POST #comments" do - - it "can publish comments successfully" do - - register = create(:user) + user = User.find_by(email: register.email) + session[:user_id] = user.id + params = {name: "project addUsers test",content: "addUsers test"} project = Project.new(params) project.users << register @@ -66,14 +18,133 @@ RSpec.describe MissionsController, type: :controller do params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id} params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id} mission = Mission.new(params_mission) + mission2 = Mission.new(params_mission2) + mission.save + mission2.save + get :getlist,project_id: project.id,status_type: 1 + + puts response.body + + + end + + end + + describe "GET #detail" do + + it "return a correct json format detail" do + + register = create(:user) + session[:user_id] = register.id + params = {name: "project addUsers test",content: "addUsers test"} + project = Project.new(params) + project.users << register + project.save + + params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id} + #params_mission2 = {name: "mission2",content: "mission content2",deadline:"2016-10-17 10:00:00",priority:1,status:2,project_id: project.id} + mission = Mission.new(params_mission) mission.save - post :comments , mission_id: mission.id,content: "用户评论" + post :commentPublish,content: "评论1",mission_id: mission.id + post :commentPublish,content: "评论2",mission_id: mission.id + + get :detail,id: mission.id - puts "hello world" puts response.body end end + describe "POST #comments" do + + it "can publish comments successfully" do + + + register = create(:user) + params = {name: "project addUsers test",content: "addUsers test"} + session[:user_id] = register.id + project = Project.new(params) + project.users << register + project.save + + params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id} + mission = Mission.new(params_mission) + mission.save + + post :commentPublish,mission_id: mission.id,content: "asda" + + puts response.body + end + + end + + + describe "POST #update" do + + it "can update missions successfully" do + + register = create(:user) + user = User.find_by(email: register.email) + session[:user_id] = user.id + + params = {name: "project mission update test",content: "update test"} + project = Project.new(params) + project.users << register + project.save + + user1 = create(:user,name: "yangzhao") + user2 = create(:user,name: "chenyi") + + user3 = create(:user,name: "yangzhao") + user4 = create(:user,name: "chenyi") + + puts user1.inspect + params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id} + mission = Mission.new(params_mission) + mission.save + + post :create,project_id: project.id,name: "前端原型开发",content:"开发出前端原型",priority:1,status:1,deadline:"2017-01-02 14:14:54",users: [user1.name,user2.name] + + patch :update,name: "前端原型开发",id: mission.id,content:"完善前端原型",priority:1,status:1,deadline:"2017-01-03 14:14:54",users: [user3.name,user4.name] + + post :detail,id: 1 + puts response.body + + + end + + end + + + describe "POST #create" do + + it "can create missions successfully" do + + register = create(:user) + user = User.find_by(email: register.email) + session[:user_id] = user.id + + params = {name: "project mission update test",content: "update test"} + project = Project.new(params) + project.users << register + project.save + + user1 = create(:user) + user2 = create(:user) + project.users << user1 + project.users << user2 + #params_mission = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:1,project_id: project.id} + + post :create,project_id: project.id,name: "前端原型开发",content:"开发出前端原型",priority:1,status:1,deadline:"2017-01-02 10:00:00",users: [user1.name,user2.name] + + post :detail,id: 1 + + puts response.body + + end + + end + + end \ No newline at end of file diff --git a/spec/controllers/notes_controller_spec.rb b/spec/controllers/notes_controller_spec.rb new file mode 100644 index 0000000..fe76efe --- /dev/null +++ b/spec/controllers/notes_controller_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +RSpec.describe NotesController,type: :controller do + + describe "GET #getNotesList" do + + it "return correct json format of noteslist" do + + register = create(:user) + user = User.find_by(email: register.email) + session[:user_id] = user.id + + params = {name: "project mission update test",content: "update test"} + project = Project.new(params) + project.users << register + project.save + + params_note = {content: "note content 1",user_id: user.id,category: 2,project_id: project.id} + note = Note.new(params_note) + note.save + + params_note2 = {content: "note content 2",user_id: user.id,category: 1,project_id: project.id} + note2 = Note.new(params_note2) + note2.save + + + post :getNotesList,project_id: project.id + puts response.body + end + + end + +end + \ No newline at end of file diff --git a/spec/controllers/shares_controller_spec.rb b/spec/controllers/shares_controller_spec.rb new file mode 100644 index 0000000..6ec852c --- /dev/null +++ b/spec/controllers/shares_controller_spec.rb @@ -0,0 +1,56 @@ +require 'rails_helper' + +RSpec.describe SharesController,type: :controller do + + describe "POST #create" do + + it "create a correct share" do + + register = create(:user) + user = User.find_by(email: register.email) + session[:user_id] = user.id + + params = {name: "project addUsers test",content: "addUsers test"} + project = Project.new(params) + project.users << register + project.save + + post :create,project_id: project.id,content: "苟利国家生死以,岂因福祸避趋之" + + + end + + end + + describe "GET #getShareList" do + + it "return correct share list" do + + + register = create(:user) + user = User.find_by(email: register.email) + session[:user_id] = user.id + + params = {name: "project test",content: " test"} + project = Project.new(params) + project.users << register + project.save + + post :create,project_id: project.id,content: "苟利国家生死以,岂因福祸避趋之" + + post :create,project_id: project.id,content: "too young too simple" + + post :create,project_id: project.id,content: "too young too naive" + + + get :getShareList,project_id: project.id + + puts response.body + + end + + end + + + +end \ No newline at end of file