forked from Trustie/forgeplus
启智竞赛
This commit is contained in:
parent
521a1a2c8a
commit
9b91557bb6
|
|
@ -0,0 +1,76 @@
|
|||
class CompetitionInfosController < ApplicationController
|
||||
before_action :find_competition, only: [:show, :enroll, :upload, :enroll_status]
|
||||
before_action :manager_competition, only: [:enroll_list]
|
||||
|
||||
def enroll_list
|
||||
competition_users = CompetitionUser.all.order("updated_at desc")
|
||||
if params[:zone].present?
|
||||
competition_users = competition_users.where(zone: params[:zone].to_s)
|
||||
end
|
||||
if params[:sub_competition].present?
|
||||
competition_users = competition_users.where(sub_competition: params[:sub_competition].to_s)
|
||||
end
|
||||
if params[:upload].to_s == "true"
|
||||
competition_users = competition_users.joins("join attachments on attachments.container_type='CompetitionUser' and attachments.container_id = competition_users.id")
|
||||
end
|
||||
@competition_users_count = competition_users.count
|
||||
@competition_users = paginate(competition_users.select("DISTINCT competition_users.*"))
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def enroll
|
||||
competition_user = CompetitionUser.find_by(competition_info: @competition_info.id, user_id: current_user.id)
|
||||
tip_exception "您已经报名!" if competition_user.present?
|
||||
competition_user = CompetitionUser.new(enroll_params)
|
||||
competition_user.competition_info = @competition_info
|
||||
competition_user.user = current_user
|
||||
competition_user.save
|
||||
end
|
||||
|
||||
def enroll_status
|
||||
@competition_user = CompetitionUser.find_by(competition_info: @competition_info.id, user_id: current_user.id)
|
||||
end
|
||||
|
||||
def upload
|
||||
competition_user = CompetitionUser.find_by(competition_info: @competition_info.id, user_id: current_user.id)
|
||||
tip_exception "未报名,请先报名" if competition_user.blank?
|
||||
tip_exception "未提交作品" if params[:attachment_ids].blank?
|
||||
tip_exception "您已经提交作品!" if competition_user.present? && competition_user.attachments.present?
|
||||
if competition_user.present? && params[:attachment_ids].present?
|
||||
params[:attachment_ids].each do |id|
|
||||
attachment = Attachment.find_by_id(id)
|
||||
unless attachment.blank?
|
||||
attachment.container = competition_user
|
||||
attachment.author_id = current_user.id
|
||||
attachment.description = ""
|
||||
attachment.save
|
||||
end
|
||||
end
|
||||
render_ok
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def enroll_params
|
||||
params.permit(:org_name, :org_job, :org_rank, :leader, :zone, :sub_competition, :phone,
|
||||
:subject_source_type, :subject_source_name)
|
||||
end
|
||||
|
||||
def find_competition
|
||||
if CompetitionInfo.where(:identifier => params[:id]).exists?
|
||||
@competition_info = CompetitionInfo.where(:identifier => params[:id]).first
|
||||
else
|
||||
@competition_info = CompetitionInfo.find(params[:id])
|
||||
end
|
||||
end
|
||||
|
||||
def manager_competition
|
||||
if current_user.admin?
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: competition_infos
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# identifier :string(255)
|
||||
# title :string(255)
|
||||
# content :text(4294967295)
|
||||
# video_url :string(255)
|
||||
# manager_ids :string(255)
|
||||
# zones :string(255)
|
||||
# sub_competitions :string(255)
|
||||
# is_local :boolean default(FALSE)
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_competition_infos_on_identifier (identifier) UNIQUE
|
||||
#
|
||||
|
||||
class CompetitionInfo < ApplicationRecord
|
||||
has_many :competition_users
|
||||
end
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: competition_users
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# competition_info_id :integer
|
||||
# user_id :integer
|
||||
# leader :string(255)
|
||||
# org_name :string(255)
|
||||
# org_job :string(255)
|
||||
# org_rank :string(255)
|
||||
# zone :string(255)
|
||||
# sub_competition :string(255)
|
||||
# subject_source_type :integer
|
||||
# subject_source_name :string(255)
|
||||
# phone :string(255)
|
||||
# members :text(65535)
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_competition_users_on_competition_info_id (competition_info_id)
|
||||
# index_competition_users_on_user_id (user_id)
|
||||
#
|
||||
|
||||
class CompetitionUser < ApplicationRecord
|
||||
belongs_to :competition_info
|
||||
belongs_to :user
|
||||
|
||||
has_many :attachments, as: :container, dependent: :destroy
|
||||
|
||||
serialize :members, JSON
|
||||
|
||||
# %i[real_name org_name org_job org_rank].each do |method_name|
|
||||
# define_method method_name do
|
||||
# members&.[](method_name.to_s)
|
||||
# end
|
||||
#
|
||||
# define_method "#{method_name}=" do |value|
|
||||
# self.config ||= {}
|
||||
# members.[]=(method_name.to_s, value)
|
||||
# end
|
||||
# end
|
||||
end
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
json.status 0
|
||||
json.message "success"
|
||||
json.count @competition_users_count
|
||||
json.data do
|
||||
json.array! @competition_users.to_a do |c_user|
|
||||
json.extract! c_user, :org_name, :org_job, :org_rank, :leader, :zone, :sub_competition, :subject_source_type, :subject_source_name, :phone
|
||||
json.user_id c_user.user_id
|
||||
json.user_real_name c_user.user&.real_name
|
||||
json.members c_user.members
|
||||
json.attachments c_user.attachments do |attachment|
|
||||
json.partial! "attachments/attachment_simple", locals: {attachment: attachment}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
json.status 0
|
||||
json.message "success"
|
||||
json.data do
|
||||
json.enroll_status @competition_user.present?
|
||||
json.upload_status @competition_user.present? ? @competition_user.attachments.present? : false
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
json.status 0
|
||||
json.message "success"
|
||||
json.data do
|
||||
json.extract! @competition_info, :id, :identifier, :title, :content, :video_url, :is_local
|
||||
json.zones @competition_info.zones.to_s.split(",")
|
||||
json.sub_competitions @competition_info.sub_competitions.to_s.split(",")
|
||||
end
|
||||
1778
config/routes.rb
1778
config/routes.rb
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,16 @@
|
|||
class CreateCompetitionInfos < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :competition_infos do |t|
|
||||
t.string :identifier
|
||||
t.string :title
|
||||
t.longtext :content
|
||||
t.string :video_url
|
||||
t.string :manager_ids
|
||||
t.string :zones
|
||||
t.string :sub_competitions
|
||||
t.boolean :is_local, default: false
|
||||
t.timestamps
|
||||
end
|
||||
add_index :competition_infos, :identifier, unique: true
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
class CreateCompetitionUsers < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :competition_users do |t|
|
||||
t.references :competition_info
|
||||
t.references :user
|
||||
t.string :leader
|
||||
t.string :org_name
|
||||
t.string :org_job
|
||||
t.string :org_rank
|
||||
t.string :zone
|
||||
t.string :sub_competition
|
||||
t.integer :subject_source_type
|
||||
t.string :subject_source_name
|
||||
t.string :phone
|
||||
t.text :members
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue