diff --git a/main.rb b/main.rb index 46ad006..d36b2c3 100755 --- a/main.rb +++ b/main.rb @@ -28,6 +28,7 @@ get '/words/:id' do json({ primary_form: context.primary_form(word_id), part_of_speech: context.part_of_speech_name(word_id), + translations: context.translations(word_id), examples: context.examples(word_id), }) end @@ -115,6 +116,37 @@ public str unless str.empty? end + def translations(word_id) + word_id = Integer word_id + + values = @db_conn.exec_params( + ( + <<~SQL + SELECT + translation_texts.value, + translation_texts.commentary + FROM words + INNER JOIN translations + ON translations.word_id = words.id + INNER JOIN translation_texts + ON translation_texts.translation_id = translations.id + WHERE + word_id = $1 + AND + translation_texts.lang_id = $2 + SQL + ), + [word_id, user_lang_id], + ).to_a + + values.map do |row| + { + translation: String(row['value']).strip.freeze, + commentary: String(row['commentary']).strip.freeze, + }.freeze + end.freeze + end + ## # @return [Array] #