/** * * User: Martin Donnelly * Date: 2016-07-05 * Time: 15:01 * */ var log4js = require('log4js'); var logger = log4js.getLogger(); //var nano = require('nano')('http://localhost:5984'); var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984'); var db_name = 'recipes'; var dbCouch = nano.use(db_name); var busEmitter = null; var keeperService; module.exports = exports = keeperService = function setBus(cfg) { 'use strict'; busEmitter = cfg; console.log(busEmitter); var core = {}; core.listGET = function(args, res) { logger.debug('list..'); dbCouch.view('titles', 'titles', function(err, body) { if (!err) { var outJSON = []; body.rows.forEach(function(doc) { outJSON.push({id: doc.id, title: doc.value}); }); //Logger.debug(util.inspect(body)); res.status(200).json({list: outJSON}); } else { res.status(500).json({}); } }); }; core.entryIdGET = function(args, res) { logger.debug('entry..'); logger.debug(args.id); dbCouch.get(args.id, function(err, body) { if (!err) { var outJSON = {}; outJSON._id = body._id; outJSON._rev = body._rev; outJSON.title = body.title; outJSON.reduced = body.reduced; outJSON.url = body.url; outJSON.tags = body.tags || {solid: '', list: []}; res.status(200).json(outJSON); } else { logger.error(err); res.status(500).json({}); } }); }; core.tagsGET = function(args, res) { logger.debug('tag list..'); logger.debug(args.id); dbCouch.view('taglist', 'taglist', function(err, body) { if (!err) { logger.debug(body); var outJSON = []; body.rows.forEach(function(doc) { logger.info(doc.value.taglist); if (doc.value[0] === args.id) { outJSON = doc.value.taglist.sort(); } }); res.status(200).json({list: outJSON}); } else { logger.error(err); res.status(500).json({}); } }); }; core.tagsPOST = function(args, res) { console.log(args); logger.info('regetting:' + args._id); dbCouch.get(args._id, function(err, body) { if (!err) { var obj = {}; obj.url = body.url; obj.html = body.html; obj.reduced = body.reduced; obj.title = body.title; obj.tags = args.tags; logger.info('Updating...'); busEmitter.emit('updateBookmarkData', obj, body._id, body._rev, res); var outJSON = {}; outJSON._id = body._id; outJSON._rev = body._rev; outJSON.title = body.title; outJSON.reduced = body.reduced; outJSON.url = body.url; outJSON.tags = args.tags; //Logger.debug(util.inspect(body)); res.status(200).json(outJSON); } else { logger.error(err); res.status(500).json({}); } }); }; core.tagsIDGET = function(args, res) { logger.debug('entry..'); logger.debug(args.id); dbCouch.view('getTagByKey', 'getTagByKey', function(err, body) { if (!err) { var outJSON = []; body.rows.forEach(function(doc) { if (doc.value[0] == args.id) { outJSON.push({id: doc.id, title: doc.value[1]}); } }); res.status(200).json({list: outJSON}); } else { logger.error(err); res.status(500).json({}); } }); }; core.addPOST = function(args, res) { var url; logger.debug('add entry..'); if (args.hasOwnProperty('url')) { url = JSON.parse(args.url.toString()); logger.debug(url); busEmitter.emit('getBookmark', args); } else { logger.error('No data block!'); } res.status(200).json({adding: url}); }; return core; };