modify project packages

dev_aliyun
p31729568 6 years ago
parent 1898f2ad1e
commit acc9d603b3

@ -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?

@ -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

@ -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数组'

@ -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 }

@ -0,0 +1,9 @@
module Mobile
module Entities
class ProjectPackageCategory < Grape::Entity
expose :id
expose :name
end
end
end

@ -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

@ -0,0 +1,5 @@
class ProjectPackageCategory < ActiveRecord::Base
default_scope order: 'position asc'
has_many :project_packages, dependent: :destroy
end

@ -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)

@ -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

@ -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
Loading…
Cancel
Save