From b4ac9b05204a4701f0749bc89f207d3b35b26fd6 Mon Sep 17 00:00:00 2001 From: SylorHuang Date: Tue, 24 Dec 2019 19:44:45 +0800 Subject: [PATCH] =?UTF-8?q?issue=E7=9A=84=E9=87=8C=E7=A8=8B=E7=A2=91?= =?UTF-8?q?=E5=92=8C=E7=8A=B6=E6=80=81=E7=9A=84=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../compose_projects_controller.rb | 2 +- app/controllers/issue_tags_controller.rb | 111 +++++++++++++ app/controllers/issues_controller.rb | 7 + app/controllers/versions_controller.rb | 152 ++++++++++++++++++ app/helpers/issues_helper.rb | 23 +++ app/models/compose.rb | 1 + app/models/compose_project.rb | 1 + app/models/issue.rb | 10 ++ app/models/issue_tag.rb | 4 + app/models/project.rb | 1 + app/models/tracker.rb | 3 + app/models/user.rb | 1 + app/models/version.rb | 17 ++ app/views/issue_tags/edit.json.jbuilder | 1 + app/views/issue_tags/index.json.jbuilder | 7 + app/views/versions/edit.json.jbuilder | 1 + app/views/versions/index.json.jbuilder | 13 ++ app/views/versions/show.json.jbuilder | 27 ++++ config/routes.rb | 6 + .../20191224064403_create_issue_tags.rb | 14 ++ spec/models/issue_tag_spec.rb | 5 + 21 files changed, 406 insertions(+), 1 deletion(-) create mode 100644 app/controllers/issue_tags_controller.rb create mode 100644 app/controllers/issues_controller.rb create mode 100644 app/controllers/versions_controller.rb create mode 100644 app/helpers/issues_helper.rb create mode 100644 app/models/issue_tag.rb create mode 100644 app/models/tracker.rb create mode 100644 app/views/issue_tags/edit.json.jbuilder create mode 100644 app/views/issue_tags/index.json.jbuilder create mode 100644 app/views/versions/edit.json.jbuilder create mode 100644 app/views/versions/index.json.jbuilder create mode 100644 app/views/versions/show.json.jbuilder create mode 100644 db/migrate/20191224064403_create_issue_tags.rb create mode 100644 spec/models/issue_tag_spec.rb diff --git a/app/controllers/compose_projects_controller.rb b/app/controllers/compose_projects_controller.rb index 1f9c1dc6c..4355571c7 100644 --- a/app/controllers/compose_projects_controller.rb +++ b/app/controllers/compose_projects_controller.rb @@ -1,4 +1,5 @@ class ComposeProjectsController < ApplicationController + #未做完 before_action :require_login before_action :set_compose @@ -29,7 +30,6 @@ class ComposeProjectsController < ApplicationController def set_compose @compose = Compose.find(params[:compose_id]) - Rails.logger.info() unless @compose.present? normal_status(-1, "组织不存在") end diff --git a/app/controllers/issue_tags_controller.rb b/app/controllers/issue_tags_controller.rb new file mode 100644 index 000000000..345627f8d --- /dev/null +++ b/app/controllers/issue_tags_controller.rb @@ -0,0 +1,111 @@ +class IssueTagsController < ApplicationController + before_action :require_login + before_action :set_project + before_action :check_issue_permission, except: :index + before_action :set_issue_tag, only: [:edit, :update, :destroy] + + + def index + order_name = params[:order_name] || "created_at" + order_type = params[:order_type] || "desc" + + issue_tags = @project.issue_tags.order("#{order_name} #{order_type}") + + @page = params[:page] || 1 + @limit = params[:limit] || 15 + @issue_tags_size = issue_tags.size + @issue_tags = issue_tags.page(@page).per(@limit) + end + + + def create + title = params[:title].to_s.strip + desc = params[:description] + color = params[:color] || "#ccc" + + tag_params = { + title: title, + description: desc, + color: color + } + + if title.present? + if IssueTag.exists?(title: title, project_id: @project.id) + normal_status(-1, "标签已存在") + else + issue_tag = IssueTag.new(tag_params.merge(project_id: @project.id, user_id: current_user.id)) + if issue_tag.save + normal_status(0, "标签创建成功") + else + normal_status(-1, "标签创建失败") + end + end + + else + normal_status(-1, "标签名称不能为空") + end + + end + + def edit + + end + + def update + title = params[:title] + desc = params[:description] + color = params[:color] || "#ccc" + + tag_params = { + title: title, + description: desc, + color: color + } + if title.present? + if IssueTag.exists?(title: title, project_id: @project.id) && (@issue_tag.title != title) + normal_status(-1, "标签已存在") + else + if @issue_tag.update_attributes(tag_params) + normal_status(0, "标签更新成功") + else + normal_status(-1, "标签更新失败") + end + end + + else + normal_status(-1, "标签名称不能为空") + end + end + + def destroy + if @issue_tag.destroy + normal_status(0, "标签删除成功") + else + normal_status(-1, "标签删除失败") + end + + end + + private + + def set_project + @project = Project.find_by_id(params[:project_id]) + unless @project.present? + normal_status(-1, "项目不存在") + end + end + + def check_issue_permission + unless @project.member?(current_user) || current_user.admin? + normal_status(-1, "您没有权限") + end + end + + def set_issue_tag + @issue_tag = IssueTag.find_by_id(params[:id]) + unless @issue_tag.present? + normal_status(-1, "标签不存在") + end + end + +end \ No newline at end of file diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb new file mode 100644 index 000000000..de055777b --- /dev/null +++ b/app/controllers/issues_controller.rb @@ -0,0 +1,7 @@ +class IssuesController < ApplicationController + before_action :require_login + + + + +end \ No newline at end of file diff --git a/app/controllers/versions_controller.rb b/app/controllers/versions_controller.rb new file mode 100644 index 000000000..2ea398f60 --- /dev/null +++ b/app/controllers/versions_controller.rb @@ -0,0 +1,152 @@ +class VersionsController < ApplicationController + before_action :require_login + before_action :set_project + before_action :check_issue_permission, except: [:show, :index] + before_action :set_version, only: [:edit, :update, :destroy, :show,:update_status] + + def index + order_name = params[:order_name] || "created_on" + order_type = params[:order_type] || "desc" + status = params[:status] + versions = @project.versions.version_includes + if status.present? + versions = versions.where(status: status.to_s.strip) + end + + versions = versions.order("#{order_name} #{order_type}") + + @page = params[:page] || 1 + @limit = params[:limit] || 15 + @versions_size = versions.size + @versions = versions.page(@page).per(@limit) + end + + def show + order_name = params[:order_name] || "created_on" + order_type = params[:order_type] || "desc" + + version_issues = @version.issues.issue_includes.order("#{order_name} #{order_type}") + @close_issues_size = version_issues.where(status_id: 5).size + + @page = params[:page] || 1 + @limit = params[:limit] || 15 + @version_issues_size = version_issues.size + @version_issues = version_issues.page(@page).per(@limit) + end + + def create + name = params[:name].to_s.strip + desc = params[:description] + effective_date = params[:effective_date] + status = params[:status] || "open" + sharing = params[:sharing] || "" + wiki_page_title = params[:wiki_page_title] || "" + + tag_params = { + name: name, + description: desc, + effective_date: effective_date, + status: status, + sharing: sharing, + wiki_page_title: wiki_page_title + } + + if name.present? + if Version.exists?(name: name, project_id: @project.id) + normal_status(-1, "里程碑已存在") + else + version = Version.new(tag_params.merge(project_id: @project.id, user_id: current_user.id)) + if version.save + normal_status(0, "里程碑创建成功") + else + normal_status(-1, "里程碑创建失败") + end + end + + else + normal_status(-1, "里程碑名称不能为空") + end + + end + + def edit + + end + + def update + name = params[:name].to_s.strip + desc = params[:description] + effective_date = params[:effective_date] + status = params[:status] || "open" + sharing = params[:sharing] || "" + wiki_page_title = params[:wiki_page_title] || "" + + tag_params = { + name: name, + description: desc, + effective_date: effective_date, + status: status, + sharing: sharing, + wiki_page_title: wiki_page_title + } + + if name.present? + if Version.exists?(name: name, project_id: @project.id) && (@version.name != name) + normal_status(-1, "里程碑已存在") + else + if @version.update_attributes(tag_params) + normal_status(0, "里程碑更新成功") + else + normal_status(-1, "里程碑更新失败") + end + end + else + normal_status(-1, "里程碑名称不能为空") + end + end + + def destroy + if @version.destroy + normal_status(0, "里程碑删除成功") + else + normal_status(-1, "里程碑删除失败") + end + end + + def update_status + status = params[:status] || "open" + all_status = %w(open closed locked) + if all_status.include?(status) + if @version.update_attribute(:status, status) + normal_status(0, "操作成功") + else + normal_status(-1, "操作失败") + end + else + normal_status(-1, "status状态值错误") + end + end + + private + + def set_project + @project = Project.find_by_id(params[:project_id]) + unless @project.present? + normal_status(-1, "项目不存在") + end + end + + def check_issue_permission + unless @project.member?(current_user) || current_user.admin? + normal_status(-1, "您没有权限") + end + end + + def set_version + @version = Version.find_by_id(params[:id]) + unless @version.present? + normal_status(-1, "里程碑不存在") + end + end + +end \ No newline at end of file diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb new file mode 100644 index 000000000..f742d5af8 --- /dev/null +++ b/app/helpers/issues_helper.rb @@ -0,0 +1,23 @@ +module IssuesHelper + + def issue_status + { + "新增": "1", + "正在解决": "2", + "已解决": "3", + "反馈": "4", + "关闭": "5", + "拒绝": "6" + } + end + + def version_status + { + "开启": "open", + "关闭": "closed", + "锁定": "locked" + } + + end + +end \ No newline at end of file diff --git a/app/models/compose.rb b/app/models/compose.rb index 996570118..0b3733943 100644 --- a/app/models/compose.rb +++ b/app/models/compose.rb @@ -1,4 +1,5 @@ class Compose < ApplicationRecord + #组织 belongs_to :user has_many :compose_projects has_many :compose_users diff --git a/app/models/compose_project.rb b/app/models/compose_project.rb index 617204eff..3c2d099e1 100644 --- a/app/models/compose_project.rb +++ b/app/models/compose_project.rb @@ -1,3 +1,4 @@ class ComposeProject < ApplicationRecord + #组织的项目记录表 belongs_to :compose end diff --git a/app/models/issue.rb b/app/models/issue.rb index e8f05202c..f3d54f98b 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1,3 +1,13 @@ class Issue < ApplicationRecord belongs_to :project + belongs_to :tracker + belongs_to :issue_tag, foreign_key: :priority_id + belongs_to :version, foreign_key: :fixed_version_id + belongs_to :user,optional: true + + scope :issue_includes, ->{includes(:user)} + + def get_assign_user + User.select(:login, :lastname,:firstname, :nickname)&.find_by_id(self.assigned_to_id) + end end \ No newline at end of file diff --git a/app/models/issue_tag.rb b/app/models/issue_tag.rb new file mode 100644 index 000000000..47193addb --- /dev/null +++ b/app/models/issue_tag.rb @@ -0,0 +1,4 @@ +class IssueTag < ApplicationRecord + has_many :issues + belongs_to :projects, optional: true +end diff --git a/app/models/project.rb b/app/models/project.rb index 9f803b684..3b8d293de 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -9,6 +9,7 @@ class Project < ApplicationRecord has_many :manager_members, -> { joins(:roles).where(roles: { name: 'Manager' }) }, class_name: 'Member' has_one :project_score, dependent: :destroy has_one :repository, dependent: :destroy + has_many :issue_tags has_many :issues has_many :user_grades, dependent: :destroy diff --git a/app/models/tracker.rb b/app/models/tracker.rb new file mode 100644 index 000000000..f91ad92ee --- /dev/null +++ b/app/models/tracker.rb @@ -0,0 +1,3 @@ +class Tracker < ApplicationRecord + has_many :issues +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 5b90d6311..e57c8833a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -51,6 +51,7 @@ class User < ApplicationRecord has_many :study_shixuns, through: :myshixuns, source: :shixun # 已学习的实训 has_many :course_messages has_many :courses, foreign_key: 'tea_id', dependent: :destroy + has_many :versions #试卷 has_many :exercise_banks, :dependent => :destroy diff --git a/app/models/version.rb b/app/models/version.rb index c278ff0a9..47b987953 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -1,3 +1,20 @@ class Version < ApplicationRecord belongs_to :project + has_many :issues, class_name: "Issue", foreign_key: "fixed_version_id" + belongs_to :user, optional: true + + scope :version_includes, ->{includes(:issues, :user)} + + def open_issues_count + issues.select(:id,:status_id).where(status_id: [1,2,3,4,6]).size + end + + def close_issues_count + issues.select(:id,:status_id).where(status_id: 5).size + end + + def version_user + User.select(:login, :lastname,:firstname, :nickname)&.find_by_id(self.user_id) + end + end diff --git a/app/views/issue_tags/edit.json.jbuilder b/app/views/issue_tags/edit.json.jbuilder new file mode 100644 index 000000000..72e2b14d9 --- /dev/null +++ b/app/views/issue_tags/edit.json.jbuilder @@ -0,0 +1 @@ +json.extract! @issue_tag, :id, :title, :description, :color,:project_id \ No newline at end of file diff --git a/app/views/issue_tags/index.json.jbuilder b/app/views/issue_tags/index.json.jbuilder new file mode 100644 index 000000000..9e04a74d3 --- /dev/null +++ b/app/views/issue_tags/index.json.jbuilder @@ -0,0 +1,7 @@ +json.issue_tags_count @issue_tags_size +json.issue_tags do + + json.array! @issue_tags.each.to_a do |tag| + json.extract! tag, :id, :title, :description, :color, :issues_count, :project_id + end +end \ No newline at end of file diff --git a/app/views/versions/edit.json.jbuilder b/app/views/versions/edit.json.jbuilder new file mode 100644 index 000000000..55e081dc6 --- /dev/null +++ b/app/views/versions/edit.json.jbuilder @@ -0,0 +1 @@ +json.extract! @version, :id,:name,:project_id,:description, :effective_date, :status, :sharing,:wiki_page_title diff --git a/app/views/versions/index.json.jbuilder b/app/views/versions/index.json.jbuilder new file mode 100644 index 000000000..71c640d8f --- /dev/null +++ b/app/views/versions/index.json.jbuilder @@ -0,0 +1,13 @@ +json.versions_count @versions_size +json.versions do + + json.array! @versions.each.to_a do |version| + json.extract! version, :id, :name, :description, :effective_date,:status + json.open_issues_count version.open_issues_count + json.close_issues_count version.close_issues_count + json.created_at format_time(version.created_on) + json.updated_at format_time(version.updated_on) + json.user_name version.version_user.try(:show_real_name) + json.user_login version.version_user.try(:login) + end +end \ No newline at end of file diff --git a/app/views/versions/show.json.jbuilder b/app/views/versions/show.json.jbuilder new file mode 100644 index 000000000..52d275f9b --- /dev/null +++ b/app/views/versions/show.json.jbuilder @@ -0,0 +1,27 @@ +json.issues_count @version_issues_size +json.close_issues_count @close_issues_size +json.user_name @version.version_user.try(:show_real_name) +json.user_login @version.version_user.try(:login) +json.created_at format_time(@version.created_on) +json.updated_at format_time(@version.updated_on) +json.extract! @version, :id,:name,:project_id,:description, :effective_date, :status, :sharing,:wiki_page_title + +json.issues do + + json.array! @version_issues.each.to_a do |issue| + user = issue.get_assign_user + json.id issue.id + json.name issue.subject + json.project_id issue.project_id + json.estimated_hours issue.estimated_hours.to_f + json.tracker issue.tracker.try(:name) + json.assign_user_name user.try(:show_real_name) + json.assign_user_login user.try(:login) + json.updated_at format_time(issue.updated_on) + json.status issue_status.key(issue.status_id.to_s) + json.done_ratio "#{issue.done_ratio.to_s}%" + end +end + + + diff --git a/config/routes.rb b/config/routes.rb index 10751e45c..0f97eba11 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,12 @@ Rails.application.routes.draw do resources :licenses, only: [:index, :show] resources :projects, only: [:index, :create, :show] do + resources :issue_tags, only: [:create, :edit, :update, :destroy, :index] + resources :versions do + member do + post :update_status + end + end collection do post :migrate end diff --git a/db/migrate/20191224064403_create_issue_tags.rb b/db/migrate/20191224064403_create_issue_tags.rb new file mode 100644 index 000000000..09564db28 --- /dev/null +++ b/db/migrate/20191224064403_create_issue_tags.rb @@ -0,0 +1,14 @@ +class CreateIssueTags < ActiveRecord::Migration[5.2] + def change + create_table :issue_tags do |t| + t.string :title + t.string :description + t.string :color + t.integer :user_id + t.integer :project_id + t.integer :issues_count,default: 0 + t.timestamps + end + add_index :issue_tags, [:user_id, :title,:project_id] + end +end diff --git a/spec/models/issue_tag_spec.rb b/spec/models/issue_tag_spec.rb new file mode 100644 index 000000000..fb5a8a01b --- /dev/null +++ b/spec/models/issue_tag_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe IssueTag, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end