Advertisement
Guest User

Untitled

a guest
May 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class UnidadesCurriculares {
  2.   constructor(app, con, NodeRSA, proteger, key) {
  3.     app.get('/ucs/listar/coordenador', (request, response) => {
  4.       const PAGE_LIMIT = 10;
  5.       var filtros = JSON.parse(key.decryptv2(request.query.filtros));
  6.       console.dir(filtros);
  7.       var access_token = key.decryptv2(request.query.access_token);
  8.  
  9.       var responder = function(p, ucs, pagina_atual, pagina_maxima) {
  10.         //console.dir(ucs);
  11.         console.log(JSON.stringify(ucs));
  12.         var data = {
  13.           ucs: ucs, //Array com as ucs
  14.           pagina_atual: pagina_atual,
  15.           pagina_maxima: pagina_maxima
  16.         }
  17.  
  18.         //data = p.userKey.encryptv2(JSON.stringify(data)); //Encriptar
  19.         response.json({
  20.           status: p.userKey.encryptv2("success"),
  21.           data: p.userKey.encryptv2(JSON.stringify(data))
  22.         });
  23.       };
  24.  
  25.       console.log(access_token);
  26.       proteger(access_token, ["aluno", "professor", "coordenador"]).then(function(p) {
  27.         if (p.msg != null) {
  28.           console.log(p.msg);
  29.         }
  30.         if (p.status) {
  31.           //Tem permissão
  32.  
  33.           con.query("SELECT count(*) AS count FROM ucs", function(err, result, fields) {
  34.             if (err) {
  35.               console.log(err);
  36.               response.json({
  37.                 status: p.userKey.encryptv2("error")
  38.               });
  39.             }
  40.  
  41.             var paginas = Math.ceil(JSON.parse(JSON.stringify(result))[0].count / PAGE_LIMIT);
  42.  
  43.             //Se não estiverem definidos filtos vai buscar as primeiras X sem filtrar
  44.             if (filtros.nome == "" && filtros.ano == "" && filtros.semestre == "" && filtros.anoletivo == "") {
  45.               //Ir buscar as primeiras X ucs
  46.               var sql_query = "SELECT * FROM ucs LIMIT ?";
  47.  
  48.               con.query(sql_query, [PAGE_LIMIT], function(err, result, fields) {
  49.                 if (err) {
  50.                   console.log(err);
  51.                   response.json({
  52.                     status: p.userKey.encryptv2("error_select")
  53.                   });
  54.                 } else {
  55.                   //A query foi bem sucessdida
  56.                   responder(p, result, 1, paginas);
  57.                 }
  58.               });
  59.             } else {
  60.               var query_array = [];
  61.               var extra_query = "";
  62.               var filtrar = function() {
  63.                 if (filtros.ano != "") {
  64.                   if (extra_query != "") {
  65.                     extra_query += "AND ano = ? ";
  66.                   } else {
  67.                     extra_query += "WHERE ano = ? ";
  68.                   }
  69.                   query_array.push(filtros.ano);
  70.                 }
  71.                 if (filtros.regime != "") {
  72.                   if (extra_query != "") {
  73.                     extra_query += "AND regime = ? ";
  74.                   } else {
  75.                     extra_query += "WHERE regime = ? ";
  76.                   }
  77.                   query_array.push(filtros.regime);
  78.                 }
  79.                 if (filtros.semestre != "") {
  80.                   if (extra_query != "") {
  81.                     extra_query += "AND semestre = ? ";
  82.                   } else {
  83.                     extra_query += "WHERE semestre = ? ";
  84.                   }
  85.                   query_array.push(filtros.semestre);
  86.                 }
  87.                 if (filtros.pesquisa != "") {
  88.                   if (extra_query != "") {
  89.                     extra_query += "AND nome LIKE '%?%' ";
  90.                   } else {
  91.                     extra_query += "WHERE nome LIKE '%?%' ";
  92.                   }
  93.                   query_array.push(filtros.pesquisa);
  94.                 }
  95.                 /*
  96.                 if (filtros.pagina != "") {
  97.                     extra_query += " LIMIT ? OFFSET ?";
  98.                     query_array.push(PAGE_LIMIT);
  99.                     query_array.push((filtros.pagina.atual - 1) * PAGE_LIMIT);
  100.                 }
  101.                 */
  102.               };
  103.  
  104.               filtrar();
  105.               sql_query = "SELECT * FROM ucs " + extra_query;
  106.               con.query(sql_query, query_array[0], function(err, result, fields) {
  107.                 responder(p, result, pag_atual, pag_max);
  108.               });
  109.             }
  110.           });
  111.         } else {
  112.           //A query foi bem sucessedida
  113.           responder(p, result, 1, paginas);
  114.         }
  115.       });
  116.       //TODO não leva este else
  117.     } else {
  118.       var query_array = [];
  119.       var extra_query = "";
  120.       var filtrar = function() {
  121.         if (filtros.ano != "") {
  122.           if (extra_query != "") {
  123.             extra_query += "AND ano = ? ";
  124.           } else {
  125.             extra_query += "WHERE ano = ? ";
  126.           }
  127.           query_array.push(filtros.ano);
  128.         }
  129.         if (filtros.regime != "") {
  130.           if (extra_query != "") {
  131.             extra_query += "AND regime = ? ";
  132.           } else {
  133.             extra_query += "WHERE regime = ? ";
  134.           }
  135.           query_array.push(filtros.regime);
  136.         }
  137.         if (filtros.semestre != "") {
  138.           if (extra_query != "") {
  139.             extra_query += "AND semestre = ? ";
  140.           } else {
  141.             extra_query += "WHERE semestre = ? ";
  142.           }
  143.           query_array.push(filtros.semestre);
  144.         }
  145.         if (filtros.nome != "") {
  146.           if (extra_query != "") {
  147.             extra_query += "AND nome LIKE '%?%' ";
  148.           } else {
  149.             extra_query += "WHERE nome LIKE '%?%' ";
  150.           }
  151.           query_array.push(filtros.nome);
  152.         }
  153.         /*if (filtros.pagina != "") {
  154.           extra_query += " LIMIT ? OFFSET ?";
  155.           query_array.push(PAGE_LIMIT);
  156.           query_array.push((filtros.pagina.atual - 1) * PAGE_LIMIT);
  157.         }*/
  158.       };
  159.  
  160.       filtrar();
  161.       sql_query = "SELECT * FROM ucs " + extra_query;
  162.       console.log("query_array[0] --> " + query_array[0]);
  163.       console.log("sql_query --> " + sql_query);
  164.       con.query(sql_query, [query_array[0]], function(err, result, fields) {
  165.         console.log("result --> " + result);
  166.         console.log("err -->" + err);
  167.         responder(p, result, 1 /*pag_atual*/ , 2 /*pag_max*/ );
  168.       });
  169.     });
  170.   } else { //TODO não leva este else
  171.     //Não tem permissão
  172.     response.json({
  173.       data: "unauthorized"
  174.     });
  175.   } //TODO nem isto
  176. });
  177. });
  178.  
  179. console.log(access_token);
  180. proteger(access_token, ["aluno", "professor", "coordenador"]).then(function(p) {
  181. if (p.status) {
  182.   //Tem permissão
  183.   console.log("uc_desencriptada = " + uc_desencriptada);
  184.   try { //Tentar transformar a string json num objeto
  185.     var uc_obj = JSON.parse(uc_desencriptada);
  186.  
  187.     //?", [access_token]
  188.     //TODO apagar este console.log();
  189.     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)");
  190.     var sql_query = "INSERT INTO ucs(nome, ano, curso, ects, semestre, regime, ano_letivo, id_professor) VALUES(?,?,?,?,?,?,?,NULL)";
  191.     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) {
  192.       if (err) {
  193.         console.log(err);
  194.         response.json({
  195.           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 }
  196.         });
  197.       } else {
  198.         //A query foi bem sucessdida
  199.         response.json({
  200.           status: p.userKey.encryptv2("success")
  201.         });
  202.       }
  203.     });
  204.  
  205.   } catch (e) {
  206.     console.log(e);
  207.     response.json({
  208.       status: p.userKey.encryptv2("error_generic")
  209.     });
  210.   }
  211. } else {
  212.   //Não tem permissão
  213.   response.json({
  214.     status: p.userKey.encryptv2("unauthorized")
  215.   });
  216. }
  217. });
  218. });
  219.  
  220. app.get('/ucs/listar/professor', (request, response) => {
  221.   var access_token = key.decryptv2(request.query.access_token);
  222.  
  223.   console.log(access_token);
  224.   proteger(access_token, ["professor", "coordenador"]).then(function(p) {
  225.     if (p.msg != null) {
  226.       console.log(p.msg);
  227.     }
  228.     if (p.status) {
  229.       //Tem permissão
  230.       if (p.professorId > -1) {
  231.  
  232.         con.query("SELECT id, nome, ano, semestre, regime, ano_letivo FROM ucs WHERE id_professor=?", "[" + p.professorId + "]", function(err, result, fields) {
  233.           if (err) {
  234.             console.log(err);
  235.             response.json({
  236.               status: p.userKey.encryptv2("error")
  237.             });
  238.           } else {
  239.             response.json({
  240.               status: p.userKey.encryptv2("success"),
  241.               ucs: p.userKey.encryptv2(JSON.stringify(result))
  242.             });
  243.           }
  244.         });
  245.       } else {
  246.         console.log("ER_p.pid")
  247.         response.json({
  248.           data: "unauthorized"
  249.         });
  250.       }
  251.     } else {
  252.       //Não tem permissão
  253.       response.json({
  254.         status: p.userKey.encryptv2("unauthorized")
  255.       });
  256.     }
  257.   });
  258. });
  259. }
  260. }
  261.  
  262. module.exports = UnidadesCurriculares;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement