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

This commit is contained in:
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 Pod = mongoose.model('Pod')
const Video = mongoose.model('Video') const Video = mongoose.model('Video')
router.get('/', listPodsUrl) router.get('/', listPods)
router.post('/', validators.podsAdd, addPods) router.post('/', validators.podsAdd, addPods)
router.post('/makefriends', router.post('/makefriends',
oAuth.authenticate, oAuth.authenticate,
@ -74,11 +74,11 @@ function addPods (req, res, next) {
}) })
} }
function listPodsUrl (req, res, next) { function listPods (req, res, next) {
Pod.listOnlyUrls(function (err, podsUrlList) { Pod.list(function (err, podsUrlList) {
if (err) return next(err) 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() 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({ const PodSchema = mongoose.Schema({
url: String, url: String,
publicKey: 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...) // TODO: set options (TLD...)
@ -19,12 +23,15 @@ PodSchema.path('url').validate(validator.isURL)
PodSchema.path('publicKey').required(true) PodSchema.path('publicKey').required(true)
PodSchema.path('score').validate(function (value) { return !isNaN(value) }) PodSchema.path('score').validate(function (value) { return !isNaN(value) })
PodSchema.methods = {
toFormatedJSON: toFormatedJSON
}
PodSchema.statics = { PodSchema.statics = {
countAll: countAll, countAll: countAll,
incrementScores: incrementScores, incrementScores: incrementScores,
list: list, list: list,
listAllIds: listAllIds, listAllIds: listAllIds,
listOnlyUrls: listOnlyUrls,
listBadPods: listBadPods, listBadPods: listBadPods,
load: load, load: load,
loadByUrl: loadByUrl, loadByUrl: loadByUrl,
@ -46,6 +53,19 @@ PodSchema.pre('save', function (next) {
const Pod = mongoose.model('Pod', PodSchema) 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 ------------------------------ // ------------------------------ Statics ------------------------------
function countAll (callback) { function countAll (callback) {
@ -69,10 +89,6 @@ function listAllIds (callback) {
}) })
} }
function listOnlyUrls (callback) {
return this.find({}, { _id: 0, url: 1 }, callback)
}
function listBadPods (callback) { function listBadPods (callback) {
return this.find({ score: 0 }, callback) return this.find({ score: 0 }, callback)
} }

View File

@ -6,6 +6,7 @@ const expect = chai.expect
const series = require('async/series') const series = require('async/series')
const loginUtils = require('../utils/login') const loginUtils = require('../utils/login')
const miscsUtils = require('../utils/miscs')
const podsUtils = require('../utils/pods') const podsUtils = require('../utils/pods')
const serversUtils = require('../utils/servers') const serversUtils = require('../utils/servers')
@ -92,7 +93,11 @@ describe('Test basic friends', function () {
const result = res.body const result = res.body
expect(result).to.be.an('array') expect(result).to.be.an('array')
expect(result.length).to.equal(1) 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() next()
}) })
@ -105,7 +110,11 @@ describe('Test basic friends', function () {
const result = res.body const result = res.body
expect(result).to.be.an('array') expect(result).to.be.an('array')
expect(result.length).to.equal(1) 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() next()
}) })