Commit 80cf3966 authored by Matheus Horstmann's avatar Matheus Horstmann 🐴

Merge branch '24-automigrate-substituir-post-por-put' into 'development'

Resolve "Automigrate: Substituir POST por PUT"

Closes #24

See merge request !17
parents 557463bf 47c69c96
......@@ -12,6 +12,8 @@ var lbTables = models.models;
var LOG_CREATE = false;
// console.log(app.models.Curso.scopes.disciplinas.modelFrom);
ds.automigrate(lbTables, function(err) {
if (err) throw err;
async.waterfall([
......@@ -26,7 +28,10 @@ ds.automigrate(lbTables, function(err) {
loadModelFromOldDB(old_db.get_departamentos, app.models.Departamento, LOG_CREATE),
loadModelFromOldDB(old_db.get_setores, app.models.Setor, LOG_CREATE),
loadModelFromOldDB(old_db.get_disciplinas, app.models.Disciplina, LOG_CREATE),
loadModelFromOldDB(old_db.get_cursos, app.models.Curso, LOG_CREATE)
loadModelFromOldDB(old_db.get_equivalenciasDisciplinas, app.models.Equivalenciadisciplina, LOG_CREATE),
loadModelFromOldDB(old_db.get_cursos, app.models.Curso, LOG_CREATE),
loadRelationsFromOldDB(old_db.get_cursoDisciplina, app.models.Curso, "disciplinas", LOG_CREATE),
],
function(err) {
if (err) throw err;
......@@ -35,20 +40,63 @@ ds.automigrate(lbTables, function(err) {
});
// Returns a function that populate a DB model with data loaded from
// old database (see migrate-old-schema.js and load-old-dabase.js codes)
function loadModelFromOldDB(oldDataAccess, model, create_log){
return function(cb){
oldDataAccess.then(data => {
async.each(data, function(instance, callback) {
async.each(data, (instance, callback) => {
if(create_log) console.log('Trying to insert:', instance);
model.create(instance, function(err, instanceCreated) {
model.upsert(instance, (err, instanceCreated) => {
callback(err);
if(create_log) console.log('Created:', instanceCreated);
});
}, function(err) {
}, (err) => {
if (err) throw err;
cb(err);
});
});
}
}
function addRelationBetweenModels(model, modelRelationName,
relationInstance, cb, create_log){
var query = {where: {}};
query.where[relationInstance.pkey_name] = relationInstance.pkeys[0];
model.findOne(query, (err,data) => {
if (err) throw err;
data[modelRelationName].add(relationInstance.pkeys[1], (err,data) => {
cb(err,data);
});
});
}
// Returns a function that populate a relation DB model with data loaded from
// old database (see migrate-old-schema.js and load-old-dabase.js codes)
function loadRelationsFromOldDB(oldDataAccess, model, modelRelationName, create_log){
return function(cb){
oldDataAccess.then(data => {
async.each(data, (instance, callback) => {
if(create_log){
console.log('Trying to insert relation on ',
modelRelationName,':', instance
);
}
addRelationBetweenModels(
model,
modelRelationName,
instance,
(err, instanceCreated) => {
callback(err);
if(create_log) console.log('Created:', instanceCreated);
});
}, (err) => {
if (err) throw err;
cb(err);
});
......@@ -56,13 +104,14 @@ function loadModelFromOldDB(oldDataAccess, model, create_log){
}
}
function criaRecursodesala(cb){
var recursos = [
{descricao: 'recurso1'},
{descricao: 'recurso2'}
];
async.each(recursos, function(recurso, callback) {
app.models.Recursodesala.create(recurso, function(err, model) {
app.models.Recursodesala.upsert(recurso, function(err, model) {
callback(err);
if(LOG_CREATE) console.log('Created:', model);
});
......@@ -78,7 +127,7 @@ function criaTipodesala(cb){
{nome: 'tipo2'}
];
async.each(tipos, function(tipo, callback) {
app.models.Tipodesala.create(tipo, function(err, model) {
app.models.Tipodesala.upsert(tipo, function(err, model) {
callback(err);
if(LOG_CREATE) console.log('Created:', model);
});
......@@ -90,7 +139,7 @@ function criaTipodesala(cb){
function criaUser(cb){
app.models.Secretario.create([
app.models.Secretario.replaceOrCreate([
{username: 'admin', email: 'admin@admin.com', password: '123mudar'},
{username: 'secretario1', email: 'secretario1@secretario.com', password: '123mudar'},
{username: 'secretario2', email: 'secretario2@secretario.com', password: '123mudar'}
......@@ -127,13 +176,13 @@ function criaEquivalencia(cb){
function criaDisciplina(cb){
var disciplinas = [
{"codigo": "d1","carga_horaria": 0,"duracao": "semestral","modalidade": "presencial"},
{"codigo": "d2","carga_horaria": 1,"duracao": "semestral","modalidade": "distância"},
{"codigo": "d3","carga_horaria": 2,"duracao": "anual","modalidade": "presencial"}
{"codigo": "d1", "nome": "disciplina1", "carga_horaria": 0,"duracao": "semestral","modalidade": "presencial"},
{"codigo": "d2", "nome": "disciplina2", "carga_horaria": 1,"duracao": "semestral","modalidade": "distância"},
{"codigo": "d3", "nome": "disciplina3", "carga_horaria": 2,"duracao": "anual","modalidade": "presencial"}
];
async.each(disciplinas, function(disciplina, callback) {
app.models.Disciplina.create(disciplina, function(err, model) {
app.models.Disciplina.upsert(disciplina, function(err, model) {
if (err) throw err;
callback(err);
if(LOG_CREATE) console.log('Created:', model);
......@@ -143,5 +192,4 @@ function criaDisciplina(cb){
cb(err);
});
}
var path = require("path");
var fs = require("fs");
var LOG_QUERIES = false;
const { Client } = require('pg')
......@@ -16,6 +17,7 @@ function load_data_from_db(columns, table_name){
});
client.connect()
return new Promise( (resolve, reject) => {
if(LOG_QUERIES) console.log('SELECT '+columns+' FROM '+table_name);
client.query('SELECT '+columns+' FROM '+table_name, (err, res) => {
if (!err) {
resolve(res);
......
......@@ -26,7 +26,8 @@
// On automigrate, this will be used to access data with a promisse.
// For instance, get_blocos.then ...
var connectionString = 'postgresql://bhm15:pass@localhost:5432/ensalamento';
var connectionString = 'postgresql://user:password@localhost:5432/ensalamento';
var rooms = [
{
......@@ -67,6 +68,9 @@ var rooms = [
"salas"
];
var blocks = [
{
"name": "nome",
......@@ -97,6 +101,9 @@ var blocks = [
"blocos"
];
var professors = [
{
"name": "nome",
......@@ -121,6 +128,9 @@ var professors = [
"professores"
];
var departments = [
{
"name": "nome",
......@@ -145,6 +155,8 @@ var departments = [
];
var sectors = [
{
"email": "email",
......@@ -159,6 +171,9 @@ var sectors = [
"setores" //SAVE FILE HAS
];
var subjects = [
{
"code": "codigo",
......@@ -185,6 +200,33 @@ var subjects = [
"disciplinas" //SAVE FILE HAS
];
var subjects_equivalences = [
{
"code1": "disciplina1",
"code2": "disciplina2",
},function(x){
return x;
},
//SELECT
" s1.code as code1,\
s2.code as code2\
",
//FROM
" subjects as s1\
INNER JOIN\
subjects as s2 ON s1.subject_id=s2.subject_id\
where s1.code!=s2.code\
",
"equivalenciasDisciplinas" //SAVE FILE HAS
];
var courses = [
{
"code": "codigo",
......@@ -204,6 +246,58 @@ var courses = [
////////////////////////////////////////////////////////////////////////////////
/*
On cases of data relations (necessary to "N to N" relations), its needed
to exist a special processing.
The structure must follow the previous documentation, and the function
that transform the data after load from DB must transform all elements from
query in the follow format:
[
{
pkeys = ["valueOfPrimaryKey1", "valueOfPrimaryKey2"]
pkey_name: "nameOfPrimaryKeyColumn"
},
...
]
where valueOfPrimaryKey1 and valueOfPrimaryKey2 represents the values of
primary keys of the models relationed. And pkey_name must be the name of
column relationed with "valueOfPrimaryKey1".
The column name of "valueOfPrimaryKey2" value is not needed because the
Loopback API will discover automatically.
*/
var subjects_courses_relation = [
{
"coursecode": "cursoCod",
"subjectcode": "disciplinaCod",
},function(x){
x.pkeys = [x.cursoCod, x.disciplinaCod];
delete x.cursoCod;
delete x.disciplinaCod;
x.pkey_name = "codigo";
return x;
},
//SELECT
" courses.code as coursecode,\
subjects.code as subjectcode\
",
//FROM
" courses_subjects\
INNER JOIN\
courses on courses.id=courses_subjects.course_id\
INNER JOIN\
subjects on subjects.id=courses_subjects.subject_id\
",
"cursoDisciplina" //SAVE FILE HAS
];
exports.tables = [
rooms,
......@@ -212,7 +306,10 @@ exports.tables = [
departments,
sectors,
subjects,
courses
courses,
subjects_equivalences,
subjects_courses_relation
];
exports.connectionString = connectionString;
This diff is collapsed.
[{"disciplina1":"cd015","disciplina2":"ceg001"},{"disciplina1":"cd015","disciplina2":"cd044"},{"disciplina1":"cd015","disciplina2":"cd029"},{"disciplina1":"cd029","disciplina2":"ceg001"},{"disciplina1":"cd029","disciplina2":"cd044"},{"disciplina1":"cd029","disciplina2":"cd015"},{"disciplina1":"cd044","disciplina2":"ceg001"},{"disciplina1":"cd044","disciplina2":"cd029"},{"disciplina1":"cd044","disciplina2":"cd015"},{"disciplina1":"cd031","disciplina2":"ceg204"},{"disciplina1":"cd027","disciplina2":"ceg206"},{"disciplina1":"ceg001","disciplina2":"cd044"},{"disciplina1":"ceg001","disciplina2":"cd029"},{"disciplina1":"ceg001","disciplina2":"cd015"},{"disciplina1":"cd035","disciplina2":"ceg210"},{"disciplina1":"ce003","disciplina2":"ce009"},{"disciplina1":"cf060","disciplina2":"cf110"},{"disciplina1":"cf110","disciplina2":"cf060"},{"disciplina1":"cm041","disciplina2":"cm047"},{"disciplina1":"cm044","disciplina2":"cma314"},{"disciplina1":"ci183","disciplina2":"ci180"},{"disciplina1":"ci183","disciplina2":"ci208"},{"disciplina1":"ci184","disciplina2":"ci181"},{"disciplina1":"ci184","disciplina2":"ci202"},{"disciplina1":"ci240","disciplina2":"ci182"},{"disciplina1":"ci182","disciplina2":"ci240"},{"disciplina1":"cq025","disciplina2":"cq207"},{"disciplina1":"tt008","disciplina2":"tt081"},{"disciplina1":"tt081","disciplina2":"tt008"},{"disciplina1":"tt007","disciplina2":"tt080"},{"disciplina1":"ceg204","disciplina2":"cd031"},{"disciplina1":"ceg206","disciplina2":"cd027"},{"disciplina1":"ceg210","disciplina2":"cd035"},{"disciplina1":"ci208","disciplina2":"ci180"},{"disciplina1":"ci208","disciplina2":"ci183"},{"disciplina1":"ci180","disciplina2":"ci208"},{"disciplina1":"ci180","disciplina2":"ci183"},{"disciplina1":"bq401","disciplina2":"bq064"},{"disciplina1":"bmed001","disciplina2":"bmed003"},{"disciplina1":"bmed010","disciplina2":"bmed012"},{"disciplina1":"bmed012","disciplina2":"bmed010"},{"disciplina1":"bq007","disciplina2":"bq058"},{"disciplina1":"bb080","disciplina2":"bb020"},{"disciplina1":"bb020","disciplina2":"bb080"},{"disciplina1":"bb079","disciplina2":"bb019"},{"disciplina1":"bb019","disciplina2":"bb079"},{"disciplina1":"bq053","disciplina2":"bq066"},{"disciplina1":"bq066","disciplina2":"bq053"},{"disciplina1":"bq058","disciplina2":"bq007"},{"disciplina1":"tmec004","disciplina2":"tm052"},{"disciplina1":"tmec017","disciplina2":"tm227"},{"disciplina1":"tmec018","disciplina2":"tm228"},{"disciplina1":"tmec019","disciplina2":"tm230"},{"disciplina1":"tmec001","disciplina2":"tm236"},{"disciplina1":"tmec026","disciplina2":"tm244"},{"disciplina1":"tmec032","disciplina2":"tm245"},{"disciplina1":"tmec031","disciplina2":"tm259"},{"disciplina1":"tmec031","disciplina2":"tm368"},{"disciplina1":"tmec027","disciplina2":"tm262"},{"disciplina1":"tmec020","disciplina2":"tm347"},{"disciplina1":"bz014","disciplina2":"bz071"},{"disciplina1":"tm368","disciplina2":"tm259"},{"disciplina1":"tm368","disciplina2":"tmec031"},{"disciplina1":"cm047","disciplina2":"cm041"},{"disciplina1":"bz071","disciplina2":"bz014"},{"disciplina1":"bz070","disciplina2":"bz013"},{"disciplina1":"bt057","disciplina2":"bt058"},{"disciplina1":"cd014","disciplina2":"cd033"},{"disciplina1":"cd014","disciplina2":"cd020"},{"disciplina1":"cd020","disciplina2":"cd033"},{"disciplina1":"cd020","disciplina2":"cd014"},{"disciplina1":"bt058","disciplina2":"bt057"},{"disciplina1":"cq207","disciplina2":"cq025"},{"disciplina1":"ce009","disciplina2":"ce003"},{"disciplina1":"tmec040","disciplina2":"tm268"},{"disciplina1":"tmec040","disciplina2":"tm038"},{"disciplina1":"tmec038","disciplina2":"tm357"},{"disciplina1":"tmec038","disciplina2":"tm246"},{"disciplina1":"tmec039","disciplina2":"tm342"},{"disciplina1":"tmec039","disciplina2":"tm249"},{"disciplina1":"tmec042","disciplina2":"tm256"},{"disciplina1":"tmec034","disciplina2":"tm264"},{"disciplina1":"cd033","disciplina2":"cd020"},{"disciplina1":"cd033","disciplina2":"cd014"},{"disciplina1":"gb115","disciplina2":"gb131"},{"disciplina1":"gb131","disciplina2":"gb115"},{"disciplina1":"tm327","disciplina2":"tmec010"},{"disciplina1":"tm038","disciplina2":"tm268"},{"disciplina1":"tm038","disciplina2":"tmec040"},{"disciplina1":"tm052","disciplina2":"tmec004"},{"disciplina1":"tm227","disciplina2":"tmec017"},{"disciplina1":"tm228","disciplina2":"tmec018"},{"disciplina1":"tm230","disciplina2":"tmec019"},{"disciplina1":"tm244","disciplina2":"tmec026"},{"disciplina1":"tm245","disciplina2":"tmec032"},{"disciplina1":"tm246","disciplina2":"tm357"},{"disciplina1":"tm246","disciplina2":"tmec038"},{"disciplina1":"tm249","disciplina2":"tm342"},{"disciplina1":"tm249","disciplina2":"tmec039"},{"disciplina1":"tm256","disciplina2":"tmec042"},{"disciplina1":"tm259","disciplina2":"tm368"},{"disciplina1":"tm259","disciplina2":"tmec031"},{"disciplina1":"tm262","disciplina2":"tmec027"},{"disciplina1":"tm264","disciplina2":"tmec034"},{"disciplina1":"tm268","disciplina2":"tm038"},{"disciplina1":"tm268","disciplina2":"tmec040"},{"disciplina1":"tm342","disciplina2":"tm249"},{"disciplina1":"tm342","disciplina2":"tmec039"},{"disciplina1":"tm347","disciplina2":"tmec020"},{"disciplina1":"tm357","disciplina2":"tm246"},{"disciplina1":"tm357","disciplina2":"tmec038"},{"disciplina1":"tmec010","disciplina2":"tm327"},{"disciplina1":"tmec058","disciplina2":"tm031"},{"disciplina1":"tm031","disciplina2":"tmec058"},{"disciplina1":"tmec030","disciplina2":"tm252"},{"disciplina1":"tm252","disciplina2":"tmec030"},{"disciplina1":"tm236","disciplina2":"tmec001"},{"disciplina1":"tmec075","disciplina2":"tm265"},{"disciplina1":"tm265","disciplina2":"tmec075"},{"disciplina1":"tmec073","disciplina2":"tm266"},{"disciplina1":"tm266","disciplina2":"tmec073"},{"disciplina1":"bmed011","disciplina2":"bmed013"},{"disciplina1":"bmed013","disciplina2":"bmed011"},{"disciplina1":"ci202","disciplina2":"ci181"},{"disciplina1":"ci202","disciplina2":"ci184"},{"disciplina1":"bq064","disciplina2":"bq401"},{"disciplina1":"bmed003","disciplina2":"bmed001"},{"disciplina1":"tm251","disciplina2":"tmec036"},{"disciplina1":"tm251","disciplina2":"tm359"},{"disciplina1":"tm359","disciplina2":"tmec036"},{"disciplina1":"tm359","disciplina2":"tm251"},{"disciplina1":"tmec036","disciplina2":"tm359"},{"disciplina1":"tmec036","disciplina2":"tm251"},{"disciplina1":"bz013","disciplina2":"bz070"},{"disciplina1":"bq004","disciplina2":"bq067"},{"disciplina1":"ci181","disciplina2":"ci202"},{"disciplina1":"ci181","disciplina2":"ci184"},{"disciplina1":"cma314","disciplina2":"cm044"},{"disciplina1":"th041","disciplina2":"th056"},{"disciplina1":"th056","disciplina2":"th041"},{"disciplina1":"bp063","disciplina2":"bp310"},{"disciplina1":"bp310","disciplina2":"bp063"},{"disciplina1":"bmed002","disciplina2":"bmed004"},{"disciplina1":"bmed004","disciplina2":"bmed002"},{"disciplina1":"tt080","disciplina2":"tt007"},{"disciplina1":"cf059","disciplina2":"cf109"},{"disciplina1":"cf109","disciplina2":"cf059"},{"disciplina1":"bq067","disciplina2":"bq004"}]
\ No newline at end of file
......@@ -41,6 +41,7 @@
- Public: `True`
- Attributes `idInjection`=`false`
* codigo : **string** `Required`, `id`
* nome : **string**
* carga_horaria : **number** `Required`, default=`0`
* duracao : **string** `Required`, default=`semestral`
* modalidade : **string** default=`presencial`
......
......@@ -7,10 +7,10 @@ module.exports = function(Disciplina) {
// in the Model Self Relation in LoopBack API 3
// The model EquivalenciaDisciplina was created to implement this relation
Disciplina.prototype.addEquivalencia = function(disciplina_eq_id, cb) {
Disciplina.prototype.addEquivalencia = function(disciplina_eq_codigo, cb) {
var Eq = app.models.Equivalenciadisciplina;
Eq.create({"disciplina1": this.id, "disciplina2":disciplina_eq_id}, function(err, eq){
Eq.create({"disciplina1": this.codigo, "disciplina2":disciplina_eq_codigo}, function(err, eq){
cb(err,eq);
});
......@@ -18,32 +18,34 @@ module.exports = function(Disciplina) {
Disciplina.prototype.getEquivalencias = function(cb) {
var Eq = app.models.Equivalenciadisciplina;
let myId = this.id;
Eq.find({where:{or:[ {disciplina1:myId}, {disciplina2:myId} ]}}, function(err, eq){
let myCode = this.codigo;
Eq.find({where:{or:[ {disciplina1:myCode}, {disciplina2:myCode} ]}}, function(err, eq){
// Filter only the ids of equivalents courses
// The id can be on disciplina1 or disciplina2 columns
// console.log(eq[0].disciplina1.toString(), eq[0].disciplina2.toString());
var ret = eq.map(function(x){
x = x.toJSON();
if(myId == x.disciplina1) return x.disciplina2;
return x.disciplina1;
});
var codigoDisciplina1 = x.__data.disciplina1;
var codigoDisciplina2 = x.__data.disciplina2;
if(myCode == codigoDisciplina1) return codigoDisciplina2;
return codigoDisciplina1;
});
// Remove repeated information
var uniqueArray = ret.filter(function(item, pos) {
return ret.indexOf(item) == pos;
})
});
cb(err,uniqueArray);
});
};
Disciplina.prototype.deleteEquivalencia = function(disciplina_eq_id, cb) {
Disciplina.prototype.deleteEquivalencia = function(disciplina_eq_codigo, cb) {
var Eq = app.models.Equivalenciadisciplina;
let myId = this.id;
let myCode = this.codigo;
// Find and destroy all relations between the two specified instances
// The relation can be in form of (id1,id2) or (id2,id1)
......@@ -51,14 +53,14 @@ module.exports = function(Disciplina) {
or:[
{
and:[
{disciplina1:myId},
{disciplina2:disciplina_eq_id}
{disciplina1:myCode},
{disciplina2:disciplina_eq_codigo}
]
},
{
and:[
{disciplina1:disciplina_eq_id},
{disciplina2:myId}
{disciplina1:disciplina_eq_codigo},
{disciplina2:myCode}
]
}
]
......
......@@ -12,6 +12,12 @@
"precision": null,
"scale": null
},
"nome": {
"type": "String",
"length": 256,
"precision": null,
"scale": null
},
"carga_horaria": {
"type": "Number",
"required": true,
......@@ -87,8 +93,8 @@
"prototype.addEquivalencia": {
"accepts": [
{
"arg": "disciplina_eq_id",
"type": "number"
"arg": "disciplina_eq_code",
"type": "string"
}
],
"returns": {
......@@ -114,8 +120,8 @@
"prototype.deleteEquivalencia": {
"accepts": [
{
"arg": "disciplina_eq_id",
"type": "number"
"arg": "disciplina_eq_code",
"type": "string"
}
],
"returns": {
......
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 to comment