Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class UnidadesCurriculares {
- constructor(app, con, NodeRSA, proteger, key) {
- app.get('/ucs/listar/coordenador', (request, response) => {
- const PAGE_LIMIT = 10;
- var filtros = JSON.parse(key.decryptv2(request.query.filtros));
- console.dir(filtros);
- var access_token = key.decryptv2(request.query.access_token);
- var responder = function(p, ucs, pagina_atual, pagina_maxima) {
- //console.dir(ucs);
- console.log(JSON.stringify(ucs));
- var data = {
- ucs: ucs, //Array com as ucs
- pagina_atual: pagina_atual,
- pagina_maxima: pagina_maxima
- }
- //data = p.userKey.encryptv2(JSON.stringify(data)); //Encriptar
- response.json({
- status: p.userKey.encryptv2("success"),
- data: p.userKey.encryptv2(JSON.stringify(data))
- });
- };
- console.log(access_token);
- proteger(access_token, ["aluno", "professor", "coordenador"]).then(function(p) {
- if (p.msg != null) {
- console.log(p.msg);
- }
- if (p.status) {
- //Tem permissão
- con.query("SELECT count(*) AS count FROM ucs", function(err, result, fields) {
- if (err) {
- console.log(err);
- response.json({
- status: p.userKey.encryptv2("error")
- });
- }
- var paginas = Math.ceil(JSON.parse(JSON.stringify(result))[0].count / PAGE_LIMIT);
- //Se não estiverem definidos filtos vai buscar as primeiras X sem filtrar
- if (filtros.nome == "" && filtros.ano == "" && filtros.semestre == "" && filtros.anoletivo == "") {
- //Ir buscar as primeiras X ucs
- var sql_query = "SELECT * FROM ucs LIMIT ?";
- con.query(sql_query, [PAGE_LIMIT], function(err, result, fields) {
- if (err) {
- console.log(err);
- response.json({
- status: p.userKey.encryptv2("error_select")
- });
- } else {
- //A query foi bem sucessdida
- responder(p, result, 1, paginas);
- }
- });
- } else {
- var query_array = [];
- var extra_query = "";
- var filtrar = function() {
- if (filtros.ano != "") {
- if (extra_query != "") {
- extra_query += "AND ano = ? ";
- } else {
- extra_query += "WHERE ano = ? ";
- }
- query_array.push(filtros.ano);
- }
- if (filtros.regime != "") {
- if (extra_query != "") {
- extra_query += "AND regime = ? ";
- } else {
- extra_query += "WHERE regime = ? ";
- }
- query_array.push(filtros.regime);
- }
- if (filtros.semestre != "") {
- if (extra_query != "") {
- extra_query += "AND semestre = ? ";
- } else {
- extra_query += "WHERE semestre = ? ";
- }
- query_array.push(filtros.semestre);
- }
- if (filtros.pesquisa != "") {
- if (extra_query != "") {
- extra_query += "AND nome LIKE '%?%' ";
- } else {
- extra_query += "WHERE nome LIKE '%?%' ";
- }
- query_array.push(filtros.pesquisa);
- }
- /*
- if (filtros.pagina != "") {
- extra_query += " LIMIT ? OFFSET ?";
- query_array.push(PAGE_LIMIT);
- query_array.push((filtros.pagina.atual - 1) * PAGE_LIMIT);
- }
- */
- };
- filtrar();
- sql_query = "SELECT * FROM ucs " + extra_query;
- con.query(sql_query, query_array[0], function(err, result, fields) {
- responder(p, result, pag_atual, pag_max);
- });
- }
- });
- } else {
- //A query foi bem sucessedida
- responder(p, result, 1, paginas);
- }
- });
- //TODO não leva este else
- } else {
- var query_array = [];
- var extra_query = "";
- var filtrar = function() {
- if (filtros.ano != "") {
- if (extra_query != "") {
- extra_query += "AND ano = ? ";
- } else {
- extra_query += "WHERE ano = ? ";
- }
- query_array.push(filtros.ano);
- }
- if (filtros.regime != "") {
- if (extra_query != "") {
- extra_query += "AND regime = ? ";
- } else {
- extra_query += "WHERE regime = ? ";
- }
- query_array.push(filtros.regime);
- }
- if (filtros.semestre != "") {
- if (extra_query != "") {
- extra_query += "AND semestre = ? ";
- } else {
- extra_query += "WHERE semestre = ? ";
- }
- query_array.push(filtros.semestre);
- }
- if (filtros.nome != "") {
- if (extra_query != "") {
- extra_query += "AND nome LIKE '%?%' ";
- } else {
- extra_query += "WHERE nome LIKE '%?%' ";
- }
- query_array.push(filtros.nome);
- }
- /*if (filtros.pagina != "") {
- extra_query += " LIMIT ? OFFSET ?";
- query_array.push(PAGE_LIMIT);
- query_array.push((filtros.pagina.atual - 1) * PAGE_LIMIT);
- }*/
- };
- filtrar();
- sql_query = "SELECT * FROM ucs " + extra_query;
- console.log("query_array[0] --> " + query_array[0]);
- console.log("sql_query --> " + sql_query);
- con.query(sql_query, [query_array[0]], function(err, result, fields) {
- console.log("result --> " + result);
- console.log("err -->" + err);
- responder(p, result, 1 /*pag_atual*/ , 2 /*pag_max*/ );
- });
- });
- } else { //TODO não leva este else
- //Não tem permissão
- response.json({
- data: "unauthorized"
- });
- } //TODO nem isto
- });
- });
- console.log(access_token);
- proteger(access_token, ["aluno", "professor", "coordenador"]).then(function(p) {
- if (p.status) {
- //Tem permissão
- console.log("uc_desencriptada = " + uc_desencriptada);
- try { //Tentar transformar a string json num objeto
- var uc_obj = JSON.parse(uc_desencriptada);
- //?", [access_token]
- //TODO apagar este console.log();
- console.log(">>> Dados a Inserir: (" + uc_obj.nome + "," + uc_obj.ano + "," + uc_obj.curso + "," + uc_obj.ects + "," + uc_obj.semestre + "," + uc_obj.regime + "," + uc_obj.anoletivo + ", 0)");
- var sql_query = "INSERT INTO ucs(nome, ano, curso, ects, semestre, regime, ano_letivo, id_professor) VALUES(?,?,?,?,?,?,?,NULL)";
- con.query(sql_query, [uc_obj.nome, uc_obj.ano, uc_obj.curso, uc_obj.ects, uc_obj.semestre, uc_obj.regime, uc_obj.anoletivo], function(err, result, fields) {
- if (err) {
- console.log(err);
- response.json({
- status: p.userKey.encryptv2("error_inserir") //TODO se quiseres mete estas mensagens mais especificas, tipo "error_inserir" depois no cliente if("error_inserir"){ mostra mensagem decente }
- });
- } else {
- //A query foi bem sucessdida
- response.json({
- status: p.userKey.encryptv2("success")
- });
- }
- });
- } catch (e) {
- console.log(e);
- response.json({
- status: p.userKey.encryptv2("error_generic")
- });
- }
- } else {
- //Não tem permissão
- response.json({
- status: p.userKey.encryptv2("unauthorized")
- });
- }
- });
- });
- app.get('/ucs/listar/professor', (request, response) => {
- var access_token = key.decryptv2(request.query.access_token);
- console.log(access_token);
- proteger(access_token, ["professor", "coordenador"]).then(function(p) {
- if (p.msg != null) {
- console.log(p.msg);
- }
- if (p.status) {
- //Tem permissão
- if (p.professorId > -1) {
- con.query("SELECT id, nome, ano, semestre, regime, ano_letivo FROM ucs WHERE id_professor=?", "[" + p.professorId + "]", function(err, result, fields) {
- if (err) {
- console.log(err);
- response.json({
- status: p.userKey.encryptv2("error")
- });
- } else {
- response.json({
- status: p.userKey.encryptv2("success"),
- ucs: p.userKey.encryptv2(JSON.stringify(result))
- });
- }
- });
- } else {
- console.log("ER_p.pid")
- response.json({
- data: "unauthorized"
- });
- }
- } else {
- //Não tem permissão
- response.json({
- status: p.userKey.encryptv2("unauthorized")
- });
- }
- });
- });
- }
- }
- module.exports = UnidadesCurriculares;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement