Skip to content

Commit

Permalink
fix: some optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsinayoob committed Sep 25, 2024
1 parent 8f82a55 commit d95102f
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions app/controllers/api/qdc/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,10 @@ def country_language_preference
user_device_language = request.query_parameters[:user_device_language]
country = request.query_parameters[:country]&.upcase

if user_device_language.blank?
return render_bad_request('user_device_language is required')
if user_device_language.blank? || country.blank?
return render_bad_request("#{user_device_language.blank? ? 'user_device_language' : 'country'} is required")
end

if country.blank?
return render_bad_request('country is required')
end

unless Language.exists?(iso_code: user_device_language)
return render_bad_request('Invalid user_device_language')
end
Expand All @@ -143,22 +139,28 @@ def country_language_preference
.find_by(user_device_language: user_device_language, country: nil)

if @preference
@data = {
preference: @preference,
default_mushaf: @preference.mushaf,
default_translations: @preference.default_translation_ids.present? ? ResourceContent.where(id: @preference.default_translation_ids.split(',')).approved : [],
default_tafsir: @preference.tafsir,
default_wbw_language: @preference.wbw_language,
default_reciter: @preference.reciter,
ayah_reflections_languages: Language.where(iso_code: @preference.ayah_reflections_languages&.split(',') || []),
learning_plan_languages: Language.where(iso_code: @preference.learning_plan_languages&.split(',') || [])
}
@data = build_preference_data(@preference)
render
else
render_404("No matching country language preference found")
end
end

private

def build_preference_data(preference)
{
preference: preference,
default_mushaf: preference.mushaf,
default_translations: preference.default_translation_ids.present? ? ResourceContent.where(id: preference.default_translation_ids.split(',')).approved : [],
default_tafsir: preference.tafsir,
default_wbw_language: preference.wbw_language,
default_reciter: preference.reciter,
ayah_reflections_languages: Language.where(iso_code: preference.ayah_reflections_languages&.split(',') || []),
learning_plan_languages: Language.where(iso_code: preference.learning_plan_languages&.split(',') || [])
}
end

protected

def load_translations
Expand Down

0 comments on commit d95102f

Please sign in to comment.