1
0
Fork 0
backend/main.rb

200 lines
4.7 KiB
Ruby
Raw Normal View History

2023-02-04 23:43:57 +00:00
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'connection_pool'
require 'pg'
require 'sinatra'
2023-02-05 02:44:16 +00:00
require 'sinatra/json'
2023-02-04 23:43:57 +00:00
$DB_POOL = ConnectionPool.new size: 5, timeout: 5 do
PG.connect(
host: 'pg.causa-arcana.com',
dbname: 'leqsikoni',
user: 'leqsikoni',
password: 'ggeucene3ou7mqh2upehhm52tfp5bkcj',
2023-02-05 01:18:22 +00:00
).tap do |conn|
conn.type_map_for_results = PG::BasicTypeMapForResults.new conn
end
2023-02-04 23:43:57 +00:00
end
2023-02-05 02:53:47 +00:00
before do
headers 'Access-Control-Allow-Origin' => '*'
end
2023-02-11 05:54:34 +00:00
get '/words/:id' do
2023-02-18 10:40:01 +00:00
Context.call db_pool: $DB_POOL, user_lang_id: 2 do |context|
2023-02-11 05:54:34 +00:00
word_id = Integer params[:id]
2023-02-05 01:18:22 +00:00
2023-02-18 09:44:02 +00:00
json({
2023-02-18 11:43:07 +00:00
primary_form: context.primary_form(word_id),
2023-02-18 09:44:02 +00:00
part_of_speech: context.part_of_speech_name(word_id),
2023-02-18 14:09:20 +00:00
translations: context.translations(word_id),
2023-02-18 09:44:02 +00:00
examples: context.examples(word_id),
})
end
end
class Context
private_class_method :new
def self.call(db_pool:, **kwargs, &block)
new(**kwargs).send :call, db_pool: db_pool, &block
end
def initialize(user_lang_id:)
self.user_lang_id = user_lang_id
end
private
attr_reader :user_lang_id
def call(db_pool:)
db_pool.with do |db_conn|
@db_conn = db_conn
result = yield self
@db_conn = nil
result
end
end
def user_lang_id=(user_lang_id)
return @user_lang_id = nil if user_lang_id.nil?
user_lang_id = Integer user_lang_id
raise unless user_lang_id.positive?
@user_lang_id = user_lang_id
end
public
2023-02-18 11:43:07 +00:00
##
# @return [String, nil]
#
def primary_form(word_id)
word_id = Integer word_id
column = @db_conn.exec_params(
'SELECT primary_form FROM words WHERE id = $1',
[word_id],
).values.first&.first
str = String(column).strip.freeze
str unless str.empty?
end
2023-02-18 09:44:02 +00:00
##
# @return [String, nil]
#
def part_of_speech_name(word_id)
word_id = Integer word_id
column = @db_conn.exec_params(
2023-02-18 07:59:47 +00:00
(
<<~SQL
2023-02-18 10:40:01 +00:00
SELECT part_names.value
2023-02-18 07:59:47 +00:00
FROM words
INNER JOIN parts
ON words.part_id = parts.id
2023-02-18 10:40:01 +00:00
INNER JOIN part_names
ON part_names.part_id = parts.id
WHERE
words.id = $1
AND (
part_names.name_lang_id = $2
OR
$2 IS NULL
)
2023-02-18 07:59:47 +00:00
LIMIT 1
SQL
),
2023-02-18 10:40:01 +00:00
[word_id, user_lang_id],
2023-02-18 08:07:11 +00:00
).values.first&.first
2023-02-18 07:59:47 +00:00
2023-02-18 09:44:02 +00:00
str = String(column).strip.freeze
str unless str.empty?
end
2023-02-18 14:09:20 +00:00
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
2023-02-18 14:14:42 +00:00
ORDER BY translations.index ASC
2023-02-18 14:09:20 +00:00
SQL
),
[word_id, user_lang_id],
).to_a
2023-02-18 14:14:42 +00:00
values.map.with_index do |row, index|
2023-02-18 14:32:15 +00:00
commentary = String(row['commentary']).strip.freeze
commentary = nil if commentary.empty?
2023-02-18 14:09:20 +00:00
{
2023-02-18 14:14:42 +00:00
index: index + 1,
2023-02-18 14:09:20 +00:00
translation: String(row['value']).strip.freeze,
2023-02-18 14:32:15 +00:00
commentary: commentary,
2023-02-18 14:09:20 +00:00
}.freeze
end.freeze
end
2023-02-18 09:44:02 +00:00
##
# @return [Array<Array(String, String)>]
#
def examples(word_id)
word_id = Integer word_id
result = @db_conn.exec_params(
2023-02-05 01:18:22 +00:00
(
<<~SQL
SELECT
array_agg(foo.value ORDER BY foo.language_id DESC)::text[] AS values
FROM example_texts foo
INNER JOIN (
SELECT example_id, language_id, MIN(index) as min_index
FROM example_texts
WHERE
2023-02-11 05:54:34 +00:00
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
)
2023-02-05 01:18:22 +00:00
AND
(language_id = 5 OR language_id = 2)
GROUP BY example_id, language_id
) bar
ON
foo.example_id = bar.example_id AND
foo.language_id = bar.language_id AND
foo.index = bar.min_index
GROUP BY foo.example_id
SQL
),
2023-02-11 05:54:34 +00:00
[word_id],
2023-02-05 01:18:22 +00:00
).map { |row| row['values'] }
2023-02-18 09:44:02 +00:00
result.map { |pair| pair.map(&:freeze).freeze }.freeze
2023-02-04 23:43:57 +00:00
end
end