Commit 4a5093e8 by Ngô Trung Hưng

fix -part 5

parent cde1c3b4
Pipeline #733 failed with stages
in 0 seconds
......@@ -3,5 +3,5 @@
# Description/Explanation of Person class
class City < ApplicationRecord
has_many :city_jobs
has_many :jobs, through: :city_jobs
has_many :jobs, through: :city_jobs
end
......@@ -11,7 +11,7 @@
#
default: &default
adapter: mysql2
encoding: utf8
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: '1'
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateCompanies < ActiveRecord::Migration[5.2]
def change
create_table :companies, options: 'COLLATE=utf8_general_ci' do |t|
create_table :companies do |t|
t.string :name
t.string :address
t.text :short_description
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateCities < ActiveRecord::Migration[5.2]
def change
create_table :cities, options: 'COLLATE=utf8_general_ci' do |t|
create_table :cities do |t|
t.string :name
t.boolean :area
t.timestamps
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateIndustries < ActiveRecord::Migration[5.2]
def change
create_table :industries, options: 'COLLATE=utf8_general_ci' do |t|
create_table :industries do |t|
t.string :name
t.timestamps
end
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateJobs < ActiveRecord::Migration[5.2]
def change
create_table :jobs, options: 'COLLATE=utf8_general_ci' do |t|
create_table :jobs do |t|
t.string :name
t.integer :company_id
t.string :level
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateHistories < ActiveRecord::Migration[5.2]
def change
create_table :histories, options: 'COLLATE=utf8_general_ci' do |t|
create_table :histories do |t|
t.integer :user_id
t.integer :job_id
t.timestamps
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateFavorites < ActiveRecord::Migration[5.2]
def change
create_table :favorites, options: 'COLLATE=utf8_general_ci' do |t|
create_table :favorites do |t|
t.integer :user_id
t.integer :job_id
t.timestamps
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users, options: 'COLLATE=utf8_general_ci' do |t|
create_table :users do |t|
t.string :email
t.string :name
t.string :password_digest
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateCityJobs < ActiveRecord::Migration[5.2]
def change
create_table :city_jobs, options: 'COLLATE=utf8_general_ci' do |t|
create_table :city_jobs do |t|
t.references :job
t.references :city
t.timestamps
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateIndustryJobs < ActiveRecord::Migration[5.2]
def change
create_table :industry_jobs, options: 'COLLATE=utf8_general_ci' do |t|
create_table :industry_jobs do |t|
t.references :industry
t.references :job
t.timestamps
......
......@@ -3,7 +3,7 @@
# Description/Explanation of Person class
class CreateAppliedJobs < ActiveRecord::Migration[5.2]
def change
create_table :applied_jobs, options: 'COLLATE=utf8_general_ci' do |t|
create_table :applied_jobs do |t|
t.references :user
t.references :job
t.string :name
......
......@@ -12,7 +12,7 @@
ActiveRecord::Schema.define(version: 2020_07_28_021412) do
create_table "applied_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "applied_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.bigint "user_id"
t.bigint "job_id"
t.string "name"
......@@ -24,14 +24,14 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.index ["user_id"], name: "index_applied_jobs_on_user_id"
end
create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "name"
t.boolean "area"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "city_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "city_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.bigint "job_id"
t.bigint "city_id"
t.datetime "created_at", null: false
......@@ -40,7 +40,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.index ["job_id"], name: "index_city_jobs_on_job_id"
end
create_table "companies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "companies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "name"
t.string "address"
t.text "short_description"
......@@ -48,27 +48,27 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.datetime "updated_at", null: false
end
create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.integer "user_id"
t.integer "job_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "histories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "histories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.integer "user_id"
t.integer "job_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "industry_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "industry_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.bigint "industry_id"
t.bigint "job_id"
t.datetime "created_at", null: false
......@@ -77,7 +77,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.index ["job_id"], name: "index_industry_jobs_on_job_id"
end
create_table "jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "name"
t.integer "company_id"
t.string "level"
......@@ -90,7 +90,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.datetime "updated_at", null: false
end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "email"
t.string "name"
t.string "password_digest"
......
......@@ -13,8 +13,8 @@ class FtpSever
NAME_CSV = 'jobs.zip'
def data_csv
donwload_csv
CSV.parse(File.read('lib/csv/jobs.csv'), headers: true)
download_csv
CSV.parse(File.read(Rails.root.join('lib', 'csv', 'jobs.csv')), headers: true)
end
def logger
......@@ -23,12 +23,16 @@ class FtpSever
private
def donwload_csv
def jobs_csv_path
Rails.root.join(NAME_CSV)
end
def download_csv
Net::FTP.open(CONTENT_SERVER_DOMAIN_NAME, CONTENT_SERVER_USER_NAME, CONTENT_SERVER_USER_PASSWORD) do |ftp|
ftp.getbinaryfile(NAME_CSV)
Unzip.extract_zip(Rails.root.join(NAME_CSV), Rails.root.join('lib', 'csv'))
File.delete(Rails.root.join(NAME_CSV)) if File.exist?(Rails.root.join(NAME_CSV))
logger.info 'Donwload & extract success'
Unzip.extract_zip((jobs_csv_path), Rails.root.join('lib', 'csv'))
File.delete(jobs_csv_path) if File.exist?(jobs_csv_path)
logger.info 'Download & extract success'
end
end
end
......@@ -15,10 +15,13 @@ class ImportData
import_jobs_from(csv_data)
end
def logger
@logger ||= Logger.new(Rails.root.join('log', 'import_data_csv.log'))
end
private
def import_industries_from(csv)
puts 'Import data industries . . .'
industries = []
industries += csv['category'].map(&:strip)
industries.each do |val|
......@@ -26,30 +29,30 @@ class ImportData
val.gsub!('/', ' / ')
Industry.find_or_create_by(name: val)
end
puts 'Done parse csv industries'
rescue StandardError => e
logger.error "Import_industries: #{e}"
end
def import_cities_from(csv)
puts 'Import data cities . . .'
cities = csv['work place'].uniq.select(&:present?)
cities = cities.map { |val| val.delete('[]\"') }
city_names = csv['work place'].uniq.select(&:present?)
cities = city_names.map { |val| val.delete('[]\"') }
cities.each do |val|
next if val.blank?
City.find_or_create_by(name: val) { |city| city.area = DOMESTIC }
end
rescue StandardError => e
logger.error "Import_cities: #{e}"
end
def import_companies_from(csv)
puts 'Import data companies . . .'
csv['company name'].each_with_index do |name, index|
Company.find_or_create_by(name: name.strip) do |company|
company.address = csv['company address'][index]
company.short_description = csv['benefit'][index]
end
puts index
rescue StandardError => e
puts e
logger.error "Import_companies: #{e}"
end
end
......@@ -57,34 +60,25 @@ class ImportData
csv['name'].each_with_index do |name, index|
desc = "#{csv['requirement'][index]} #{(csv['description'][index])}"
company = Company.find_by name: csv['company name'][index].to_s.strip
company_id = company.blank? ? COMPANY_SECURITY : company.id
company_id = company.try(:id) || COMPANY_SECURITY
begin
job = Job.create!(name: name,
company_id: company_id,
level: csv['level'][index],
salary: csv['salary'][index],
create_date: Time.now,
description: desc)
make_foreign_cities_table(csv['work place'][index], job.id)
make_foreign_industries_table(csv['category'][index], job.id)
job = Job.create(name: name,
company_id: company_id,
level: csv['level'][index],
salary: csv['salary'][index],
create_date: Time.now,
description: desc)
city_names = csv['work place'][index].to_s.delete('[]\"')
city = City.find_or_create_by(name: city_names.strip) { |record| record.area = DOMESTIC }
job.cities << city
industry_name = csv['category'][index].to_s.gsub(',', '/').gsub('/', ' / ')
industry = Industry.find_or_create_by(name: industry_name.strip)
job.industries << industry
puts index
rescue StandardError => e
puts e
logger.error "Import_jobs: #{e}"
end
end
end
def make_foreign_cities_table(data, id_job)
data = data.to_s.delete('[]\"')
city = City.find_or_create_by(name: data.strip) { |record| record.area = DOMESTIC }
city_id = city.id
CityJob.create(job_id: id_job, city_id: city_id)
end
def make_foreign_industries_table(data, id_job)
industry_name = data.to_s.gsub(',', '/').gsub('/', ' / ')
industry = Industry.find_or_create_by(name: industry_name.strip)
industry_id = industry.id
IndustryJob.create(industry_id: industry_id, job_id: id_job)
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