mirror of
https://github.com/originalmk/skrytka-app.git
synced 2024-11-20 10:28:50 +00:00
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:
parent
00ae400e0d
commit
847513ebdd
8
app.js
8
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) {
|
||||
|
@ -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;
|
||||
SELECT id, name, image_path FROM fire_truck;
|
||||
|
Loading…
Reference in New Issue
Block a user