Server: add pod created date and score to the list controller

pull/10/head
Chocobozzz 2016-08-26 18:55:10 +02:00
parent cc599f34b7
commit 535724234a
3 changed files with 49 additions and 12 deletions

View 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
}

View 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)
}

View 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()
})