Server: add pod created date and score to the list controller
authorChocobozzz <florian.bigard@gmail.com>
Fri, 26 Aug 2016 16:55:10 +0000 (18:55 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Fri, 26 Aug 2016 16:55:10 +0000 (18:55 +0200)
server/controllers/api/v1/pods.js
server/models/pods.js
server/tests/api/friends-basic.js

index 360575a0d229694bc748bae25e90a66aca8bf691..2bdfe0c923b6c623cef73a8ae90facf4d9a8eab3 100644 (file)
@@ -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
+}
index bf43d7b25c14315672fe3c27031791d6763b4d57..59de2d60cb62dd07b17445f24386b1d29c3aba7c 100644 (file)
@@ -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)
 }
index 2a6883acb3c894f592dfc744abfbef445459940b..2f2f25e255b467b8165f6c5e9395ad41e9339985 100644 (file)
@@ -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()
         })