Skip to content

Instantly share code, notes, and snippets.

@cfoch
Created November 14, 2017 21:54
Show Gist options
  • Save cfoch/be639ece41868b28b2d929e3339bcd3c to your computer and use it in GitHub Desktop.
Save cfoch/be639ece41868b28b2d929e3339bcd3c to your computer and use it in GitHub Desktop.
// # Users Controller
var _ = require('lodash'),
Model = require('../models'),
debug = require('debug')('sisges:controllers:cursos'),
cursos;
/**
* ### Controller Cursos
*/
Curso = {
getHorarios: function(object, options) {
// Yes, I feel horrrible because of this hard-coded const.
// const especialidadId = 1;
/*
const ret = [];
const cursoId = options.params.id;
const semestreId = options.query.idSemestre;
const pageSize = options.query.pageSize,
pageIndex = Math.floor(options.query.pageIndex / pageSize + 1),
query = '%' + options.query.query + '%';
let horarios = model.knex('horario')
.select('id', 'nombre')
.where({
'curso_id': cursoId,
'semestre_id': semestreId
});
return horarios.then(data => {
//console.log(data);
data.forEach(horario => {
let item = {
idHorario: horario.id,
nombre: horario.nombre,
salones: []
};
ret.push(item);
// Llenar salones
const salonXhorario = model.knex('horario_x_salon')
.select('salon_id').where({horario_id: horario.id});
salonXhorario.then(data => {
data.forEach(salon => {
item.salones.push({
idSalon: salon.salon_id,
horarios: salon.horarios
});
});
});
});
return ret;
});
*/
const model = {};
const processQ = _.flow(
_.groupBy('id'),
_.toPairs,
_.map(([k, v]) => ({
idHorario: k,
nombre: v[0].nombre,
salones: _.map(x => x['horario_x_salon.salon_id'], v),
}))
);
// Yes, I feel horrrible because of this hard-coded const.
const cursoId = params.id;
const semestreId = query.idSemestre;
const data = await model
.knex('horario')
.join('horario_x_salon', 'horario.id', '=', 'horario_x_salon.horario_id')
.select('id', 'nombre', 'horario_x_salon.salon_id')
.where({
curso_id: cursoId,
semestre_id: semestreId,
});
return processQ(data);
},
/**
* ## Read
* @param {{id, context}} options
* @returns {Promise<Curso>} Curso
*/
read: function (object, options) {
// Make the call to the Model
let espId = 1,
pageSize = options.query.pageSize,
pageIndex = options.query.pageIndex,
query = '%' + options.query.query + '%'
debug(pageSize, pageIndex)
m = model.Curso.where('especialidad_id', '=', espId)
if (options.query.query !== undefined && options.query.query !== '' ) {
m = m.where('nombre', 'ILIKE', query)
}
// pageIndex en realidad no es el índice sino la cantidad de filas que tiene
// la tabla al parecer.
pageIndex = Math.floor(pageIndex / pageSize + 1);
if (pageSize == -1) {
return m
.fetchAll()
.then((data) => {
debug(data)
return {
data: data,
totalResultsAvailable: 0,
pageIndex: 0,
pageSize: 0
}
})
} else {
return m
.fetchPage({
withRelated: ['especialidad'],
pageSize: pageSize,
page: pageIndex
}).then((cursos) => {
let data = []
cursos.forEach((curso) => {
let e = curso.related('especialidad').toJSON()
let c = curso.toJSON()
data.push({
id:c.id,
curso: c.nombre,
descripcion: c.descripcion,
especialidad: e.nombre,
creditos: c.creditos
})
})
//const curs = Object.keys(data).map((k) => data[k]);
//console.log('curs: ' + curs);
//data = curs.slice(pageIndex*pageSize,(pageIndex+1)*pageSize);
//console.log('data: ' + data)
//console.log('cursos.pagination.rowCount: ' + cursos.pagination.rowCount);
return {
data: data,
totalResultsAvailable: cursos.pagination.rowCount,
pageIndex: cursos.pagination.page - 1,
pageSize: cursos.pagination.pageSize,
}
})
}
},
read2: function (object, options) {
// Make the call to the Model
let id = options.params.id
return model.knex.raw(`
select s.id, s.year, s.secuencia
from semestre s
join horario ccs
on s.id = ccs.semestre_id
where ccs.curso_id = '` + String(id) +
'\' group by s.id order by s.id')
.then((data) => {
debug(data.rows)
return Promise.resolve(data.rows)
})
},
getdata: function (object, options) {
// Make the call to the Model
//id es params :D
//semestreId es query
let espId = 1,
query = '%' + options.query.query + '%',
id = options.params.id
m = model.Curso.where('especialidad_id', '=', espId)
if (options.query.query !== undefined && options.query.query !== '' ) {
m = m.where('nombre', 'LIKE', query)
}
return m
.fetchPage({
withRelated: ['resultadosAprendizaje','resultadosAprendizaje.competencias', 'especialidad']
}).then((cursos) => {
let data = []
cursos.forEach((curso) => {
if(curso.id === id){
let c = curso.toJSON()
//let e = curso.related('especialidad').toJSON()
//debug(c.especialidad)
//let c = curso.toJSON()
//let r = curso.related('resultadosAprendizaje')
// Set de id de competencias para evitar repetir competencias.
let competenciasIds = []
let todasCompetencias = []
c.resultadosAprendizaje.forEach((ra) => {
//debug(ra.competencias)
ra.competencias.forEach((compt) => {
if (competenciasIds.indexOf(compt.id) == -1){
let item = {
idCompetencia : compt.id,
nombre: compt.nombre,
descripcion: compt.descripcion,
resultadosAprendizaje: []
}
todasCompetencias.push(item)
competenciasIds.push(compt.id);
}
})
})
let anything = []
todasCompetencias.forEach((allcompetenci) => {
c.resultadosAprendizaje.forEach((ra) => {
ra.competencias.forEach((competenci) => {
if(competenci.id == allcompetenci.idCompetencia){
let item = {
nombre: ra.nombre,
nivel: ra.nivel,
descripcion: ra.descripcion
}
allcompetenci.resultadosAprendizaje.push(item)
debug(item)
}
})
})
})
//debug(todasCompetencias)
data.push({
id:c.id,
nombre: c.nombre,
descripcion: c.descripcion,
especialidad: c.especialidad.nombre,
creditos: c.creditos,
ciclo: c.ciclo,
habilitado: c.habilitado,
resultadosAprendizaje : c.resultadosAprendizaje
})
data[data.length - 1].competencias = todasCompetencias
return data[data.length - 1]
}
})
return data[0]
})
},
/**
* ## Create
* @param {{id, nombre, apellidos, token_pucp, email}} Curso
* @returns {Promise<Curso> Curso
*/
create: function (object, options) {
var curso = object.curso
console.log('curso: ' + object.curso);
_.extend(curso, {especialiad_id: object.especialidad})
console.log("especialidad ID : " + object.especialidad)
var obj = {
especialidad_id: curso.especialidad,
id: curso.id,
nombre: curso.nombre,
descripcion: curso.descripcion,
creditos: curso.creditos,
ciclo: curso.ciclo
}
return model.Curso.add(obj)
},
/**
* ## Update
* @param {{id, nombre, apellidos, token_pucp, email}} Curso
* @returns {Promise<Curso>} Curso
*/
update: function (data, options){
var where = {
id: data.id
}
return model.Curso.edit(data, where, options)
},
/**
* ## Delete
* @param {{id}} Curso
* @returns {Promise<Curso>} Curso
*/
delete: function (data, options){
return model.Curso
.where(data)
.destroy()
.then((m)=>{
return m;
})
}
};
module.exports = {
Curso: Curso
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment