Add autofill backend
This commit is contained in:
parent
35554e37ff
commit
aafb81a17b
@ -1,6 +1,7 @@
|
||||
import bodyParser from 'body-parser';
|
||||
import express from 'express';
|
||||
|
||||
import autofillController from './controllers/autofillController';
|
||||
import * as editHistoryController from './controllers/editHistoryController';
|
||||
import buildingsRouter from './routes/buildingsRouter';
|
||||
import extractsRouter from './routes/extractsRouter';
|
||||
@ -19,6 +20,7 @@ server.use('/users', usersRouter);
|
||||
server.use('/extracts', extractsRouter);
|
||||
|
||||
server.get('/history', editHistoryController.getGlobalEditHistory);
|
||||
server.get('/autofill', autofillController.getAutofillOptions);
|
||||
|
||||
// POST user auth
|
||||
server.post('/login', function (req, res) {
|
||||
|
14
app/src/api/controllers/autofillController.ts
Normal file
14
app/src/api/controllers/autofillController.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import asyncController from '../routes/asyncController';
|
||||
import * as autofillService from '../services/autofill';
|
||||
|
||||
const getAutofillOptions = asyncController(async (req, res) => {
|
||||
const { field_name, field_value } = req.query;
|
||||
|
||||
const options = await autofillService.getAutofillOptions(field_name, field_value);
|
||||
|
||||
res.send(options);
|
||||
});
|
||||
|
||||
export default {
|
||||
getAutofillOptions
|
||||
};
|
25
app/src/api/services/autofill.ts
Normal file
25
app/src/api/services/autofill.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import db from '../../db';
|
||||
|
||||
const autofillFunctionMap = {
|
||||
current_landuse_class: getLanduseClassOptions
|
||||
};
|
||||
|
||||
function getLanduseClassOptions(value: string) {
|
||||
return db.manyOrNone(`
|
||||
SELECT landuse_id AS id, description as value, similarity(description, $1) AS similarity
|
||||
FROM reference_tables.building_landuse_class
|
||||
WHERE description % $1
|
||||
ORDER BY similarity DESC, description
|
||||
`, [value]
|
||||
);
|
||||
}
|
||||
|
||||
export function getAutofillOptions(fieldName: string, fieldValue: any) {
|
||||
const optionsFn = autofillFunctionMap[fieldName];
|
||||
|
||||
if (optionsFn == undefined) {
|
||||
throw new Error(`Autofill options not available for field '${fieldName}'`);
|
||||
}
|
||||
|
||||
return optionsFn(fieldValue);
|
||||
}
|
Loading…
Reference in New Issue
Block a user