mirror of
https://github.com/Steffo99/registro-fermi.git
synced 2024-11-21 23:54:28 +00:00
534 lines
9.8 KiB
JavaScript
534 lines
9.8 KiB
JavaScript
/* Copyright © 2016 Stefano Pigozzi, Emiliano Maccaferri
|
|
* Released under the GPL v3 license
|
|
*/
|
|
|
|
var db = require("./connection");
|
|
var utils = require("./utils");
|
|
|
|
module.exports.addWriteUp = function(sessionid, writeup, classname, student, response, callback){
|
|
|
|
db.query("SELECT username FROM sessions WHERE session_id = ? and rank = 'prof'", [sessionid], function(err, row){
|
|
|
|
console.log("Query eseguita");
|
|
if(row.length == 1){
|
|
|
|
console.log("200");
|
|
var username = utils.rowToString(row, "username");
|
|
|
|
db.query("SELECT classes FROM prof_classes WHERE username = ?", [username], function(err, classes){
|
|
|
|
if(classes.length == 1){
|
|
|
|
var cla = utils.rowToString(classes, "classes");
|
|
var cArr = cla.split(",");
|
|
|
|
if(cArr.contains(classname)){
|
|
|
|
db.query("SELECT firstname, lastname FROM users WHERE username = ?", [student], function(err, row){
|
|
|
|
if(err){
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "fatal"
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
callback(err, response);
|
|
|
|
}
|
|
|
|
if(row.length == 0){
|
|
|
|
console.log("Username inesistente!");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "username_does_not_exist"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
if(row.length == 1){
|
|
|
|
db.query("INSERT INTO personal_writeups (username, writeup) VALUES(?,?)", [student, writeup], function(err, success){
|
|
|
|
if(err){
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "fatal"
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
callback(err, response);
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
success: true,
|
|
writeup: writeup,
|
|
student: student
|
|
|
|
}
|
|
|
|
console.log("Fatto!");
|
|
callback(null, response);
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("Non di questa classe!");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "not_of_this_class"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "no_classes_found"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("403");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "Unauthorized"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
module.exports.addClassWriteUp = function(sessionid, writeup, classname, response, callback){
|
|
|
|
db.query("SELECT username FROM sessions WHERE session_id = ? and rank = 'prof'", [sessionid], function(err, row){
|
|
|
|
console.log("Query eseguita");
|
|
if(row.length == 1){
|
|
|
|
console.log("200");
|
|
var username = utils.rowToString(row, "username");
|
|
|
|
db.query("SELECT classes FROM prof_classes WHERE username = ?", [username], function(err, classes){
|
|
|
|
if(classes.length == 1){
|
|
|
|
var cla = utils.rowToString(classes, "classes");
|
|
var cArr = cla.split(",");
|
|
|
|
if(cArr.contains(classname)){
|
|
|
|
db.query("INSERT INTO class_writeups (added_by, writeup, class) VALUES(?,?,?)", [username, writeup, classname], function(err, success){
|
|
|
|
if(err){
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "fatal"
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
callback(err, response);
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
success: true,
|
|
writeup: writeup,
|
|
classname: classname
|
|
|
|
}
|
|
|
|
console.log("Fatto!");
|
|
callback(null, response);
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("Non di questa classe!");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "not_of_this_class"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "no_classes_found"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("403");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "Unauthorized"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
module.exports.getClassWriteUps = function(sessionid, classname, response, callback){
|
|
|
|
db.query("SELECT username, rank FROM sessions WHERE session_id = ?", [sessionid], function(err, row){
|
|
|
|
if(row.length == 1){
|
|
|
|
console.log("Autorizzato (sessionid)");
|
|
|
|
var username = utils.rowToString(row, "username");
|
|
var rank = utils.rowToString(row, "rank");
|
|
|
|
switch(rank){
|
|
|
|
case "prof":
|
|
|
|
db.query("SELECT classes FROM prof_classes WHERE username = ?", [username], function(err, classes){
|
|
|
|
if(classes.length == 1){
|
|
|
|
db.query("SELECT writeup FROM class_writeups WHERE class = ?", [classname], function(err, writeups){
|
|
|
|
if(err){
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "fatal"
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
callback(err, response);
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
success: true,
|
|
list: writeups
|
|
|
|
}
|
|
|
|
console.log("Fatto!");
|
|
callback(null, response);
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("Non autorizzato (classe)");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "not_of_this_class"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
});
|
|
break;
|
|
|
|
case "stud":
|
|
db.query("SELECT class FROM users WHERE username = ?", [username], function(err, row){
|
|
|
|
if(err){
|
|
|
|
callback(err, response);
|
|
return false;
|
|
|
|
}
|
|
|
|
if(row.length == 1){
|
|
|
|
console.log("OK! Può leggere le note (studente)");
|
|
|
|
db.query("SELECT writeup FROM class_writeups WHERE class = ?", [classname], function(err, writeups){
|
|
|
|
if(err){
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "fatal"
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
callback(err, response);
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
success: true,
|
|
list: writeups
|
|
|
|
}
|
|
|
|
console.log("Fatto!");
|
|
callback(null, response);
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("Non autorizzato (classe)");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "Unauthorized"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
});
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
console.log("sessionid non autorizzato!");
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "Unauthorized"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
module.exports.getWriteUps = function(sessionid, student, response, callback){
|
|
|
|
db.query("SELECT username, rank FROM sessions WHERE session_id = ?", [sessionid], function(err, row){
|
|
|
|
if(row.length == 1){
|
|
|
|
console.log("Autorizzato (sessionid)");
|
|
|
|
var username = utils.rowToString(row, "username");
|
|
var rank = utils.rowToString(row, "rank");
|
|
|
|
console.log(jRow);
|
|
|
|
switch(rank){
|
|
|
|
case "prof":
|
|
|
|
db.query("SELECT classes FROM prof_classes WHERE username = ?", [username], function(err, classes){
|
|
|
|
if(classes.length == 1){
|
|
|
|
db.query("SELECT writeup FROM personal_writeups WHERE username = ?", [student], function(err, writeups){
|
|
|
|
if(err){
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "fatal"
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
callback(err, response);
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
success: true,
|
|
list: writeups
|
|
|
|
}
|
|
|
|
console.log("Fatto!");
|
|
callback(null, response);
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("Non autorizzato (classe)");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "not_of_this_class"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
});
|
|
break;
|
|
|
|
case "stud":
|
|
db.query("SELECT class FROM users WHERE username = ?", [username], function(err, row){
|
|
|
|
if(err){
|
|
|
|
callback(err, response);
|
|
return false;
|
|
|
|
}
|
|
|
|
if(row.length == 1){
|
|
|
|
console.log("OK! Può leggere le note (studente)");
|
|
|
|
db.query("SELECT writeup FROM personal_writeups WHERE username = ?", [student], function(err, writeups){
|
|
|
|
if(err){
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "fatal"
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
callback(err, response);
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
success: true,
|
|
list: writeups
|
|
|
|
}
|
|
|
|
console.log("Fatto!");
|
|
callback(null, response);
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("Non autorizzato (classe)");
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "Unauthorized"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
});
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
console.log("sessionid non autorizzato!");
|
|
|
|
response = {
|
|
|
|
success: false,
|
|
errordesc: "Unauthorized"
|
|
|
|
}
|
|
|
|
callback(null, response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|