-
Notifications
You must be signed in to change notification settings - Fork 4
/
apis.js
170 lines (152 loc) · 5 KB
/
apis.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// # Site Routes
// --------------------------------------
// contains all the routes of the site including pages, and rest api services.
//
// 1. Public Routes
// 2. Rest Routes
//
// requires
// * app
// * config
var app = module.parent.exports.app,
config = module.parent.exports.config,
sequelize = exports.sequelize = module.parent.exports.sequelize,
epilogue = module.parent.exports.epilogue,
logger = module.parent.exports.logger;
// ## Models
var Client = require('../models/clients.js'),
Provider = require('../models/providers.js'),
ClientProviders = require('../models/clients_providers.js');
// Setup: Belongs-To-Many associations
// http://docs.sequelizejs.com/en/latest/docs/associations/#belongs-to-many-associations
Client.belongsToMany(Provider, {through: 'ClientProviders'});
Provider.belongsToMany(Client, {through: 'ClientProviders'});
// ## Public Rest
// --------------------------------------
// Initialize epilogue
epilogue.initialize({
app : app,
sequelize : sequelize
});
// Create REST resources
/**
* @api {get} /clients Request Clients List
* @apiName findAll
* @apiGroup Clients
*
* @apiExample {curl} Example usage:
* curl -i http://localhost:3000/api/v1/clients
*
* @apiSuccess {Array} List of clients objects
*/
// https://github.com/sequelize/sequelize/issues/1869
var clientResource = epilogue.resource({
model: Client,
endpoints: ['/api/v1/clients', '/api/v1/clients/:id'],
associations: true,
search: [
{operator: '$like', param: 'sname', attributes: [ 'name' ]},
{operator: '$like', param: 'sphone', attributes: [ 'phone' ]},
{operator: '$like', param: 'semail', attributes: [ 'email' ]}
]
});
/**
* @api {get} /providers Request Providers List
* @apiName findAll
* @apiGroup Providers
*
* @apiExample {curl} Example usage:
* curl -i http://localhost:3000/api/v1/providers
*
* @apiSuccess {Array} List of providers objects
*/
var providerResource = epilogue.resource({
model: Provider,
endpoints: ['/api/v1/providers', '/api/v1/providers/:id']
});
/**
* @api {get} /clientproviders Request ClientProviders List
* @apiName findAll
* @apiGroup RelationClientProviders
*
* @apiExample {curl} Example usage:
* curl -i http://localhost:3000/api/v1/clientproviders
*
* @apiSuccess {Array} List of relationships between client and providers
*/
var clientproviderResource = epilogue.resource({
model: ClientProviders,
endpoints: ['/api/v1/clientproviders', '/api/v1/clientproviders/:id']
});
/**
* @api {get} /add-providers-to-clients/:clientId/:csvList Save Client Providers Relationships
* @apiName saveClientProvidersRelationships
* @apiGroup RelationClientProviders
*
* @apiExample {curl} Example usage:
* curl -i http://localhost:3000/api/v1/add-providers-to-clients/1/1,2
*
* @apiSuccess {Object} Returns the status of the operationD
*/
app.get('/api/v1/add-providers-to-clients/:clientId/:csvList', function(req, res){
// find the client by id
// start finding the providers
// create record in relationship table
// TODO: add posible errored statuses
var providersList = [];
providersList = req.params.csvList.split(',');
providersList.filter(function(item){
return !isNaN(parseFloat(item));
}).map(function(num){ return parseFloat(num); });
console.log(providersList);
// Search Providers
Provider.findAll({ where: { id: providersList } }).then(function(providers) {
// providers will be an array of Providers having the ids
console.log("Providers found:", providers.length);
// Get current client
Client.findOne({ where: { id: req.params.clientId } }).then(function(client){
console.log("Client found by id :", req.params.clientId, client);
// Update relationships
client.setProviders(providers).then(function(result){
console.log("Relationships udpated!");
res.json({ status: 'ok' });
});
})
});
});
/**
* @api {get} /add-providers-to-clients/:clientId/:csvList Save Client Providers Relationships
* @apiName saveClientProvidersRelationships
* @apiGroup RelationClientProviders
*
* @apiExample {curl} Example usage:
* curl -i http://localhost:3000/api/v1/count/clients
*
* @apiSuccess {Object} Returns the status of the operationD
*/
app.get('/api/v1/count/clients', function(req, res){
Client.count().then(function(result){
res.json({ "total": result });
});
});
if (config.db.sync && config.db.sync === "enabled") {
sequelize.sync({force:true});
}
// CORS Interceptors
if (config.cors && config.cors === "enabled") {
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.options('/api/v1/*', function(req, res){
res.end();
});
}
// ## Public Routes
// --------------------------------------
// ### Home Page
app.get('/', function (req, res) {
res.render('index', { title: 'Sequelize Express Angular App', section: 'Home' });
});