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.
This commit is contained in:
Maciej Krzyżanowski 2023-02-27 16:13:56 +01:00
parent 00ae400e0d
commit 847513ebdd
2 changed files with 10 additions and 7 deletions

8
app.js
View File

@ -84,13 +84,13 @@ app.get('/ping', (req, res) => {
* e.g. https://skrytka.app/osp-units?prefix=Gda * e.g. https://skrytka.app/osp-units?prefix=Gda
* Returns 3 matching results sorted by locality, name.*/ * Returns 3 matching results sorted by locality, name.*/
app.get('/osp-units', async (req, res) => { 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.status(400);
res.json({ res.json({
queryErrors: { queryErrors: {
'locality-prefix': ['Nie podano parametru!'] 'locality': ['Nie podano parametru!']
}, },
otherErrors: [] otherErrors: []
}); });
@ -101,7 +101,7 @@ app.get('/osp-units', async (req, res) => {
// Also are these database constraints reasonable? // Also are these database constraints reasonable?
try { 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.status(200);
res.json(unitsList); res.json(unitsList);
} catch(error) { } catch(error) {

View File

@ -1,12 +1,15 @@
DROP VIEW IF EXISTS units_list; DROP VIEW IF EXISTS units_list;
CREATE VIEW units_list AS CREATE VIEW units_list AS
SELECT id, name, locality FROM osp_unit; SELECT id, name, locality FROM osp_unit;
DROP FUNCTION IF EXISTS get_units_list; DROP FUNCTION IF EXISTS get_units_list;
CREATE FUNCTION get_units_list(prefix VARCHAR) RETURNS TABLE (id INTEGER, name VARCHAR, locality VARCHAR) CREATE FUNCTION get_units_list(prefix VARCHAR) RETURNS TABLE (id INTEGER, name VARCHAR, locality VARCHAR)
AS AS
$func$ $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$ $func$
LANGUAGE SQL; LANGUAGE SQL;
@ -62,4 +65,4 @@ LANGUAGE SQL;
DROP VIEW IF EXISTS trucks_list; DROP VIEW IF EXISTS trucks_list;
CREATE VIEW trucks_list AS CREATE VIEW trucks_list AS
SELECT id, name, image_path FROM fire_truck; SELECT id, name, image_path FROM fire_truck;