added new model and controller for licenses

parent 0816363f
/* global Instruction, Method */
/**
* LicenseController manages the requests related to the License model.
* Read it's documentation for further information.
* @type {Object}
*/
module.exports = {
/**
* Create a new Instruction, which is associated to the given method
* @param {request} req
* {
* duration (number of months)
* }
* @param {response} res
* {
* code (instructionId)
* method
* name
* objective
* }
*/
create: function (req, res) {
var params = req.allParams();
function get_new_random_license (callback) {
function random_license () {
var length = 16;
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
var license;
var found = true;
var maxtries = 10;
async.doWhilst(
function (cb) {
license = random_license();
License.findOne({number: license})
.then((l) => {
if (!l)
found = false;
cb();
})
.catch((err) => {
found = false;
cb();
});
},
function () {
return found;
},
function () {
callback(license);
}
);
}
if (!params.duration || params.duration < 0)
return res.badRequest();
get_new_random_license(function (license) {
License.create({
number: license,
duration: params.duration
})
.then((l) => {
if (l) {
delete l.id;
return res.ok(l);
}
return res.badRequest();
})
.catch((err) => {
return res.serverError(err);
});
});
},
/**
* Activate an existing License
* @param {request} req (with license number as parameter ID)
* {
* id_stu: id of the student
* }
* @param {response} res
*/
activate: function (req, res) {
var params = req.allParams();
if (!params.number)
return res.badRequest();
License.findOne({ number: params.number })
.then((l) => {
if (!l)
return res.badRequest();
if (l.activation_ts)
return res.badRequest("The license is already active");
l.activation_ts = new Date();
Student.find(params.id_stu)
.then((s) => {
if (!s)
return res.badRequest("Student not found");
l.student = params.id_stu;
l.save((err) => {
if (err)
return res.serverError();
return res.ok();
});
})
.catch(() => {
return res.serverError();
});
})
.catch(() => {
return res.serverError();
});
}
};
/**
* License.js
*
* @description :: TODO: Write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
tableName : 'license',
migrate : 'safe',
schema : true,
autoPK : false,
autoCreatedAt : false,
autoUpdatedAt : false,
attributes: {
id: {
type: "integer",
autoIncrement: true,
primaryKey: true,
unique: true
},
student: { // FK de Student. 1 a N
columnName: "id_stu",
type: "integer",
model: "Student"
},
creation_ts: {
columnName: "creation_ts",
type: "datetime"
},
activation_ts: {
columnName: "activation_ts",
type: "datetime"
},
duration: {
columnName: "duration",
type: "integer"
},
number: {
type: "string",
columnName: "number",
size: 16,
unique: true
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment