Merge branch 'master' of https://bdgit.educoder.net/Hjqreturn/educoder into dev_aliyun
commit
c696207ac3
@ -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
|
@ -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
|
@ -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
|
Loading…
Reference in new issue