1
0
Fork 0

Select examples by words

This commit is contained in:
Alex Kotov 2023-02-11 09:54:34 +04:00
parent af60b2f721
commit 775e9251c6
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
1 changed files with 18 additions and 10 deletions

28
main.rb
View File

@ -21,14 +21,9 @@ before do
headers 'Access-Control-Allow-Origin' => '*'
end
get '/examples' do
get '/words/:id' do
$DB_POOL.with do |db_conn|
ids =
case params[:ids]
when Array then params[:ids]
when String then params[:ids].split(',')
else raise 'Invalid param "ids"'
end.map { |id| Integer id }
word_id = Integer params[:id]
examples = db_conn.exec_params(
(
@ -40,7 +35,18 @@ get '/examples' do
SELECT example_id, language_id, MIN(index) as min_index
FROM example_texts
WHERE
example_id = ANY($1)
example_id = ANY(
SELECT example_texts.example_id
FROM words
INNER JOIN word_forms
ON word_forms.word_id = words.id
INNER JOIN word_form_example_texts
ON word_form_example_texts.word_form_id = word_forms.id
INNER JOIN example_texts
ON example_texts.id = word_form_example_texts.example_text_id
WHERE words.id = $1
GROUP BY example_texts.example_id
)
AND
(language_id = 5 OR language_id = 2)
GROUP BY example_id, language_id
@ -52,9 +58,11 @@ get '/examples' do
GROUP BY foo.example_id
SQL
),
[PG::TextEncoder::Array.new.encode(ids)],
[word_id],
).map { |row| row['values'] }
json examples
json({
examples: examples,
})
end
end