From acc9d603b30a6f8d4af0334bd0ecb7c096c26278 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Sat, 13 Jul 2019 10:29:37 +0800 Subject: [PATCH] modify project packages --- app/api/mobile/api.rb | 1 + .../mobile/apis/project_package_categories.rb | 17 +++++++++++++++++ app/api/mobile/apis/project_packages.rb | 10 +++++----- app/api/mobile/entities/project_package.rb | 3 +++ .../mobile/entities/project_package_category.rb | 9 +++++++++ app/models/project_package.rb | 8 ++++++-- app/models/project_package_category.rb | 5 +++++ app/services/project_packages/save_service.rb | 2 ++ ...0713015331_add_project_package_categories.rb | 10 ++++++++++ ...15513_add_category_id_to_project_packages.rb | 12 ++++++++++++ 10 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 app/api/mobile/apis/project_package_categories.rb create mode 100644 app/api/mobile/entities/project_package_category.rb create mode 100644 app/models/project_package_category.rb create mode 100644 db/migrate/20190713015331_add_project_package_categories.rb create mode 100644 db/migrate/20190713015513_add_category_id_to_project_packages.rb diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 8d01bb5b..c631d56f 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -213,6 +213,7 @@ module Mobile mount Apis::Cnmooc mount Apis::ProjectPackages + mount Apis::ProjectPackageCategories # add_swagger_documentation ({host: 'educoder.0bug.info', api_version: 'v1', base_path: '/api'}) if Rails.env.development? add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development? diff --git a/app/api/mobile/apis/project_package_categories.rb b/app/api/mobile/apis/project_package_categories.rb new file mode 100644 index 00000000..8fcfbf7f --- /dev/null +++ b/app/api/mobile/apis/project_package_categories.rb @@ -0,0 +1,17 @@ +module Mobile + module Apis + class ProjectPackageCategories < Grape::API + resources :project_package_categories do + desc 'project package categoires index' + params do + end + get do + categories = ProjectPackageCategory.where(nil) + + present :count, categories.size + present :categories, categories, with: Mobile::Entities::ProjectPackageCategory + end + end + end + end +end diff --git a/app/api/mobile/apis/project_packages.rb b/app/api/mobile/apis/project_packages.rb index 1d2b4f69..1ddd4ebb 100644 --- a/app/api/mobile/apis/project_packages.rb +++ b/app/api/mobile/apis/project_packages.rb @@ -18,7 +18,7 @@ module Mobile resources :project_packages do desc 'project packages index' params do - optional :category, type: String, desc: '类型' + optional :category_id, type: Integer, desc: '类型ID' optional :keyword, type: String, desc: '搜索关键字' optional :sort_by, type: String, desc: '排序' optional :sort_direction, type: String, desc: '排序方向' @@ -28,7 +28,7 @@ module Mobile get do packages = ProjectPackage.where(status: %w(published bidding_ended bidding_finished)) - packages = packages.where(category: params[:category]) if params[:category].present? + packages = packages.where(project_package_category_id: params[:category_id]) if params[:category_id].present? keyword = params[:keyword].to_s.strip packages = packages.where('title LIKE ?', "%#{keyword}%") if keyword.present? @@ -39,7 +39,7 @@ module Mobile sort = params[:sort_by] == 'price' ? 'min_price' : 'published_at' packages = packages.order("#{sort} #{direction}") - packages = paginate packages.includes(:creator, :attachments, bidding_users: :user) + packages = paginate packages.includes(:creator, :attachments, :project_package_category, bidding_users: :user) present :count, count present :project_packages, packages, with: Mobile::Entities::ProjectPackage, type: :index, user: current_user @@ -96,7 +96,7 @@ module Mobile desc 'create project packages' params do - requires :category, type: String, desc: '类型' + requires :category_id, type: Integer, desc: '类型ID' requires :title, type: String, desc: '标题' requires :content, type: String, desc: '描述' optional :attachment_ids, type: Array[Integer], desc: '附件ID数组' @@ -124,7 +124,7 @@ module Mobile desc 'update project package' params do requires :id, type: Integer, desc: 'ID' - requires :category, type: String, desc: '类型' + requires :category_id, type: Integer, desc: '类型ID' requires :title, type: String, desc: '标题' requires :content, type: String, desc: '描述' optional :attachment_ids, type: Array[Integer], desc: '附件ID数组' diff --git a/app/api/mobile/entities/project_package.rb b/app/api/mobile/entities/project_package.rb index 6bfbef18..ddf03547 100644 --- a/app/api/mobile/entities/project_package.rb +++ b/app/api/mobile/entities/project_package.rb @@ -8,6 +8,9 @@ module Mobile expose :title expose :content expose :category + expose :category_id do |p, _| + p.project_package_category_id + end expose :status expose :visit_count, if: { type: :index } expose :bidding_users_count, if: { type: :index } diff --git a/app/api/mobile/entities/project_package_category.rb b/app/api/mobile/entities/project_package_category.rb new file mode 100644 index 00000000..6dedca67 --- /dev/null +++ b/app/api/mobile/entities/project_package_category.rb @@ -0,0 +1,9 @@ +module Mobile + module Entities + class ProjectPackageCategory < Grape::Entity + expose :id + expose :name + end + end +end + diff --git a/app/models/project_package.rb b/app/models/project_package.rb index 8f9035f6..86bf8653 100644 --- a/app/models/project_package.rb +++ b/app/models/project_package.rb @@ -5,9 +5,10 @@ class ProjectPackage < ActiveRecord::Base CATEGORY_VALUES = %w(front backend mobile database cloud_compute_and_big_data devops_and_test ai other) - attr_accessible :title, :content, :category, :deadline_at, :contact_name, :contact_phone, :min_price, :max_price + attr_accessible :title, :content, :project_package_category_id, :deadline_at, :contact_name, :contact_phone, :min_price, :max_price belongs_to :creator, class_name: 'User' + belongs_to :project_package_category has_many :project_package_applies, dependent: :destroy has_one :process_project_package_apply, conditions: { status: :pending }, class_name: 'ProjectPackageApply' @@ -18,7 +19,6 @@ class ProjectPackage < ActiveRecord::Base has_many :attachments, as: :container, dependent: :destroy - validates :category, presence: true, inclusion: { in: CATEGORY_VALUES } validates :title, presence: true, length: { maximum: 60 } validates :content, presence: true validates :deadline_at, presence: true @@ -56,6 +56,10 @@ class ProjectPackage < ActiveRecord::Base end end + def category + project_package_category.name + end + def visitable? !editable? end diff --git a/app/models/project_package_category.rb b/app/models/project_package_category.rb new file mode 100644 index 00000000..d0b63533 --- /dev/null +++ b/app/models/project_package_category.rb @@ -0,0 +1,5 @@ +class ProjectPackageCategory < ActiveRecord::Base + default_scope order: 'position asc' + + has_many :project_packages, dependent: :destroy +end \ No newline at end of file diff --git a/app/services/project_packages/save_service.rb b/app/services/project_packages/save_service.rb index 9011722c..bb91f5d4 100644 --- a/app/services/project_packages/save_service.rb +++ b/app/services/project_packages/save_service.rb @@ -12,6 +12,8 @@ class ProjectPackages::SaveService check_code_valid! if need_check_code? is_create = package.new_record? + raise Error, '类型不存在' unless ProjectPackageCategory.where(id: params[:category_id]).exists? + params[:project_package_category_id] = params[:category_id].to_i ActiveRecord::Base.transaction do package.assign_attributes(params) diff --git a/db/migrate/20190713015331_add_project_package_categories.rb b/db/migrate/20190713015331_add_project_package_categories.rb new file mode 100644 index 00000000..a427d31b --- /dev/null +++ b/db/migrate/20190713015331_add_project_package_categories.rb @@ -0,0 +1,10 @@ +class AddProjectPackageCategories < ActiveRecord::Migration + def change + create_table :project_package_categories do |t| + t.string :name + t.integer :position + end + + execute 'insert into project_package_categories(name, position) values("前端开发",1),("后端开发", 2),("移动开发", 3),("数据库", 4),("云计算和大数据", 5),("运维与测试", 6),("人工智能", 7),("其它", 8)' + end +end diff --git a/db/migrate/20190713015513_add_category_id_to_project_packages.rb b/db/migrate/20190713015513_add_category_id_to_project_packages.rb new file mode 100644 index 00000000..64a7de6f --- /dev/null +++ b/db/migrate/20190713015513_add_category_id_to_project_packages.rb @@ -0,0 +1,12 @@ +class AddCategoryIdToProjectPackages < ActiveRecord::Migration + def up + add_column :project_packages, :project_package_category_id, :integer + remove_column :project_packages, :category + execute "update project_packages set project_package_category_id = 1" + end + + def down + remove_column :project_packages, :project_package_category_id + add_column :project_packages, :category, :string + end +end