Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
yotta
/
pictogram
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
60
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
eadd39f9
authored
Dec 15, 2016
by
Arturo Montejo Ráez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
added new model and controller for licenses
parent
0816363f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
173 additions
and
0 deletions
sails/src/api/controllers/LicenseController.js
sails/src/api/models/License.js
sails/src/api/controllers/LicenseController.js
0 → 100644
View file @
eadd39f9
/* 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
();
});
}
};
sails/src/api/models/License.js
0 → 100644
View file @
eadd39f9
/**
* 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
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment