Fix year handling

This commit is contained in:
Roberto Alsina 2023-06-04 19:08:36 -03:00
parent 9263815727
commit 6a357fe776
2 changed files with 14 additions and 7 deletions

View File

@ -65,7 +65,7 @@ def handle(req):
{
p: prefijo del nombre,
g: genero del nombre,
a: año de nacimiento
y: año de nacimiento
}
"""

View File

@ -108,6 +108,16 @@ class Handler
end
def run(request : HTTP::Request)
# Try to find most popular names based on a prefix, year and gender.
#
# Request body is JSON in this form:
#
# {
# p: prefijo del nombre,
# g: genero del nombre,
# y: año de nacimiento
# }
unless (body = request.body).nil?
query = Hash(String, String).from_json(body)
else
@ -120,7 +130,7 @@ class Handler
# Each one either a valid string or nil
prefijo = query.fetch("p", "")
genero = query.fetch("g", "")
año = query.fetch("a", "")
año = query.fetch("y", "")
if !prefijo.empty?
prefijo = normalize_name(prefijo)
@ -132,10 +142,7 @@ class Handler
genero = nil
end
if año.empty?
año = nil
# TODO: check for valid integer
end
año = año.to_i?
if prefijo.nil? && año.nil?
# Global totals
@ -166,7 +173,7 @@ class Handler
LIMIT 50
)
else
# We have all the filters
# We have both
sql = %(
SELECT contador, nombre
FROM nombres