-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfootball.js
74 lines (67 loc) · 2.46 KB
/
football.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict'
const footballDb = require('./../db/footballDb')
/**
* This API is similar to the football.js but with
* node.js callback convention instead of Promises.
*/
module.exports = {
getLeaguesIdTable, // binds to /football/leagues/:id/table
getLeagues, // binds to /football/leagues
index, // binds to /football/
indexId // binds to /football/:id
}
/**
* Every action parameter (e.g. id) taking part of method's name (e.g. _id_)
* is bound to the corresponding argument of req.params (e.g. req.params.id).
* In this case this function is useless and we could simply bound
* property 'getLeaguesIdTable' to method `footballDb.getLeaguesIdTable`.
*/
function getLeaguesIdTable(id, cb){
footballDb
.getLeaguesIdTable(id)
.then(data => cb(null, data))
.catch(err => cb(err))
}
/**
* Every action parameter that is NOT part of the method's name
* must be req, res or next.
* NOTE that if you receive res, then you are responsible for sending the response.
*/
function getLeagues(req, cb) {
const name = req.query.name? req.query.name.toLowerCase() : ''
footballDb
.getLeagues()
.then(leagues => leagues
.filter(l => !name || l.caption.toLowerCase().indexOf(name) >= 0)
.map(addLeaguePath))
.then(data => cb(null, data))
.catch(err => cb(err))
function addLeaguePath(league) {
league.leagueHref = '/football/leagues/' + league.id + '/table'
return league
}
}
/**
* Whenever an action receives the `res` parameter, the connect-controller
* gets out of the way and delegates on that action the responsibility of
* sending the response.
* So whenever you want to do something different from the default behavior
* you just have to append `res` to your parameters.
*/
function index(res) {
/**
* If this controller is loaded with an options object set with
* the property `redirectOnStringResult` then this is equivalent
* to removing the `res` parameter and just return the destination
* string path '/football/leagues'.
*/
res.redirect('/football/leagues')
}
/**
* If this controller is loaded with an options object set with the property
* `redirectOnStringResult` then this action method redirects to
* `/football/leagues/:id/table`.
*/
function indexId(id) {
return '/football/leagues/' + id + '/table'
}