-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ParamUser table create, get, delete, setValue, getValue working with …
…tests
- Loading branch information
1 parent
e843975
commit 9f6e149
Showing
26 changed files
with
432 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
module.exports = { | ||
|
||
index: function(req, res, next){ | ||
gladys.paramUser.get(req.session.User) | ||
.then(function(paramUsers){ | ||
return res.json(paramUsers); | ||
}) | ||
.catch(next); | ||
}, | ||
|
||
create: function(req, res, next){ | ||
req.body.user = req.session.User.id; | ||
gladys.paramUser.setValue(req.body) | ||
.then(function(paramUser){ | ||
return res.status(201).json(paramUser); | ||
}) | ||
.catch(next); | ||
}, | ||
|
||
update: function(req, res, next){ | ||
req.body.user = req.session.User.id; | ||
req.body.name = req.params.name; | ||
gladys.paramUser.setValue(req.body) | ||
.then(function(paramUser){ | ||
return res.json(paramUser); | ||
}) | ||
.catch(next); | ||
}, | ||
|
||
delete: function(req, res, next){ | ||
req.body.user = req.session.User.id; | ||
req.body.name = req.params.name; | ||
gladys.paramUser.delete(req.body) | ||
.then(function(){ | ||
return res.json({success: true}); | ||
}) | ||
.catch(next); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports.clearCache = require('./paramUser.clearCache.js'); | ||
module.exports.delete = require('./paramUser.delete.js'); | ||
module.exports.getValue = require('./paramUser.getValue.js'); | ||
module.exports.get = require('./paramUser.get.js'); | ||
module.exports.setValue = require('./paramUser.setValue.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var shared = require('./paramUser.shared.js'); | ||
|
||
module.exports = function(){ | ||
shared.cache = {}; | ||
return Promise.resolve(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var queries = require('./paramUser.queries.js'); | ||
var shared = require('./paramUser.shared.js'); | ||
|
||
module.exports = function(paramUser){ | ||
|
||
// delete in database | ||
return gladys.utils.sql(queries.delete, [paramUser.name, paramUser.user]) | ||
.then(function(){ | ||
|
||
// then delete in cache | ||
if(shared.cache[paramUser.user] && shared.cache[paramUser.user][paramUser.name]){ | ||
delete shared.cache[paramUser.user][paramUser.name]; | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var queries = require('./paramUser.queries.js'); | ||
|
||
module.exports = function(user){ | ||
return gladys.utils.sql(queries.get, [user.id]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var queries = require('./paramUser.queries.js'); | ||
var shared = require('./paramUser.shared.js'); | ||
var setCacheValue = require('./paramUser.setCacheValue.js'); | ||
var Promise = require('bluebird'); | ||
|
||
|
||
module.exports = function(name, userId){ | ||
|
||
// get from cache | ||
if(shared.cache[userId] && shared.cache[userId][name]){ | ||
return shared.cache[userId][name]; | ||
} | ||
|
||
// if not in cache, get in DB | ||
return gladys.utils.sql(queries.getValue, [name, userId]) | ||
.then(function(rows){ | ||
if(rows.length){ | ||
setCacheValue(name, userId, rows[0].value); | ||
return rows[0].value; | ||
} else { | ||
return Promise.reject(new Error('NotFound')); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
module.exports = { | ||
getValue: 'SELECT * FROM paramuser WHERE name = ? AND user = ?;', | ||
deleteValue: 'DELETE FROM paramuser WHERE name = ? AND user = ?;', | ||
get: 'SELECT * FROM paramuser WHERE user = ?;' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var shared = require('./paramUser.shared.js'); | ||
|
||
module.exports = function(name, userId, value){ | ||
if(!shared.cache.hasOwnProperty(userId)){ | ||
shared.cache[userId] = {}; | ||
shared.cache[userId][name] = value; | ||
} else { | ||
shared.cache[userId][name] = value; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var queries = require('./paramUser.queries.js'); | ||
var shared = require('./paramUser.shared.js'); | ||
var setCacheValue = require('./paramUser.setCacheValue.js'); | ||
var Promise = require('bluebird'); | ||
|
||
module.exports = function(paramUser){ | ||
|
||
// we test if the param already exist | ||
return gladys.utils.sql(queries.getValue, [paramUser.name, paramUser.user]) | ||
.then(function(rows){ | ||
|
||
// if value exist, we update it | ||
if(rows.length){ | ||
|
||
return ParamUser.update({id: rows[0].id}, paramUser) | ||
.then(function(paramUsers){ | ||
return paramUsers[0]; | ||
}); | ||
} else { | ||
|
||
// if the value does not exist, we create it | ||
return ParamUser.create(paramUser); | ||
} | ||
}) | ||
.then(function(paramUser){ | ||
|
||
// we set the value of the cache | ||
setCacheValue(paramUser.name, paramUser.user, paramUser.value); | ||
return paramUser; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
module.exports = { | ||
cache: {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,6 @@ module.exports = { | |
value: { | ||
type:'string', | ||
required: true | ||
}, | ||
|
||
user: { | ||
model: 'User' | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Gladys Project | ||
* http://gladysproject.com | ||
* Software under licence Creative Commons 3.0 France | ||
* http://creativecommons.org/licenses/by-nc-sa/3.0/fr/ | ||
* You may not use this software for commercial purposes. | ||
* @author :: Pierre-Gilles Leymarie | ||
*/ | ||
|
||
|
||
module.exports = { | ||
|
||
attributes: { | ||
|
||
name: { | ||
type:'string', | ||
required: true | ||
}, | ||
|
||
value: { | ||
type:'string', | ||
required: true | ||
}, | ||
|
||
user: { | ||
model: 'User', | ||
required: true | ||
} | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "test", | ||
"value": "test", | ||
"user": 1 | ||
} | ||
] |
31 changes: 31 additions & 0 deletions
31
test/unit/api/controllers/ParamUser/paramuser.create.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var request = require('supertest'); | ||
var validateParamUser = require('../../validator/paramUserValidator.js'); | ||
|
||
describe('ParamUser', function() { | ||
|
||
describe('create', function() { | ||
|
||
it('should create a paramUser', function (done) { | ||
|
||
var param = { | ||
name:'THIS_IS_A_TEST', | ||
value: 'value' | ||
}; | ||
|
||
request(sails.hooks.http.app) | ||
.post('/paramuser?token=test') | ||
.send(param) | ||
.expect(201) | ||
.end(function(err, res) { | ||
if(err) return done(err); | ||
|
||
validateParamUser(res.body); | ||
done(); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
|
||
}); |
25 changes: 25 additions & 0 deletions
25
test/unit/api/controllers/ParamUser/paramuser.delete.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var request = require('supertest'); | ||
var validateParamUser = require('../../validator/paramUserValidator.js'); | ||
|
||
describe('ParamUser', function() { | ||
|
||
describe('delete', function() { | ||
|
||
it('should delete a paramUser', function (done) { | ||
|
||
request(sails.hooks.http.app) | ||
.delete('/param/test?token=test') | ||
.expect(200) | ||
.end(function(err, res) { | ||
if(err) return done(err); | ||
|
||
|
||
done(); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var request = require('supertest'); | ||
var validateParamUser = require('../../validator/paramUserValidator.js'); | ||
|
||
describe('ParamUser', function() { | ||
|
||
describe('get', function() { | ||
|
||
it('should get all paramUsers', function (done) { | ||
|
||
request(sails.hooks.http.app) | ||
.get('/paramuser?token=test') | ||
.expect(200) | ||
.end(function(err, res) { | ||
if(err) return done(err); | ||
|
||
res.body.should.be.instanceOf(Array); | ||
validateParamUser(res.body); | ||
done(); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
|
||
}); |
31 changes: 31 additions & 0 deletions
31
test/unit/api/controllers/ParamUser/paramuser.update.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var request = require('supertest'); | ||
var validateParamUser = require('../../validator/paramUserValidator.js'); | ||
|
||
describe('ParamUser', function() { | ||
|
||
describe('update', function() { | ||
|
||
it('should update a param', function (done) { | ||
|
||
var param = { | ||
value: 'value' | ||
}; | ||
|
||
request(sails.hooks.http.app) | ||
.patch('/paramuser/test?token=test') | ||
.send(param) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if(err) return done(err); | ||
|
||
validateParamUser(res.body); | ||
res.body.value.should.equal(param.value); | ||
done(); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
|
||
}); |
Oops, something went wrong.