Newer
Older
const schoolApp = express.Router();
const libs = `${process.cwd()}/libs`;
const squel = require('squel');
const query = require(`${libs}/middlewares/query`);
const response = require(`${libs}/middlewares/response`);
const id2str = require(`${libs}/middlewares/id2str`);
const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
let rqf = new ReqQueryFields();
let rqfCount = new ReqQueryFields();
// Return location
schoolApp.get('/location', (req, res, next) => {
req.result = [
{id: 1, name: 'Urbana'},
{id: 2, name: 'Rural'}
];
next();
}, response('location'));
schoolApp.get('/adm_dependency', (req, res, next) => {
req.sql.from('dependencia_adm')
.field('id')
.field('nome', 'name')
.where('id <= 4');
next();
}, query, response('adm_dependency'));
schoolApp.get('/adm_dependency_detailed', (req, res, next) => {
req.sql.from('dependencia_adm')
.field('id', 'id')
.field('nome', 'name');
next();
}, query, response('adm_dependency_detailed'));
schoolApp.get('/government_agreement', (req, res, next) => {
req.result = [
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('government_agreement'));
schoolApp.get('/agreement', (req, res, next) => {
req.result = [
{id: 1, name: 'Estadual'},
{id: 2, name: 'Municipal'},
{id: 3, name: 'Estadual e Municipal'}
];
next();
}, response('agreement'));
schoolApp.get('/building_school', (req, res, next) => {
req.result = [
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('building_school'));
schoolApp.get('/library_or_reading_room', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('library_or_reading_room'));
schoolApp.get('/informatics_lab', (req, res, next) => {
req.result = [
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('informatics_lab'));
schoolApp.get('/science_lab', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('/science_lab'));
schoolApp.get('/directors_room', (req, res, next) => {
req.result = [

Fernando Erd
committed
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('directors_room'));
schoolApp.get('/teacher_room', (req, res, next) => {
req.result = [

Fernando Erd
committed
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('teacher_room'));
schoolApp.get('/cook_room', (req, res, next) => {
req.result = [

Fernando Erd
committed
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('cook_room'));
schoolApp.get('/playground', (req, res, next) => {

Fernando Erd
committed
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('playground'));
schoolApp.get('/indor_sports_court', (req, res, next) => {
req.result = [

Fernando Erd
committed
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('indor_sports_court'));
schoolApp.get('/nusery', (req, res, next) => {
req.result = [
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('nusery'));
schoolApp.get('/special_attendence_room', (req, res, next) => {
req.result = [
{id: null, name: 'Não Declarado'},
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('special_attendence_room'));
schoolApp.get('/toilet_inside_building', (req, res, next) => {
req.result = [
{id: null, name: 'Não Declarado'},
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('toilet_inside_building'));
schoolApp.get('/denpendency_pne', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('denpendency_pne'));
schoolApp.get('/restroom_pne', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('restroom_pne'));
schoolApp.get('/broadband', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('broadband'));
schoolApp.get('/energy', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('energy'));
schoolApp.get('/wastepipe', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('wastepipe'));
schoolApp.get('/education_day_care_child', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('education_day_care_child'));
schoolApp.get('/education_preschool_child', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('education_preschool_child'));
schoolApp.get('/education_begin_elementary_school', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('education_begin_elementary_school'));
schoolApp.get('/education_end_elementary_school', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('education_end_elementary_school'));
schoolApp.get('/education_middle_school', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('education_middle_school'));
schoolApp.get('/education_professional', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('education_professional'));
schoolApp.get('/education_eja', (req, res, next) => {
req.result = [
{id: 0, name: 'Não'},
{id: 1, name: 'Sim'}
];
next();
}, response('education_eja'));
rqf.addField({
name: 'filter',
field: false,
where: true
}).addValue({
name: 'id',
table: 'escola',
where: {
relation: '=',
type: 'integer',
}
}).addValue({
name: 'city',
table: 'municipio',
tableField: 'nome',
resultField: 'city_name',
where: {
relation: '=',
type: 'integer',
table: 'escola'
},
join: {
primary: 'id',
foreign: 'municipio_id',
foreignTable: 'escola'
}
}).addValue({
name: 'state',
table: 'estado',
tableField: 'nome',
resultField: 'state_name',
where: {
relation: '=',
type: 'integer',
table: 'escola'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: 'escola'
}
}).addValue({
name: 'year',
table: 'escola',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '=',
type: 'integer',
field: 'ano_censo',
table: 'escola'
}
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
rqfCount.addField({
name: 'filter',
field: false,
where: true
}).addField({
name: 'dims',
field: true,
where: false
}).addValue({
name: 'id',
table: 'escola',
tableField: 'id',
where: {
relation: '=',
type: 'integer',
field: 'id'
}
}).addValue({
name: 'city',
table: 'municipio',
tableField: 'nome',
resultField: 'city_name',
where: {
relation: '=',
type: 'integer',
field: 'municipio_id',
table: 'escola'
},
join: {
primary: 'id',
foreign: 'municipio_id',
foreignTable: 'escola'
}
}).addValue({
name: 'state',
table: 'estado',
tableField: 'nome',
resultField: 'state_name',
where: {
relation: '=',
type: 'integer',
field: 'estado_id',
table: 'escola'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: 'escola'
}
}).addValue({
name: 'region',
table: 'regiao',
tableField: 'nome',
resultField: 'region_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'regiao_id',
foreignTable: 'escola'
}
}).addValue({
name: 'year',
table: 'escola',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '=',
type: 'integer',
field: 'ano_censo',
table: 'escola'
}
}).addValue({
name: 'adm_dependency',
table: 'dependencia_adm',
tableField: 'nome',
resultField: 'adm_dependency_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'dependencia_adm_id',
foreignTable: 'escola'
}
}).addValue({
name: 'adm_dependency_detailed',
table: 'dependencia_adm',
tableField: 'nome',
resultField: 'adm_dependency_detailed_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'dependencia_adm_priv',
foreignTable: 'escola'
}
}).addValue({
name: 'location',
table: 'escola',
tableField: 'cod_localizacao',
where: {
relation: '=',
type: 'integer',
field: 'cod_localizacao'
}

Fernando Erd
committed
resultField: 'cook_room_id',
where: {
relation: '=',
type: 'boolean',
field: 'cozinha'
}
}).addValue({
name: 'government_agreement',
table: 'escola',
tableField: 'conveniada_pp',
resultField: 'government_agreement_id',
where: {
relation: '=',
field: 'conveniada_pp'
}

Hamer Iboshi
committed
}).addValue({
name: 'informatics_lab',
table: 'escola',
tableField: 'lab_informatica',
resultField: 'informatics_lab_id',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'lab_informatica'
}
}).addValue({
name: 'science_lab',
table: 'escola',
tableField: 'lab_ciencias',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'lab_ciencias'
}
}).addValue({
name: 'special_attendence_room',
table: 'escola',
tableField: 'sala_atendimento_especial',
resultField: 'special_attendence_room_id',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'sala_atendimento_especial'
}
}).addValue({
name: 'indor_sports_court',
table: 'escola',
tableField: 'quadra_esportes_coberta',

Fernando Erd
committed
resultField: 'indor_sports_court_id',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'quadra_esportes_coberta'
}
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
}).addValue({
name: 'education_eja',
table: 'escola',
tableField: 'ensino_eja',
resultField: 'education_eja_name',
where: {
relation: '=',
type: 'boolean',
field: 'ensino_eja'
}
}).addValue({
name: 'education_professional',
table: 'escola',
tableField: 'educacao_profissional',
resultField: 'education_professional_name',
where: {
relation: '=',
type: 'boolean',
field: 'educacao_profissional'
}
}).addValue({
name: 'education_middle_school',
table: 'escola',
tableField: 'reg_medio_medio',
resultField: 'education_middle_school_name',
where: {
relation: '=',
type: 'boolean',
field: 'reg_medio_medio'
}
}).addValue({
name: 'education_end_elementary_school',
table: 'escola',
tableField: 'reg_fund_af',
resultField: 'education_end_elementary_school_name',
where: {
relation: '=',
type: 'boolean',
field: 'reg_fund_af'
}
}).addValue({
name: 'education_begin_elementary_school',
table: 'escola',
tableField: 'reg_fund_ai',
resultField: 'education_begin_elementary_school_name',
where: {
relation: '=',
type: 'boolean',
field: 'reg_fund_ai'
}
}).addValue({
name: 'education_preschool_child',
table: 'escola',
tableField: 'reg_infantil_preescola',
resultField: 'education_preschool_child_name',
where: {
relation: '=',
type: 'boolean',
field: 'reg_infantil_preescola'
}
}).addValue({
name: 'education_day_care_child',
table: 'escola',
tableField: 'reg_infantil_creche',
resultField: 'education_day_care_child_name',
where: {
relation: '=',
type: 'boolean',
field: 'reg_infantil_creche'
}

Hamer Iboshi
committed
}).addValue({
name: 'directors_room',
table: 'escola',
tableField: 'sala_diretoria',

Fernando Erd
committed
resultField: 'directors_room_id',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'sala_diretoria'
}
}).addValue({
name: 'teacher_room',
table: 'escola',
tableField: 'sala_professor',

Fernando Erd
committed
resultField: 'teacher_room_id',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'sala_professor'
}
}).addValue({
name: 'playground',
table: 'escola',
tableField: 'parque_infantil',

Fernando Erd
committed
resultField: 'playground_id',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'parque_infantil'
}
}).addValue({
name: 'nusery',
table: 'escola',
tableField: 'bercario',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'bercario'
}
}).addValue({
name: 'toilet_inside_building',
table: 'escola',
tableField: 'sanitario_dentro_predio',
resultField: 'toilet_inside_building_id',

Hamer Iboshi
committed
where: {
relation: '=',
type: 'boolean',
field: 'sanitario_dentro_predio'
}
name: 'wastepipe',
table: 'escola',
tableField: 'esgoto_sanitario',
resultField: 'wastepipe_name',
where: {
relation: '=',
type: 'boolean',
field: 'esgoto_sanitario'
}
}).addValue({
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
name: 'water',
table: 'escola',
tableField: 'fornecimento_agua',
resultField: 'water_name',
where: {
relation: '=',
type: 'boolean',
field: 'fornecimento_agua '
}
}).addValue({
name: 'energy',
table: 'escola',
tableField: 'fornecimento_energia',
resultField: 'energy_name',
where: {
relation: '=',
type: 'boolean',
field: 'fornecimento_energia '
}
}).addValue({
name: 'broadband',
table: 'escola',
tableField: 'internet_banda_larga',
resultField: 'broadband_name',
where: {
relation: '=',
type: 'boolean',
field: 'internet_banda_larga '
}
}).addValue({
name: 'restroom_pne',
table: 'escola',
tableField: 'sanitario_pne',
resultField: 'restroom_pne_name',
where: {
relation: '=',
type: 'boolean',
field: 'sanitario_pne '
}
}).addValue({
name: 'denpendency_pne',
table: 'escola',
tableField: 'dependencias_pne',
resultField: 'denpendency_pne_name',
where: {
relation: '=',
type: 'boolean',
field: 'dependencias_pne '
}
}).addValue({
name: 'agreement',
table: 'escola',
tableField: 'tipo_convenio_pp',
where: {
relation: '=',
type: 'integer',
field: 'tipo_convenio_pp'
}
}).addValue({
name: 'building_school',
table: 'escola',
tableField: 'local_func_predio_escolar',
resultField: 'building_school_id',
where: {
relation: '=',
type: 'boolean',
field: 'local_func_predio_escolar'
}
name: 'library_or_reading_room',
table: 'escola',
tableField: ['biblioteca', 'sala_leitura', 'biblioteca_sala_leitura'],
resultField: 'library_or_reading_room',
where: {
relation: '=',
type: 'boolean',
condition: 'or',
field: ['biblioteca', 'sala_leitura', 'biblioteca_sala_leitura']
// SELECT COUNT(escola.id) AS "total", 'Brasil' AS "name", escola.ano_censo AS "year" FROM escola WHERE (escola.biblioteca = ? OR escola.sala_leitura = ? OR escola.biblioteca_sala_leitura) AND (escola.situacao_de_funcionamento = 1 AND escola.ensino_regular = 1) GROUP BY escola.ano_censo ORDER BY escola.ano_censo ASC
schoolApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
console.log(req.filter);
if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) {
res.status(400);
next({
status: 400,
message: 'Wrong/No filter specified'
});
}
.field('escola.ano_censo', 'year')
.field('escola.nome_escola', 'name')
.field('escola.estado_id', 'state_id')
.field('escola.municipio_id', 'city_id');
schoolApp.get('/count', rqfCount.parse(), rqfCount.build(), (req, res, next) => {
req.sql.from('escola')
.field('COUNT(escola.id)', 'total')
.field("'Brasil'", 'name')
.field('escola.ano_censo', 'year')
.group('escola.ano_censo')
.order('escola.ano_censo')
.where('escola.situacao_de_funcionamento = 1 AND escola.ensino_regular = 1');
next();
}, query, id2str.transform(true), response('school'));
module.exports = schoolApp;