Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Add points and extended subjects data from warsaw #50

Merged
merged 24 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2db92c9
add data fron warsaw in json
Apr 21, 2022
b087a7b
add new models
Anakin100100 Apr 24, 2022
9682b6f
remove all references to previous model names
Anakin100100 Apr 24, 2022
b558dc6
remove class profiles from institutions
Anakin100100 Apr 24, 2022
f46e3cc
add creating subjects and mapping gdynia data to subjects
Anakin100100 Apr 24, 2022
29e312a
saving work, add warsaw data processing service object
Anakin100100 Apr 24, 2022
e8cd2ac
process data from warsaw
Anakin100100 Apr 25, 2022
ec6117e
save work on subject filtering
Anakin100100 Apr 25, 2022
915e3c2
debugging saving work
Anakin100100 Apr 27, 2022
cd77a69
almost finished just cleanup left
Anakin100100 Apr 27, 2022
179f8db
update docs
Anakin100100 Apr 27, 2022
5de6fa7
remove bug
Anakin100100 Apr 27, 2022
b310591
refactor index controller
May 2, 2022
369af2b
remove refactoring leftovers
May 2, 2022
78a2527
remove error
May 2, 2022
ebff809
test /subjects endpoint
May 2, 2022
40b92e1
rewrite and test CreateSubjectsService
May 2, 2022
d12fa32
use map inside CreaseSubjectService instead of each
May 2, 2022
723377a
refactor loading raw school data in warsaw job
Anakin100100 May 4, 2022
77d00e2
refactor warsaw processing data service
Anakin100100 May 4, 2022
6cb483e
add comments in warsaw data service
Anakin100100 May 4, 2022
b8e70aa
refactor creating subject set requirements info to another service an…
Anakin100100 May 4, 2022
09bc97a
create SubjectSetRequirementInfo factory
Anakin100100 May 4, 2022
0a45210
remove unnecessary specs
Anakin100100 May 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions app/controllers/institutions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Controller for reading and filering the school
class InstitutionsController < ApplicationController
before_action :extract_page_presence, :ensure_page_size_is_positive, :area_query, :name_query, :page_size,
:public_school, :school_rspo_type_ids, :class_profiles, :sports,
:public_school, :school_rspo_type_ids, :class_profiles, :sports,
:foreign_languages, :extracurricular_activities, only: [:index]
before_action :institution_id, only: [:show]
def index
Expand All @@ -12,12 +12,17 @@ def index
institutions = institutions.search_by_name(@name_query) unless @name_query.nil?
institutions = institutions.where(rspo_institution_type_id: @school_rspo_type_ids) unless @school_rspo_type_ids.nil?
institutions = institutions.where(public: @public_school) unless @public_school.nil?
institutions = institutions.search_by_class_profiles(@class_profiles) unless @class_profiles.nil?
institutions = institutions.search_by_sports(@sports) unless @sports.nil?
institutions = institutions.search_by_foreign_languages(@foreign_languages) unless @foreign_languages.nil?
institutions = institutions.search_by_extracurricular_activities(@extracurricular_activities) unless @extracurricular_activities.nil?
institutions = filter_by_subjects_if_present(@class_profiles, institutions)

if @class_profiles != nil
Anakin100100 marked this conversation as resolved.
Show resolved Hide resolved
institutions_count = institutions.count.size
else
institutions_count = institutions.count
end
Anakin100100 marked this conversation as resolved.
Show resolved Hide resolved

institutions_count = institutions.count
@paginated_institutions = institutions.paginate(page: @page, per_page: @page_size)

