Commit ca3f0a29 by nnnghia98

check if city/industry id valid

parent a9f7ae86
class JobsController < ApplicationController class JobsController < ApplicationController
before_action :authenticate_user!, only: [:apply, :confirm_apply, :finish_apply, :applied_jobs] before_action :authenticate_user!, only: [:apply, :confirm_apply, :finish_apply, :applied_jobs]
before_action :find_user, only: :apply_available before_action :find_user, only: :apply_available
before_action :validate_city_industry, only: :index
def index def index
@search = params[:search] || params[:city_id] || params[:industry_id] || ":" @search = params[:search] || params[:city_id] || params[:industry_id] || ":"
...@@ -59,4 +60,12 @@ class JobsController < ApplicationController ...@@ -59,4 +60,12 @@ class JobsController < ApplicationController
def find_user def find_user
@user = User.find_by(:id) @user = User.find_by(:id)
end end
def validate_city_industry
if params[:city_id]
redirect_to jobs_path unless City.find_by(id: params[:city_id])
elsif params[:industry_id]
redirect_to jobs_path unless Industry.find_by(id: params[:industry_id])
end
end
end end
require "rsolr" require "rsolr"
class SolrService class SolrService
def initialize(search_keyword) def initialize(search_keyword = {})
@solr = RSolr.connect( @solr = RSolr.connect(
url: Settings.solr.connection.server_url, url: Settings.solr.connection.server_url,
read_timeout: Settings.solr.connection.read_timeout, read_timeout: Settings.solr.connection.read_timeout,
...@@ -28,11 +28,11 @@ class SolrService ...@@ -28,11 +28,11 @@ class SolrService
} }
end end
jobs_solr_index.each_slice(5000) do |job| jobs_solr_index.each_slice(5000) do |jobs|
@solr.add job @solr.add jobs
rescue Exception rescue Exception
job.each do |j| jobs.each do |job|
@solr.add j @solr.add job
rescue rescue
solr_index_error = ActiveSupport::Logger.new("log/solr_errors.log") solr_index_error = ActiveSupport::Logger.new("log/solr_errors.log")
solr_index_error.info "This block got error! Cannot add job with id #{job.id}" solr_index_error.info "This block got error! Cannot add job with id #{job.id}"
...@@ -56,9 +56,12 @@ class SolrService ...@@ -56,9 +56,12 @@ class SolrService
end end
def query_by_city def query_by_city
city = City.find(@search_keyword) city = City.find_by(id: @search_keyword)
return { "numFound": 0, "docs": [] } unless city
city_name = city.name city_name = city.name
q = "*:*" q = "*:*"
fq = "city: #{escape_str(city_name)}" fq = "city: #{escape_str(city_name)}"
...@@ -66,7 +69,9 @@ class SolrService ...@@ -66,7 +69,9 @@ class SolrService
end end
def query_by_industry def query_by_industry
industry = Industry.find(@search_keyword) industry = Industry.find_by(id: @search_keyword)
return { "numFound": 0, "docs": [] } unless industry
industry_name = industry.name industry_name = industry.name
q = "*:*" q = "*:*"
......
...@@ -13,15 +13,15 @@ namespace :solr do ...@@ -13,15 +13,15 @@ namespace :solr do
task solr_index: :environment do task solr_index: :environment do
SolrService.new.add_data SolrService.new.add_data
solr_index_data = ActiveSupport::Logger.new("log/solr_service.log") solr_index_logger = ActiveSupport::Logger.new("log/solr_service.log")
solr_index_data.info "Solr index data succesfully at #{Time.current}" solr_index_logger.info "Solr index data succesfully at #{Time.current}"
end end
desc "solr delete data" desc "solr delete data"
task solr_delete: :environment do task solr_delete: :environment do
SolrService.new.delete_data SolrService.new.delete_data
solr_delete_data = ActiveSupport::Logger.new("log/solr_service.log") solr_delete_logger = ActiveSupport::Logger.new("log/solr_service.log")
solr_delete_data.info "Solr delete all data succesfully at #{Time.current}" solr_delete_logger.info "Solr delete all data succesfully at #{Time.current}"
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment