From 8a099c5f7b8c7fdb94dfc674aeb0e9a22772808e Mon Sep 17 00:00:00 2001 From: Matthew Berryman Date: Mon, 27 Feb 2017 21:23:32 +1100 Subject: [PATCH 1/2] feeds API for Zurich --- src/api/routes/feeds/index.js | 29 +++++++++++++++++++++++++++++ src/api/routes/feeds/model.js | 25 +++++++++++++++++++++++++ src/config.js | 3 +++ 3 files changed, 57 insertions(+) diff --git a/src/api/routes/feeds/index.js b/src/api/routes/feeds/index.js index 670251d..7a71e2c 100644 --- a/src/api/routes/feeds/index.js +++ b/src/api/routes/feeds/index.js @@ -66,5 +66,34 @@ export default ({ config, db, logger }) => { }) ); + export default ({ config, db, logger }) => { + let api = Router(); + + // Create a new Zurich record in the database + // TODO: What is mandatory around title / text, any rules AND/OR? + // TODO: Bulk endpoint for multiple POSTs + api.post('/zurich', validate({ + body: Joi.object().keys({ + post_id: Joi.number().integer().required(), + created_at: Joi.date().iso().required(), + title: Joi.string().allow(''), + text: Joi.string().allow(''), + image_url: Joi.string(), + city: Joi.string().valid(config.API_FEEDS_ZURICH_CITIES).required(), + disaster_type: Joi.string().valid(config.API_FEEDS_ZURICH_DISASTER_TYPES).required(), + location: Joi.object().required().keys({ + lat: Joi.number().min(-90).max(90).required(), + lng: Joi.number().min(-180).max(180).required() + }) + }) + }), + (req, res, next) => feeds(config, db, logger).addQlueReport(req.body) + .then((data) => res.json(data)) + .catch((err) => { + logger.error(err); + next(err); + }) + ); + return api; } diff --git a/src/api/routes/feeds/model.js b/src/api/routes/feeds/model.js index 72bce95..b73a67b 100644 --- a/src/api/routes/feeds/model.js +++ b/src/api/routes/feeds/model.js @@ -50,3 +50,28 @@ export default (config, db, logger) => ({ }) }) }); + +// Add a detik report +addZurichReport: (body) => new Promise((resolve, reject) => { + + // Setup query + let query = `INSERT INTO ${config.TABLE_FEEDS_ZURICH} + (city, contribution_id, created_at, disaster_type, title, text, url, image_url, the_geom) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, ST_SetSRID(ST_POINT($9, $10),4326))`; + + // Setup values + let values = [ body.city, body.contribution_id, body.created_at, body.disaster_type, body.title, body.text, + body.url, body.image_url, body.location.longitude, body.location.latitude ] + + // Execute + logger.debug(query, values); + db.oneOrNone(query, values).timeout(config.PGTIMEOUT) + .then(() => resolve({ contribution_id: body.contribution_id, created: true })) + .catch((err) => { + if (err.constraint === 'reports_contribution_id_key') + resolve({ contribution_id: body.contribution_id, created: false, message: `${body.contribution_id} already exists in reports table`}) + else + reject(err) + }) +}) +}); diff --git a/src/config.js b/src/config.js index f4d7eda..fe2eb13 100644 --- a/src/config.js +++ b/src/config.js @@ -5,6 +5,8 @@ export default { API_FEEDS_QLUE_CITIES: (process.env.API_FEEDS_QLUE_CITIES || 'jabodetabek,bandung,surabaya').split(','), API_FEEDS_QLUE_DISASTER_TYPES: (process.env.API_FEEDS_QLUE_DISASTER_TYPES || 'flood').split(','), API_FEEDS_DETIK_DISASTER_TYPES: (process.env.API_FEEDS_DEIK_DISASTER_TYPES || 'flood').split(','), + API_FEEDS_ZURICH_CITIES: (process.env.ZURICH_FEEDS_QLUE_CITIES || 'jabodetabek,bandung,surabaya').split(','), + API_FEEDS_ZURICH_DISASTER_TYPES: (process.env.ZURICH_FEEDS_QLUE_DISASTER_TYPES || 'flood,fire,car_accident,power_failure,other').split(','), API_REPORTS_TIME_WINDOW: process.env.API_REPORTS_TIME_WINDOW || 3600, API_REPORTS_TIME_WINDOW_MAX: process.env.API_REPORTS_TIME_WINDOW_MAX || 604800, // 1w API_REPORTS_LIMIT: process.env.API_REPORTS_LIMIT, @@ -51,6 +53,7 @@ export default { TABLE_FLOODGAUGE_REPORTS: process.env.TABLE_FLOODGAUGE_REPORTS || 'floodgauge.reports', TABLE_FEEDS_QLUE: process.env.TABLE_FEEDS_QLUE || 'qlue.reports', TABLE_FEEDS_DETIK: process.env.TABLE_FEEDS_DETIK || 'detik.reports', + TABLE_FEEDS_ZURICH: process.env.TABLE_FEEDS_DETIK || 'zurich.reports', TABLE_GRASP_CARDS: process.env.TABLE_GRASP_CARDS || 'grasp.cards', TABLE_GRASP_LOG: process.env.TABLE_GRASP_LOG || 'grasp.log', TABLE_GRASP_REPORTS: process.env.TABLE_GRASP_REPORTS || 'grasp.reports', From da2cc976dd10afca9fc777774a740a636a1c690d Mon Sep 17 00:00:00 2001 From: Matthew Berryman Date: Tue, 28 Feb 2017 10:17:11 +1100 Subject: [PATCH 2/2] post_id for zurich --- src/api/routes/feeds/model.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/routes/feeds/model.js b/src/api/routes/feeds/model.js index b73a67b..f4a9d40 100644 --- a/src/api/routes/feeds/model.js +++ b/src/api/routes/feeds/model.js @@ -56,20 +56,20 @@ addZurichReport: (body) => new Promise((resolve, reject) => { // Setup query let query = `INSERT INTO ${config.TABLE_FEEDS_ZURICH} - (city, contribution_id, created_at, disaster_type, title, text, url, image_url, the_geom) + (city, post_id, created_at, disaster_type, title, text, url, image_url, the_geom) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, ST_SetSRID(ST_POINT($9, $10),4326))`; // Setup values - let values = [ body.city, body.contribution_id, body.created_at, body.disaster_type, body.title, body.text, + let values = [ body.city, body.post_id, body.created_at, body.disaster_type, body.title, body.text, body.url, body.image_url, body.location.longitude, body.location.latitude ] // Execute logger.debug(query, values); db.oneOrNone(query, values).timeout(config.PGTIMEOUT) - .then(() => resolve({ contribution_id: body.contribution_id, created: true })) + .then(() => resolve({ contribution_id: body.post_id, created: true })) .catch((err) => { - if (err.constraint === 'reports_contribution_id_key') - resolve({ contribution_id: body.contribution_id, created: false, message: `${body.contribution_id} already exists in reports table`}) + if (err.constraint === 'reports_post_id_key') + resolve({ contribution_id: body.post_id, created: false, message: `${body.post_id} already exists in reports table`}) else reject(err) })