render status: '200', json: {
Expand All @@ -33,7 +38,7 @@ def show
institution = Institution.find_by(id: @institution_id)
render status: :bad_request, json: { message: 'school does not exists' } and return if institution.nil?

render status: '200', json: institution
render status: '200', json: institution.to_json(include: {subject_sets: {include: [:subjects, :subject_set_requirements_info]}})
end

private
Expand Down Expand Up @@ -78,29 +83,62 @@ def school_rspo_type_ids

def class_profiles
@class_profiles = params.fetch(:class_profiles, nil)
if @class_profiles.nil? == false
@class_profiles = @class_profiles.gsub(",", " ")
end
@class_profiles = @class_profiles.strip.split(",") if @class_profiles.nil? == false
end

def sports
@sports = params.fetch(:sports, nil)
if @sports.nil? == false
@sports = @sports.gsub(",", " ")
end
@sports = @sports.gsub(',', ' ') if @sports.nil? == false
end

def foreign_languages
@foreign_languages = params.fetch(:foreign_languages, nil)
if @foreign_languages.nil? == false
@foreign_languages = @foreign_languages.gsub(",", " ")
end
@foreign_languages = @foreign_languages.gsub(',', ' ') if @foreign_languages.nil? == false
end

def extracurricular_activities
@extracurricular_activities = params.fetch(:extracurricular_activities, nil)
if @extracurricular_activities.nil? == false
@extracurricular_activities = @extracurricular_activities.gsub(",", " ")
if @extracurricular_activities.nil? == false
@extracurricular_activities = @extracurricular_activities.gsub(',', ' ')
end
end

def filter_by_subjects_if_present(class_profiles, institutions)
case class_profiles
when nil
return institutions
when Array
class_profiles_pg_array = get_class_profiles_pg_array(class_profiles)

return institutions.filter_by_subjects(class_profiles_pg_array)
end
end

def get_class_profiles_pg_array(class_profiles)
size = class_profiles.size

if size > 1
class_profiles_pg_array = class_profiles_pg_array_from_class_profiles(class_profiles)
elsif size == 1
class_profiles_pg_array = class_profiles_pg_array_from_single_class_profiles(class_profiles)
end

return class_profiles_pg_array
end

def class_profiles_pg_array_from_multiple_class_profiles(class_profiles)
class_profiles_pg_array = "{"
class_profiles.each_with_index do |profile, index|
if index != class_profiles.length - 1
class_profiles_pg_array += profile + ","
else
class_profiles_pg_array += profile
end
end
class_profiles_pg_array += "}"
end

def class_profiles_pg_array_from_single_class_profiles(class_profiles)
class_profiles_pg_array = "{" + class_profiles[0] + "}"
end
end
8 changes: 8 additions & 0 deletions app/controllers/subjects_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class SubjectsController < ApplicationController
def index
@subjects = Subject.all
render status: '200', json: {
subjects: @subjects
}
end
end
Anakin100100 marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions app/jobs/create_subjects_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class CreateSubjectsJob < ApplicationJob
queue_as :default

def perform()
CreateSubjectsService.new.call
end
end

10 changes: 10 additions & 0 deletions app/jobs/process_warsaw_data_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class ProcessWarsawDataJob < ApplicationJob
queue_as :default

def perform()
ProcessWarsawDataService.new.call
end
end

13 changes: 8 additions & 5 deletions app/models/institution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

class Institution < ApplicationRecord
belongs_to :institution_type
has_many :subject_sets, dependent: :destroy

include PgSearch::Model

pg_search_scope :search_by_name, against: :name, using: :dmetaphone
pg_search_scope :search_by_area, against: %i[county municipality town]
pg_search_scope :search_by_class_profiles, against: :class_profiles, using: {tsearch: {any_word: true} }
pg_search_scope :search_by_sports, against: :sports, using: {tsearch: {any_word: true} }
pg_search_scope :search_by_foreign_languages, against: :foreign_languages, using: {tsearch: {any_word: true} }
pg_search_scope :search_by_extracurricular_activities, against: :extracurricular_activities, using: {tsearch: {any_word: true} }
pg_search_scope :search_by_class_profiles, against: :class_profiles, using: { tsearch: { any_word: true } }
pg_search_scope :search_by_sports, against: :sports, using: { tsearch: { any_word: true } }
pg_search_scope :search_by_foreign_languages, against: :foreign_languages, using: { tsearch: { any_word: true } }
pg_search_scope :search_by_extracurricular_activities, against: :extracurricular_activities,
using: { tsearch: { any_word: true } }

scope :filter_by_subjects, ->(class_profiles_pg_array) { joins(subject_sets: :subjects).group(:id).having("ARRAY_AGG(subjects.name::text) @> ?", class_profiles_pg_array) }


def address
"#{town} #{street} #{building_no}/#{apartment_no} #{zip_code}"
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/subject.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Subject < ApplicationRecord
has_and_belongs_to_many :subject_sets
end
7 changes: 7 additions & 0 deletions app/models/subject_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class SubjectSet < ApplicationRecord
has_one :subject_set_requirements_info, dependent: :destroy
belongs_to :institution
has_and_belongs_to_many :subjects
end
5 changes: 5 additions & 0 deletions app/models/subject_set_requirements_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class SubjectSetRequirementsInfo < ApplicationRecord
belongs_to :subject_set
end
40 changes: 40 additions & 0 deletions app/services/create_subjects_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class CreateSubjectsService < ApplicationService
#This service creates all the subjects that can be included in subject sets.
def call
#These are the only extended subject fields can be used in subject_sets
#If there are new subjects that are not in this list it should be updated
subject_names = [
"Polski",
"Matematyka",
"Fizyka",
"Chemia",
"Geografia",
"Historia",
"WOS",
"Informatyka",
"Biologia",
"Sztuka",
"Dziennikarstwo",
"Prawo",
"Medycyna",
"Nauki ścisłe",
"Ekonomia",
"Zarządzanie",
"Angielski",
"Hiszpański",
"Niemiecki",
"Historia Sztuki",
"Włoski",
"Rosyjski",
"Francuski",
"Gotowanie",
"Muzyka",
"Technik",
"Antyk",
"Fotografia",
"Złotnik"
]

subject_names.map { |name| Subject.create(name: name) }
end
end
53 changes: 51 additions & 2 deletions app/services/gdynia_extra_data_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,59 @@ def call(raw_data)
rspo_match[:description] = raw_school.fetch('opis_szkoly')
rspo_match[:sports] = raw_school.fetch('sport').join(',')
rspo_match[:foreign_languages] = raw_school.fetch('jezyki_obce').join(',')
rspo_match[:class_profiles] = raw_school.fetch('profile_klas').join(',')
rspo_match[:extracurricular_activities] = raw_school.fetch('zajecia_dodatkowe').join(',')

rspo_match.save

class_profiles = raw_school.fetch('profile_klas')
if class_profiles != "" && class_profiles.size != 0
class_profiles.each do |class_profile|
subject_set = SubjectSet.create(institution_id: rspo_match.id)
subject_names = gdynia_class_profile_to_subject_names_array(class_profile)

subject_names.each do |subject_name|
subject = Subject.where(name: subject_name)

if subject.empty?
raise "The subject #{subject_name} does not exist in the database, make sure that subjects have been populated"
elsif subject.count > 1
raise "There are more than one subjects with the name #{subject_name}, make sure only one exists in the database"
end

subject_set.subjects << subject
end
end
end
Anakin100100 marked this conversation as resolved.
Show resolved Hide resolved
end
end

def gdynia_class_profile_to_subject_names_array(class_profile)
subject_names_array = []

case class_profile
when "matematyczna"
subject_names_array << "Matematyka"
when "biologiczna"
subject_names_array << "Biologia"
when "artystyczna"
subject_names_array << "Sztuka"
when "dziennikarsko-prawnicza"
subject_names_array << "Dziennikarstwo"
subject_names_array << "Prawo"
when "politechniczna"
subject_names_array << "Nauki ścisłe"
when "medyczna"
subject_names_array << "Medycyna"
when "informatyczna"
subject_names_array << "Informatyka"
when "ekonomiczno-menadżerska"
subject_names_array << "Ekonomia"
subject_names_array << "Zarządzanie"
when "dziennikarsko-prawnicza"
subject_names_array << "Dziennikarstwo"
subject_names_array << "Prawo"
end
Anakin100100 marked this conversation as resolved.
Show resolved Hide resolved

return subject_names_array
end
end
Loading