Merge pull request #369 from gsiou/api-search-user
API functionality - Get lists a user has subscribed to
This commit is contained in:
commit
304b9a0fd5
2 changed files with 48 additions and 0 deletions
|
@ -4,6 +4,7 @@ let db = require('../db');
|
||||||
let tools = require('../tools');
|
let tools = require('../tools');
|
||||||
let shortid = require('shortid');
|
let shortid = require('shortid');
|
||||||
let segments = require('./segments');
|
let segments = require('./segments');
|
||||||
|
let subscriptions = require('./subscriptions');
|
||||||
let _ = require('../translate')._;
|
let _ = require('../translate')._;
|
||||||
let tableHelpers = require('../table-helpers');
|
let tableHelpers = require('../table-helpers');
|
||||||
|
|
||||||
|
@ -64,6 +65,37 @@ module.exports.quicklist = callback => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.getListsWithEmail = (email, callback) => {
|
||||||
|
db.getConnection((err, connection) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.query('SELECT id, name FROM lists', (err, rows) => {
|
||||||
|
connection.release();
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let lists = (rows || []).map(tools.convertKeys);
|
||||||
|
const results = [];
|
||||||
|
lists.forEach((list, index, arr) => {
|
||||||
|
subscriptions.getByEmail(list.id, email, (err, sub) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
if (sub) {
|
||||||
|
results.push(list.id);
|
||||||
|
}
|
||||||
|
if (index === arr.length - 1) {
|
||||||
|
return callback(null, lists.filter(list => results.includes(list.id)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.getByCid = (cid, callback) => {
|
module.exports.getByCid = (cid, callback) => {
|
||||||
resolveCid(cid, (err, id) => {
|
resolveCid(cid, (err, id) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
|
@ -348,6 +348,22 @@ router.get('/subscriptions/:listId', (req, res) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/lists/:email', (req, res) => {
|
||||||
|
lists.getListsWithEmail(req.params.email, (err, lists) => {
|
||||||
|
if (err) {
|
||||||
|
res.status(500);
|
||||||
|
return res.json({
|
||||||
|
error: err.message || err,
|
||||||
|
data: []
|
||||||
|
});
|
||||||
|
}
|
||||||
|
res.status(200);
|
||||||
|
res.json({
|
||||||
|
data: lists
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
router.post('/field/:listId', (req, res) => {
|
router.post('/field/:listId', (req, res) => {
|
||||||
let input = {};
|
let input = {};
|
||||||
Object.keys(req.body).forEach(key => {
|
Object.keys(req.body).forEach(key => {
|
||||||
|
|
Loading…
Reference in a new issue