Commit 475c2547 authored by Bruno Meyer's avatar Bruno Meyer 😢 Committed by Matheus Horstmann

Create Disciplina and EquivalenciaDisciplina Model

parent dcb5e6bb
.git
.dockerignore
docker-compose.yml
Dockerfile
.env
package-lock.json
*.bak
......@@ -22,4 +22,5 @@ npm-debug.log
dist
*xunit.xml
.nyc_output/
.editorconfig
\ No newline at end of file
.editorconfig
.env
FROM node:8.12.0
LABEL maintainer="horstmannmat <mch15@inf.ufpr.br>"
ENV DEBIAN_FRONTEND noninteractive
ENV LANG C.UTF-8
#Install apt-utils to prevent warning messages
RUN apt-get -y update -qq && apt-get -y upgrade -qq && apt-get install -y -qq apt-utils vim
# Set an environment variable to store where the app is installed to inside of the Docker image.
ENV INSTALL_PATH /app
RUN mkdir -p $INSTALL_PATH
# This sets the context of where commands will be ran in and is documented
# on Docker's website extensively.
WORKDIR $INSTALL_PATH
COPY . .
RUN yarn global add yarn@latest && \
yarn global add loopback-cli@3.x && \
yarn install
# RUN host ensalamento-postgres
#
# VOLUME ["$INSTALL_PATH/"]
#
EXPOSE 3000
ENTRYPOINT ["/app/ensalamento-entrypoint.sh"]
CMD ["DEVELOPMENT"]
"use strict";
var async = require('async');
var path = require('path');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.ensalamento;
var lbTables = ['User','AccessToken','ACL','RoleMapping','Role','Sala','Bloco','Disciplina', 'EquivalenciaDisciplina'];
ds.automigrate(lbTables, function(err) {
if (err) throw err;
async.waterfall([criaBloco,criaSala,criaUser,criaDisciplina, criaEquivalencia], function(err) {
if (err) throw err;
ds.disconnect();
})
});
function criaBloco(cb){
var blocos = [
{localizacao: '0,0', tamanho: 10},
{localizacao: '0,0', tamanho: 10}
];
async.each(blocos, function(bloco, callback) {
app.models.Bloco.create(bloco, function(err, model) {
callback(err);
console.log('Created:', model);
});
}, function(err) {
if (err) throw err;
cb(err);
});
}
function criaSala(cb){
var salas = [
{localizacao: '41.12,-71.34', capacidade: 10, tipo:"teste", restrita:false},
{localizacao: '41.12,-71.34', capacidade: 10, tipo:"teste", restrita:true}
];
async.each(salas, function(sala, callback) {
app.models.Sala.create(sala, function(err, model) {
callback(err);
console.log('Created:', model);
});
}, function(err) {
if (err) throw err;
cb(err);
});
}
function criaUser(cb){
app.models.User.create([
{username: 'admin', email: 'admin@admin.com', password: '123mudar'},
{username: 'normal', email: 'normal@normal.com', password: '123mudar'}
],function(err, users) {
if (err) throw err;
console.log('Created users:', users);
app.models.Role.create({
name: 'admin'
}, function(err, role) {
if (err) throw err;
console.log('Created role:', role);
role.principals.create({
principalType: app.models.RoleMapping.USER,
principalId: users[0].id
}, function(err, principal) {
if (err) throw err;
console.log('Assign admin Role to initial User:', users[0].email);
cb(err);
});
});
});
}
function criaEquivalencia(cb){
app.models.Disciplina.find({id:1}, function(err, disc){
disc[0].addEquivalencia(2,function(err,data){
if (err) throw err;
console.log('Created EquivalenciaDisciplina:', disc);
});
});
}
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"}
];
async.each(disciplinas, function(disciplina, callback) {
app.models.Disciplina.create(disciplina, function(err, model) {
callback(err);
console.log('Created:', model);
});
}, function(err) {
cb(err);
});
}
var path = require('path');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.ensalamento;
ds.automigrate('Bloco', function(err) {
if (err) throw err;
var blocos = [
{localizacao: '0,0', tamanho: 10},
{localizacao: '0,0', tamanho: 10}
];
var count = blocos.length;
blocos.forEach(function(bloco) {
app.models.Bloco.create(bloco, function(err, model) {
if (err) throw err;
console.log('Created:', model);
count--;
if (count === 0)
ds.disconnect();
});
});
});
ds.automigrate('Sala', function(err) {
if (err) throw err;
var salas = [
{localizacao: '41.12,-71.34', capacidade: 10, tipo:"teste", restrita:false},
{localizacao: '41.12,-71.34', capacidade: 10, tipo:"teste", restrita:true}
];
var count = salas.length;
salas.forEach(function(sala) {
app.models.Sala.create(sala, function(err, model) {
if (err) throw err;
console.log('Created:', model);
count--;
if (count === 0)
ds.disconnect();
});
});
});
ds.automigrate('User', function(err) {
if (err) throw err;
app.models.User.create([
{username: 'admin', email: 'admin@admin.com', password: '123mudar'}
],function(err, users) {
if (err) throw err;
console.log('Created users:', users);
app.models.Role.create({
name: 'admin'
}, function(err, role) {
if (err) throw err;
console.log('Created role:', role);
role.principals.create({
principalType: RoleMapping.USER,
principalId: users[0].id
}, function(err, principal) {
if (err) throw err;
console.log('Assign admin Role to initial User:', users[0].email);
});
});
});
ds.disconnect();
});
var path = require('path');
var async = require('async');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.ensalamento;
function discoverAndBuildUser(){
ds.discoverAndBuildModels('user', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.User.find(function(err, users) {
if (err) return console.log(err);
console.log(users);
});
});
}
function discoverAndBuildAccessToken(){
ds.discoverAndBuildModels('accesstoken', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.AccessToken.find(function(err, accesstokens) {
if (err) return console.log(err);
console.log(accesstoken);
});
});
}
function discoverAndBuildACL(){
ds.discoverAndBuildModels('acl', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.ACL.find(function(err, acls) {
if (err) return console.log(err);
console.log(acls);
});
});
}
function discoverAndBuildRoleMapping(){
ds.discoverAndBuildModels('rolemapping', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.RoleMapping.find(function(err, rolesmapping) {
if (err) return console.log(err);
console.log(rolesmapping);
});
});
}
function discoverAndBuildRole(){
ds.discoverAndBuildModels('role', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Role.find(function(err, roles) {
if (err) return console.log(err);
console.log(roles);
});
});
}
function discoverAndBuildSala(){
ds.discoverAndBuildModels('sala', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Sala.find(function(err, salas) {
if (err) return console.log(err);
console.log(salas);
});
});
}
function discoverAndBuildBloco(){
ds.discoverAndBuildModels('bloco', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Bloco.find(function(err, blocos) {
if (err) return console.log(err);
console.log(blocos);
});
});
}
function discoverAndBuildDisciplina(){
ds.discoverAndBuildModels('disciplina', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Disciplina.find(function(err, disciplinas) {
if (err) return console.log(err);
console.log(disciplina);
});
});
}
function discoverAndBuildEquivalenciaDisciplina(){
ds.discoverAndBuildModels('equivalenciadisciplina', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.EquivalenciaDisciplina.find(function(err, equivalenciadisciplinas) {
if (err) return console.log(err);
console.log(equivalenciadisciplinas);
});
});
}
var path = require('path');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.ensalamento;
var lbTables = ['user','accesstoken','acl','rolemapping','role','sala','bloco','disciplina','equivalenciadisciplina'];
var count = lbTables.length;
lbTables.forEach(function(table) {
ds.discoverSchema(table, {schema: "public"}, function(err, schema) {
if (err) throw err;
var json = JSON.stringify(schema, null, ' ');
console.log(json);
});
count--;
if (count === 0)
ds.disconnect();
});
......@@ -5,7 +5,7 @@
- Attributes
* localizacao : **geopoint** `Required`
* capacidade : **number** `Required`, default=`0`
* tipo : *string* `Required`, default=`"normal"`
* tipo : **string** `Required`, default=`"normal"`
* restrita : **boolean** `Required`, default=`false`
- ACLs
* All permissions not specified is `DENY`
......@@ -22,4 +22,28 @@
* **$authenticated** ROLE have all READ operations
* **admin** ROLE have all EXECUTE, READ and WRITE operations
- Relations
* Has many : **Sala** `nestRemoting`, `disableInclude`
\ No newline at end of file
* Has many : **Sala** `nestRemoting`, `disableInclude`
## Disciplina
- Public: `True`
- Attributes
* codigo : **string** `Required`
* carga_horaria : **number** `Required`, default=`0`
* duracao : **string** `Required`, default=`semestral`
* modalidade : **string** `Required`, default=`presencial`
- ACLs
* All permissions not specified is `DENY`
* **admin** ROLE have all EXECUTE, READ and WRITE operations
- Relations
* This model have a self-realtion. To implement this, we use the EquivalenciaDisciplina model
## EquivalenciaDisciplina
- Public: `False`
- Description: Contain the self relation of Disciplina
- Attributes
- ACLs
* All permissions not specified is `DENY`
* **admin** ROLE have all EXECUTE, READ and WRITE operations
- Relations
* Belongs To : **Disciplina** `Required` as `disciplina1`
* Belongs To : **Disciplina** `Required` as `disciplina2`
......@@ -4,7 +4,12 @@
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
"validateUpsert": true,
"idInjection": false,
"ensalemento": {
"schema": "strongloop",
"table": "bloco"
}
},
"properties": {
"localizacao": {
......@@ -22,7 +27,7 @@
"salas": {
"type": "hasMany",
"model": "Sala",
"foreignKey": "",
"foreignKey": "id",
"options": {
"nestRemoting": true,
"disableInclude": true
......@@ -37,19 +42,17 @@
"property": "*"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW",
"accessType": "READ"
"permission": "ALLOW"
},
{
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW",
"property":"*"
"property": "*"
}
],
"methods": {}
}
'use strict';
var app = require('../../server/server');
module.exports = function(Disciplina) {
// The follow remote methods was constructed because there is many problems
// 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) {
var Eq = app.models.EquivalenciaDisciplina;
Eq.create({"disciplina1": this.id, "disciplina2":disciplina_eq_id}, function(err, eq){
cb(err,eq);
});
};
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){
// 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;
});
// 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) {
var Eq = app.models.EquivalenciaDisciplina;
let myId = this.id;
// Find and destroy all relations between the two specified instances
// The relation can be in form of (id1,id2) or (id2,id1)
Eq.destroyAll({
or:[
{
and:[
{disciplina1:myId},
{disciplina2:disciplina_eq_id}
]
},
{
and:[
{disciplina1:disciplina_eq_id},
{disciplina2:myId}
]
}
]
}, function(err, info){
cb(err,info);
});
};
};
{
"name": "Disciplina",
"plural": "disciplinas",
"base": "PersistedModel",
"options": {
"ensalemento": {
"schema": "strongloop",
"table": "disciplina"
}
},
"properties": {
"codigo": {
"type": "String",
"required": true,
"length": 64,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "codigo",
"dataType": "character varying",
"dataLength": 64,
"dataPrecision": null,
"dataScale": null,
"nullable": "NO"
}
},
"carga_horaria": {
"type": "Number",
"required": true,
"precision": null,
"scale": null,
"default": 0,
"postgresql": {
"columnName": "carga_horaria",
"dataType": "Integer",
"dataPrecision": null,
"dataScale": null,
"nullable": "NO",
"default": 0
}
},
"duracao": {
"type": "String",
"required": true,
"length": 64,
"precision": null,
"scale": null,
"default": "semestral",
"postgresql": {
"columnName": "duracao",
"dataType": "character varying",
"dataLength": 64,
"dataPrecision": null,
"dataScale": null,
"nullable": "NO",
"default": "semestral"
}
},
"modalidade": {
"type": "String",
"required": true,
"length": 64,
"precision": null,
"scale": null,
"default": "presencial",
"postgresql": {
"columnName": "modalidade",
"dataType": "character varying",
"dataLength": 64,
"dataPrecision": null,
"dataScale": null,
"nullable": "NO",
"default": "presencial"
}
}
},
"validations": [],
"relations": {},
"methods": {
"prototype.addEquivalencia": {
"accepts": [
{
"arg": "disciplina_eq_id",
"type": "number"
}
],
"returns": {
"arg": "data",
"type": "string"
},
"http": {
"verb": "post",
"path": "/addEquivalencia"
}
},
"prototype.getEquivalencias": {
"accepts": [],
"returns": {
"arg": "data",
"type": "string"
},
"http": {
"verb": "get",
"path": "/getEquivalencias"
}
},
"prototype.deleteEquivalencia": {
"accepts": [
{
"arg": "disciplina_eq_id",
"type": "number"
}
],
"returns": {
"arg": "data",
"type": "string"
},
"http": {
"verb": "post",
"path": "/deleteEquivalencia"
}
}
},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY",
"property": "*"
},
{
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW",
"property": "*"
}
]
}
'use strict';
module.exports = function(Equivalenciadisciplina) {
};
{
"name": "EquivalenciaDisciplina",
"plural": "equivalenciasDisciplinas",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": false,
"idInjection": true