From 2887de4c4e0bfbb9527e49efdf5070c95f51b2a7 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 24 Jul 2019 09:36:16 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=85=B3=E5=8D=A1=E6=AF=94=E4=BE=8B?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E7=9A=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/game.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/game.rb b/app/models/game.rb index ae056a702..d81b794a5 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -31,7 +31,7 @@ class Game < ApplicationRecord # 根据得分比例来算实际得分(试卷、实训作业) def real_score score - (final_score.to_f / challenge.all_score) * score + ((final_score < 0 ? 0 : final_score).to_f / challenge.all_score) * score end # 判断实训是否全部通关 From 3cf598eeecd30613b59786ae8bc7f677a623892d Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 24 Jul 2019 09:58:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=99=E5=B8=88?= =?UTF-8?q?=E5=BC=B9=E6=A1=86=E7=9A=84=E6=95=99=E5=B8=88=E5=A7=93=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/courses/search_teacher_candidate.json.jbuilder | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/courses/search_teacher_candidate.json.jbuilder b/app/views/courses/search_teacher_candidate.json.jbuilder index 33bed4100..49109b729 100644 --- a/app/views/courses/search_teacher_candidate.json.jbuilder +++ b/app/views/courses/search_teacher_candidate.json.jbuilder @@ -1,7 +1,7 @@ json.candidates do json.array! @users do |user| json.id user.id - json.name user.firstname + user.lastname + json.name user.real_name json.nickname user.nickname json.school_name user.user_extension.school.try(:name) json.school_id user.user_extension.school.try(:id) From 69f958c64786f5a5fa8a756bf2e48f4df4ff0e64 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Wed, 24 Jul 2019 10:41:45 +0800 Subject: [PATCH 3/3] user center project package module --- .../users/project_packages_controller.rb | 17 +++++ app/models/project_package.rb | 3 + app/models/user.rb | 9 +++ app/services/users/project_package_service.rb | 76 +++++++++++++++++++ .../project_packages/index.json.jbuilder | 20 +++++ config/routes.rb | 1 + 6 files changed, 126 insertions(+) create mode 100644 app/controllers/users/project_packages_controller.rb create mode 100644 app/services/users/project_package_service.rb create mode 100644 app/views/users/project_packages/index.json.jbuilder diff --git a/app/controllers/users/project_packages_controller.rb b/app/controllers/users/project_packages_controller.rb new file mode 100644 index 000000000..edd6bd29b --- /dev/null +++ b/app/controllers/users/project_packages_controller.rb @@ -0,0 +1,17 @@ +class Users::ProjectPackagesController < Users::BaseController + + def index + packages = Users::ProjectPackageService.call(observed_user, query_params) + + @count = packages.count + @packages = paginate(packages.includes(:project_package_category)) + + bidding_users = BiddingUser.where(project_package_id: @packages.map(&:id), user_id: observed_user.id) + bidding_users = bidding_users.group(:project_package_id).select(:project_package_id, :status) + @bidding_status_map = bidding_users.each_with_object({}) { |u, h| h[u.project_package_id] = u.status } + end + + def query_params + params.permit(:category, :status, :sort_by, :sort_direction) + end +end \ No newline at end of file diff --git a/app/models/project_package.rb b/app/models/project_package.rb index fc541097a..219f60ca4 100644 --- a/app/models/project_package.rb +++ b/app/models/project_package.rb @@ -13,6 +13,9 @@ class ProjectPackage < ApplicationRecord has_many :attachments, as: :container, dependent: :destroy + scope :visible, -> { where(status: %i[published bidding_ended bidding_finished]) } + scope :invisible, -> { where(status: %i[pending applying refused]) } + aasm(:status) do state :pending, initiali: true state :applying diff --git a/app/models/user.rb b/app/models/user.rb index 73036c73e..03c97b9f3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -123,6 +123,11 @@ class User < ApplicationRecord has_many :user_interests, dependent: :delete_all has_many :interests, through: :user_interests, source: :repertoire + # 众包 + has_many :project_packages, foreign_key: :creator_id, dependent: :destroy + has_many :bidding_users, dependent: :destroy + has_many :bidden_project_packages, through: :bidding_users, source: :project_package + # Groups and active users scope :active, lambda { where(status: STATUS_ACTIVE) } @@ -572,6 +577,10 @@ class User < ApplicationRecord Attendance.find_by(user_id: id)&.next_gold || 60 # 基础50,连续签到+10 end + def admin_or_business? + admin? || business? + end + protected def validate_password_length # 管理员的初始密码是5位 diff --git a/app/services/users/project_package_service.rb b/app/services/users/project_package_service.rb new file mode 100644 index 000000000..870cdc98e --- /dev/null +++ b/app/services/users/project_package_service.rb @@ -0,0 +1,76 @@ +class Users::ProjectPackageService < ApplicationService + include CustomSortable + + sort_columns :published_at, default_by: :published_at, default_direction: :desc + + attr_reader :user, :params + + def initialize(user, params) + @user = user + @params = params + end + + def call + packages = category_scope_filter + + packages = user_policy_filter(packages) + + custom_sort(packages, :published_at, params[:sort_direction]) + end + + private + + def category_scope_filter + case params[:category] + when 'bidden' then + user.bidden_project_packages + when 'manage' then + user.project_packages + else + ids = user.bidding_users.pluck(:project_package_id) + user.project_packages.pluck(:id) + ProjectPackage.where(id: ids) + end + end + + def user_policy_filter(relations) + if self_or_admin? + status_filter(relations) + else + relations.visible + end + end + + def status_filter(relations) + return relations unless self_or_admin? + + case params[:category] + when 'bidden' then bidding_status_filter(relations) + when 'manage' then package_status_filter(relations) + else relations + end + end + + def bidding_status_filter(relations) + case params[:status] + when 'bidding_lost' then + relations.where(bidding_users: { status: :bidding_lost }) + when 'bidding_won' then + relations.where(bidding_users: { status: :bidding_won }) + else + relations + end + end + + def package_status_filter(relations) + case params[:status] + when 'unpublished' then relations.invisible + when 'bidding' then relations.where(status: :published) + when 'finished' then relations.where(status: %w[bidding_ended bidding_finished]) + else relations + end + end + + def self_or_admin? + User.current&.id == user.id || User.current&.admin_or_business? + end +end \ No newline at end of file diff --git a/app/views/users/project_packages/index.json.jbuilder b/app/views/users/project_packages/index.json.jbuilder new file mode 100644 index 000000000..a2574d558 --- /dev/null +++ b/app/views/users/project_packages/index.json.jbuilder @@ -0,0 +1,20 @@ +user = observed_user + +json.count @count +json.project_packages do + json.array! @packages.each do |package| + json.extract! package, :id, :title, :status, :min_price, :max_price, :visit_count, :bidding_users_count + + is_creator = user.id == package.creator_id + json.type is_creator ? 'manage' : 'bidden' + json.category_id package.project_package_category_id + json.category_name package.category_name + + unless is_creator + json.bidden_status @bidding_status_map[package.id] + end + + json.deadline_at package.display_deadline_at + json.published_at package.display_published_at + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 16140e99c..bb5bc49cc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,6 +50,7 @@ Rails.application.routes.draw do resource :experience_records, only: [:show] resource :grade_records, only: [:show] resource :watch, only: [:create, :destroy] + resources :project_packages, only: [:index] end