diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index f8f2b3f..ba158bd 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -3,18 +3,25 @@ class ProjectsController < ApplicationController before_action :authenticate skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail] - #获取我的项目列表,user_id保存在session中. + #=============================================>获取我的项目列表,user_id保存在session中. def index #Todo uid = session[:user_id] - pid = Projects_user.find_by(project_id: uid) - mc = Mission.where(:project_id => pid).size - uc = Projects_user.where(:project_id => pid).size - sc = Share.where(:project_id => pid).size + #pid = Projects_user.find_by(project_id: uid) + pids = [] + projects = Projects_user.where("user_id = ?",uid).all + projects.each do |projects_user| + pids << projects_user.project_id + end data = [] - Project.find(pid) do |i| - data += [{ + pids.each do |pid| + mc = Mission.where(:project_id => pid).size + uc = Projects_user.where(:project_id => pid).size + sc = Share.where(:project_id => pid).size + puts pid + i = Project.find_by(id: pid) + data += [{ :id => i.id, :name => i.name, :content => i.content, @@ -29,62 +36,76 @@ class ProjectsController < ApplicationController } end - #创建项目 + #=============================================>创建项目 def create #Todo - project = Project.new() - project.content = params[:content] - project.name = params[:name] - now = Time.new - project.created_at = now - project.updated_at = now - project.save! - # 将自己加入项目 - pu = Project_user.new() - pu.project_id = project.id - pu.user_id = session[:user_id].to_i - pu.save! + 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 + + #project save的时候会保存 projects_users + + # params_pu = {project_id: project.id,user_id: user.id} + # puts params_pu + # pu = Projects_user.new(params_pu) + # pu.save render :json => {:code => 0} end - #项目添加用户 + + #=============================================>项目添加用户 def addUsers #Todo - uid = params[:user_id].to_i - pid = params[:project_id].to_i - pu = Project_user.new() - pu.project_id = pid - pu.user_id = uid - pu.save! - note = Note.new - note.user_id = uid - note.project_id = pid - note.category = 2 - note.content = User.find(uid).name + "邀请您进入项目" + - Project.find(pid).name - now = Datetime.now - note.created_at = now - note.updated_at = now + uid = params[:user_id] + register = session[:user_id] + #username = params[:username] + pid = params[:project_id] + params_pu = {project_id: pid,user_id: uid} + pu = Projects_user.new(params_pu) + pu.save + content = User.find_by(id: register).name + "邀请您进入项目" + Project.find_by(id: pid).name + puts content + params_note = {user_id: uid,content: content,category: 2,project_id: pid} + note = Note.new(params_note) + note.save! + #puts Note.find_by(id: note.id).inspect + # now = Datetime.now + # note.created_at = now + # note.updated_at = now note.save! render :json => {:code => 0} end - #项目详情 + #=============================================>项目详情 def detail #Todo pid = params[:id] project = Project.find_by(id: pid) - uid = Projects_user.where(:project_id => pid) + #uid = Projects_user.where(:project_id => pid) users = [] - Users.find(uid).each do |i| + projects_users = Projects_user.where("project_id = ?",pid).all + + projects_users.each do |projects_user| + uid = projects_user.user_id + i = User.find_by(id: uid) users += [{ :nickname => i.name, :id => i.id }] end + #puts users.inspect + # Users.find(uid).each do |i| + # users += [{ + # :nickname => i.name, + # :id => i.id + # }] + # end render :json => { :code => 0, :data => { diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 58129ce..f089c0e 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,7 +4,9 @@ class SessionsController < ApplicationController before_action :authenticate, except: [ :create ] skip_before_action :verify_authenticity_token, :only => [:create,:destroy,:show] - + + # 用户登录 + def create user = User.find_by(email: params[:email]) if user && user.authenticate(params[:password]) diff --git a/app/models/project.rb b/app/models/project.rb index e54aece..7adf60b 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,7 +1,8 @@ class Project < ActiveRecord::Base has_and_belongs_to_many :users - validate :require_at_least_on_user,on:addUsers # 项目中最少要有一个用户 + validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户 + validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true private diff --git a/spec/controllers/missions_controller_spec.rb b/spec/controllers/missions_controller_spec.rb index e69de29..d5820cd 100644 --- a/spec/controllers/missions_controller_spec.rb +++ b/spec/controllers/missions_controller_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe MissionsController, type: :controller do + + describe "" + +end \ No newline at end of file diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb new file mode 100644 index 0000000..7ccc90e --- /dev/null +++ b/spec/controllers/projects_controller_spec.rb @@ -0,0 +1,83 @@ +require 'rails_helper' + +RSpec.describe ProjectsController, type: :controller do + + + + describe "GET #index " do + + it "return valid projects json" do + + register = create(:user) + user = User.find_by(email: register.email) + session[:user_id] = user.id + + post :create,content: "project content 1",name: "project1" + post :create,content: "project content 2", name: "project2" + + get :index + + puts response.body + end + + end + + describe "POST #create" do + + it "can create a valid project" do + + register = create(:user) + user = User.find_by(email: register.email) + #puts user.inspect + session[:user_id] = user.id + post :create,content: "project content 1",name: "project1" + #puts response.body + end + end + + describe "POST #addUsers" do + + it "can addUsers correctly" 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 + #post :create,content: "project content 3",name: "project3" + + post :addUsers,user_id: user2.id,project_id: project.id + + + end + + end + + describe "GET #detail" do + + it "can return correct json format of project " 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 + post :addUsers,user_id: user2.id,project_id: project.id + post :addUsers,user_id: user1.id,project_id: project.id + post :detail,id: project.id + puts response.body + end + + end + +end \ No newline at end of file diff --git a/spec/factories/projects_users.rb b/spec/factories/projects_users.rb new file mode 100644 index 0000000..facb952 --- /dev/null +++ b/spec/factories/projects_users.rb @@ -0,0 +1,6 @@ +require 'faker' +FactoryGirl.define do + factory :projects_user do + + end +end