From 535724234aafd90c9eac17d9998f3f1c6c6c7615 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 26 Aug 2016 18:55:10 +0200 Subject: [PATCH] Server: add pod created date and score to the list controller --- server/controllers/api/v1/pods.js | 20 ++++++++++++++++---- server/models/pods.js | 28 ++++++++++++++++++++++------ server/tests/api/friends-basic.js | 13 +++++++++++-- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index 360575a0d..2bdfe0c92 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -17,7 +17,7 @@ const router = express.Router() const Pod = mongoose.model('Pod') const Video = mongoose.model('Video') -router.get('/', listPodsUrl) +router.get('/', listPods) router.post('/', validators.podsAdd, addPods) router.post('/makefriends', oAuth.authenticate, @@ -74,11 +74,11 @@ function addPods (req, res, next) { }) } -function listPodsUrl (req, res, next) { - Pod.listOnlyUrls(function (err, podsUrlList) { +function listPods (req, res, next) { + Pod.list(function (err, podsUrlList) { if (err) return next(err) - res.json(podsUrlList) + res.json(getFormatedPods(podsUrlList)) }) } @@ -142,3 +142,15 @@ function quitFriends (req, res, next) { res.type('json').status(204).end() }) } + +// --------------------------------------------------------------------------- + +function getFormatedPods (pods) { + const formatedPods = [] + + pods.forEach(function (pod) { + formatedPods.push(pod.toFormatedJSON()) + }) + + return formatedPods +} diff --git a/server/models/pods.js b/server/models/pods.js index bf43d7b25..59de2d60c 100644 --- a/server/models/pods.js +++ b/server/models/pods.js @@ -11,7 +11,11 @@ const constants = require('../initializers/constants') const PodSchema = mongoose.Schema({ url: String, publicKey: String, - score: { type: Number, max: constants.FRIEND_SCORE.MAX } + score: { type: Number, max: constants.FRIEND_SCORE.MAX }, + createdDate: { + type: Date, + default: Date.now + } }) // TODO: set options (TLD...) @@ -19,12 +23,15 @@ PodSchema.path('url').validate(validator.isURL) PodSchema.path('publicKey').required(true) PodSchema.path('score').validate(function (value) { return !isNaN(value) }) +PodSchema.methods = { + toFormatedJSON: toFormatedJSON +} + PodSchema.statics = { countAll: countAll, incrementScores: incrementScores, list: list, listAllIds: listAllIds, - listOnlyUrls: listOnlyUrls, listBadPods: listBadPods, load: load, loadByUrl: loadByUrl, @@ -46,6 +53,19 @@ PodSchema.pre('save', function (next) { const Pod = mongoose.model('Pod', PodSchema) +// ------------------------------ METHODS ------------------------------ + +function toFormatedJSON () { + const json = { + id: this._id, + url: this.url, + score: this.score, + createdDate: this.createdDate + } + + return json +} + // ------------------------------ Statics ------------------------------ function countAll (callback) { @@ -69,10 +89,6 @@ function listAllIds (callback) { }) } -function listOnlyUrls (callback) { - return this.find({}, { _id: 0, url: 1 }, callback) -} - function listBadPods (callback) { return this.find({ score: 0 }, callback) } diff --git a/server/tests/api/friends-basic.js b/server/tests/api/friends-basic.js index 2a6883acb..2f2f25e25 100644 --- a/server/tests/api/friends-basic.js +++ b/server/tests/api/friends-basic.js @@ -6,6 +6,7 @@ const expect = chai.expect const series = require('async/series') const loginUtils = require('../utils/login') +const miscsUtils = require('../utils/miscs') const podsUtils = require('../utils/pods') const serversUtils = require('../utils/servers') @@ -92,7 +93,11 @@ describe('Test basic friends', function () { const result = res.body expect(result).to.be.an('array') expect(result.length).to.equal(1) - expect(result[0].url).to.be.equal(servers[2].url) + + const pod = result[0] + expect(pod.url).to.equal(servers[2].url) + expect(pod.score).to.equal(20) + expect(miscsUtils.dateIsValid(pod.createdDate)).to.be.true next() }) @@ -105,7 +110,11 @@ describe('Test basic friends', function () { const result = res.body expect(result).to.be.an('array') expect(result.length).to.equal(1) - expect(result[0].url).to.be.equal(servers[1].url) + + const pod = result[0] + expect(pod.url).to.equal(servers[1].url) + expect(pod.score).to.equal(20) + expect(miscsUtils.dateIsValid(pod.createdDate)).to.be.true next() })