From 847513ebdd61d88d051c6f4a8070dac4e3b2d021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Krzy=C5=BCanowski?= Date: Mon, 27 Feb 2023 16:13:56 +0100 Subject: [PATCH] Changed the way osp units can be searched Now parameter name is not locality-prefix, but locality. Provided string is searched at any place of OSP name or locality, not only at the beginning. --- app.js | 8 ++++---- skrytka_create_utils.sql | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 0c52da1..c2eb199 100644 --- a/app.js +++ b/app.js @@ -84,13 +84,13 @@ app.get('/ping', (req, res) => { * e.g. https://skrytka.app/osp-units?prefix=Gda * Returns 3 matching results sorted by locality, name.*/ app.get('/osp-units', async (req, res) => { - const localityPrefix = req.query['locality-prefix']; + const locality = req.query['locality']; - if(!localityPrefix) { + if(!locality) { res.status(400); res.json({ queryErrors: { - 'locality-prefix': ['Nie podano parametru!'] + 'locality': ['Nie podano parametru!'] }, otherErrors: [] }); @@ -101,7 +101,7 @@ app.get('/osp-units', async (req, res) => { // Also are these database constraints reasonable? try { - const unitsList = await db.any('SELECT id AS "ID", name, locality FROM get_units_list($1)', [localityPrefix]); + const unitsList = await db.any('SELECT id AS "ID", name, locality FROM get_units_list($1)', [locality]); res.status(200); res.json(unitsList); } catch(error) { diff --git a/skrytka_create_utils.sql b/skrytka_create_utils.sql index 32c5856..2e8c9b1 100644 --- a/skrytka_create_utils.sql +++ b/skrytka_create_utils.sql @@ -1,12 +1,15 @@ DROP VIEW IF EXISTS units_list; CREATE VIEW units_list AS SELECT id, name, locality FROM osp_unit; - + DROP FUNCTION IF EXISTS get_units_list; CREATE FUNCTION get_units_list(prefix VARCHAR) RETURNS TABLE (id INTEGER, name VARCHAR, locality VARCHAR) AS $func$ - SELECT * FROM units_list WHERE locality LIKE prefix || '%' OR name LIKE prefix || '%' ORDER BY locality, name LIMIT 3; + SELECT * FROM units_list + WHERE LOWER(locality) LIKE LOWER('%' || prefix || '%') + OR LOWER(name) LIKE LOWER('%' || prefix || '%') + ORDER BY locality, name LIMIT 3; $func$ LANGUAGE SQL; @@ -62,4 +65,4 @@ LANGUAGE SQL; DROP VIEW IF EXISTS trucks_list; CREATE VIEW trucks_list AS - SELECT id, name, image_path FROM fire_truck; \ No newline at end of file + SELECT id, name, image_path FROM fire_truck;