forgeplus/app/controllers/users/projects_controller.rb

31 lines
1.4 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class Users::ProjectsController < Users::BaseController
skip_before_action :check_observed_user_exists!, only: [:search]
def index
cached_data = Rails.cache.read("Users::ProjectsController:index:#{params[:page]}:#{params[:limit]}:#{query_params[:category]}:#{query_params[:status]}:#{query_params[:sort_direction]}:#{query_params[:topic_name]}")
unless cached_data
projects = Users::ProjectService.new(observed_user, query_params).call
data_to_cache = paginate(projects.includes(:project_score, owner: { user_extension: :school }), special: observed_user.is_teacher?)
Rails.cache.write("Users::ProjectsController:index:#{params[:page]}:#{params[:limit]}:#{query_params[:category]}:#{query_params[:status]}:#{query_params[:sort_direction]}:#{query_params[:topic_name]}", data_to_cache, expires_in: 10.minutes)
# 缓存为空时执行API请求等操作
end
@projects = cached_data
@count = @projects.total_count
end
def search
query_params = { keyword: params[:keyword], category: 'manage' }
projects = Users::ProjectService.new(current_user, query_params).call
params[:limit] = params[:per_page].to_i.zero? ? 20 : params[:per_page].to_i
@count = projects.count
@projects = paginate projects
end
private
def query_params
params.permit(:category, :status, :sort_direction, :topic_name)
end
end