From b9a3e09ad5a7673f64556d1dba122ed4c4fac980 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 7 Mar 2016 11:33:59 +0100 Subject: Prepare folders structure for angular app --- server/models/pods.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 server/models/pods.js (limited to 'server/models/pods.js') diff --git a/server/models/pods.js b/server/models/pods.js new file mode 100644 index 000000000..57ed20292 --- /dev/null +++ b/server/models/pods.js @@ -0,0 +1,88 @@ +'use strict' + +var mongoose = require('mongoose') + +var constants = require('../initializers/constants') +var logger = require('../helpers/logger') + +// --------------------------------------------------------------------------- + +var podsSchema = mongoose.Schema({ + url: String, + publicKey: String, + score: { type: Number, max: constants.FRIEND_BASE_SCORE } +}) +var PodsDB = mongoose.model('pods', podsSchema) + +// --------------------------------------------------------------------------- + +var Pods = { + add: add, + count: count, + findByUrl: findByUrl, + findBadPods: findBadPods, + incrementScores: incrementScores, + list: list, + remove: remove, + removeAll: removeAll, + removeAllByIds: removeAllByIds +} + +// TODO: check if the pod is not already a friend +function add (data, callback) { + if (!callback) callback = function () {} + var params = { + url: data.url, + publicKey: data.publicKey, + score: constants.FRIEND_BASE_SCORE + } + + PodsDB.create(params, callback) +} + +function count (callback) { + return PodsDB.count(callback) +} + +function findBadPods (callback) { + PodsDB.find({ score: 0 }, callback) +} + +function findByUrl (url, callback) { + PodsDB.findOne({ url: url }, callback) +} + +function incrementScores (ids, value, callback) { + if (!callback) callback = function () {} + PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) +} + +function list (callback) { + PodsDB.find(function (err, pods_list) { + if (err) { + logger.error('Cannot get the list of the pods.') + return callback(err) + } + + return callback(null, pods_list) + }) +} + +function remove (url, callback) { + if (!callback) callback = function () {} + PodsDB.remove({ url: url }, callback) +} + +function removeAll (callback) { + if (!callback) callback = function () {} + PodsDB.remove(callback) +} + +function removeAllByIds (ids, callback) { + if (!callback) callback = function () {} + PodsDB.remove({ _id: { $in: ids } }, callback) +} + +// --------------------------------------------------------------------------- + +module.exports = Pods -- cgit v1.2.3