1
0
Fork 0

Return word commentary

This commit is contained in:
Alex Kotov 2023-02-19 09:06:01 +04:00
parent 83092cee1b
commit f74bfce7a8
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
1 changed files with 19 additions and 8 deletions

27
main.rb
View File

@ -25,9 +25,12 @@ get '/words/:id' do
Context.call db_pool: $DB_POOL, user_lang_id: 2 do |context|
word_id = Integer params[:id]
word_info = context.word_info(word_id)
json({
primary_form: context.primary_form(word_id),
primary_form: word_info[:primary_form],
part_of_speech: context.part_of_speech_name(word_id),
commentary: word_info[:commentary],
translations: context.translations(word_id),
examples: context.examples(word_id),
})
@ -70,18 +73,26 @@ private
public
##
# @return [String, nil]
# @return [Hash{Symbol => String}, nil]
#
def primary_form(word_id)
def word_info(word_id)
word_id = Integer word_id
column = @db_conn.exec_params(
'SELECT primary_form FROM words WHERE id = $1',
row = @db_conn.exec_params(
'SELECT primary_form, commentary FROM words WHERE id = $1',
[word_id],
).values.first&.first
).to_a.first
str = String(column).strip.freeze
str unless str.empty?
primary_form = String(row['primary_form']).strip.freeze
primary_form = nil if primary_form.empty?
commentary = String(row['commentary']).strip.freeze
commentary = nil if commentary.empty?
{
primary_form: primary_form,
commentary: commentary,
}.freeze
end
##