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

Commit

Permalink
Add points and extended subjects data from warsaw (#50)
Browse files Browse the repository at this point in the history
* add data fron warsaw in json

* add new models

* remove all references to previous model names

* remove class profiles from institutions

* add creating subjects and mapping gdynia data to subjects

* saving work, add warsaw data processing service object

* process data from warsaw

* save work on subject filtering

* debugging saving work

* almost finished just cleanup left

* update docs

* remove bug

* refactor index controller

* remove refactoring leftovers

* remove error

* test /subjects endpoint

* rewrite and test CreateSubjectsService

* use map inside CreaseSubjectService instead of each

* refactor loading raw school data in warsaw job

* refactor warsaw processing data service

* add comments in warsaw data service

* refactor creating subject set requirements info to another service and test it

* create SubjectSetRequirementInfo factory

* remove unnecessary specs

Co-authored-by: Pawel Biegun <[email protected]>
  • Loading branch information
Anakin100100 and Pawel Biegun authored May 14, 2022
1 parent 907f173 commit 78b173b
Show file tree
Hide file tree
Showing 28 changed files with 7,206 additions and 25 deletions.
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
institutions_count = institutions.count.size
else
institutions_count = institutions.count
end

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
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
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

return subject_names_array
end
end
Loading

0 comments on commit 78b173b

Please sign in to comment.