diff --git a/app/controllers/weapps/subjects_controller.rb b/app/controllers/weapps/subjects_controller.rb new file mode 100644 index 000000000..ab578e5e1 --- /dev/null +++ b/app/controllers/weapps/subjects_controller.rb @@ -0,0 +1,28 @@ +class Weapps::SubjectsController < Weapps::BaseController + before_action :require_login + before_action :find_subject, except: [:index] + + # 首页 + def index + subjects = Weapps::SubjectQuery.call(current_laboratory, params) + @subject_count = subjects.map(&:id).size + @subjects = paginate subjects + end + + # 详情 + def show + # 合作团队 + Rails.logger.info("##########subject: #{@subject.id}") + @members = @subject.subject_members.includes(:user) + shixuns = @subject.shixuns.published.pluck(:id) + challenge_ids = Challenge.where(shixun_id: shixuns).pluck(:id) + # 实训路径中的所有实训标签 + @tags = ChallengeTag.where(challenge_id: challenge_ids).pluck(:name).uniq + end + + private + def find_subject + @subject = Subject.find(params[:id]) + end + +end diff --git a/app/queries/weapps/subject_query.rb b/app/queries/weapps/subject_query.rb new file mode 100644 index 000000000..e867c312b --- /dev/null +++ b/app/queries/weapps/subject_query.rb @@ -0,0 +1,37 @@ +class Weapps::SubjectQuery < ApplicationQuery + include CustomSortable + attr_reader :params + + def initialize(current_laboratory, params) + @current_laboratory = current_laboratory + @params = params + end + + def call + subjects = @current_laboratory.subjects + + # 课程体系的过滤 + if params[:sub_discipline_id].present? + subjects = subjects.joins(:sub_disciplines).where(sub_disciplines: {id: params[:sub_discipline_id]}) + elsif params[:discipline_id].present? + subjects = subjects.joins(:sub_disciplines).where(sub_disciplines: {discipline_id: params[:discipline_id]}) + else + subjects = subjects.joins(:sub_discipline_containers).where(sub_discipline_containers: {container_type: "Subject"}) + end + + subjects = subjects.left_joins(:shixuns).select('subjects.id, subjects.name, subjects.excellent, subjects.stages_count, subjects.status, + subjects.shixuns_count, subjects.updated_at, IFNULL(sum(shixuns.myshixuns_count), 0) myshixuns_count') + .group('subjects.id').order("#{order_type} #{sort_type}") + subjects + end + + private + + def order_type + params[:order] || "updated_at" + end + + def sort_type + params[:sort] || "desc" + end +end \ No newline at end of file diff --git a/app/views/weapps/subjects/index.json.jbuilder b/app/views/weapps/subjects/index.json.jbuilder new file mode 100644 index 000000000..64b18c4e9 --- /dev/null +++ b/app/views/weapps/subjects/index.json.jbuilder @@ -0,0 +1,7 @@ +json.subjects @subjects do |subject| + json.(subject, :id, :excellent, :name, :stages_count, :shixuns_count, :myshixuns_count) + json.image_url url_to_avatar(subject) + json.allow_visit subject.status > 1 || User.current.manager_of_subject?(subject) || User.current.admin? +end + +json.total_count @subject_count \ No newline at end of file diff --git a/app/views/weapps/subjects/show.json.jbuilder b/app/views/weapps/subjects/show.json.jbuilder new file mode 100644 index 000000000..757257e82 --- /dev/null +++ b/app/views/weapps/subjects/show.json.jbuilder @@ -0,0 +1,17 @@ +json.subject do + json.name @subject.name + json.description @subject.description + json.shixuns_count @subject.shixuns_count + json.member_count @subject.member_count + json.subject_score @subject.all_score + json.learning_notes @subject.learning_notes +end + +json.team_title @subject.team_title +json.members @members do |member| + json.partial! 'subjects/subject_member', locals: { user: member.user } + json.role member.role +end + +# 技能标签 +json.tags_name @tags \ No newline at end of file diff --git a/config/initializers/session_store.rb b/config/initializers/session_store.rb index 06ba35f6e..2bbdbd0d2 100644 --- a/config/initializers/session_store.rb +++ b/config/initializers/session_store.rb @@ -4,5 +4,5 @@ # Rails.application.config.session_store :active_record_store # Be sure to restart your server when you modify this file. -Rails.application.config.session_store :cache_store, :expire_after => 24.hours, key: '_educoder_session', domain: :all +# Rails.application.config.session_store :cache_store, :expire_after => 24.hours, key: '_educoder_session', domain: :all diff --git a/config/routes.rb b/config/routes.rb index e7482d813..2b91375ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1018,6 +1018,8 @@ Rails.application.routes.draw do post :cancel_sticky, on: :collection end + resources :subjects, path: :paths, only: [:index, :create, :update, :edit, :show] + resources :courses, only: [:create, :update, :edit, :show] do member do get :shixun_homework_category