241 lines
5.6 KiB
JavaScript
241 lines
5.6 KiB
JavaScript
// SQL-Class
|
|
// Author: Thomas Mack
|
|
|
|
const DEBUG = true;
|
|
|
|
const { Pool } = require('pg');
|
|
const pool = new Pool({
|
|
user: 'postgres',
|
|
host: 'localhost',
|
|
database: 'bidat_cookbook',
|
|
password: 'postgres',
|
|
port: 5432
|
|
});
|
|
|
|
|
|
const query = function (text, params, callback) {
|
|
const start = Date.now()
|
|
return pool.query(text, params, (err, res) => {
|
|
const duration = Date.now() - start;
|
|
//if(res)
|
|
if(DEBUG) console.log('executed query', { text, duration})
|
|
callback(err, res)
|
|
})
|
|
}
|
|
const getClient = function (callback) {
|
|
pool.connect((err, client, done) => {
|
|
callback(err, client, done)
|
|
})
|
|
}
|
|
|
|
const dataStructure = {
|
|
|
|
categories: {
|
|
tableName: "kategorie",
|
|
jsonName: "categories",
|
|
displayName: "Kategorien"
|
|
},
|
|
|
|
units: {
|
|
tableName: "einheit",
|
|
jsonName: "units",
|
|
displayName: "Einheiten"
|
|
},
|
|
|
|
tools: {
|
|
tableName: "geraet",
|
|
jsonName: "tools",
|
|
displayName: "Geräte und Sonstiges"
|
|
},
|
|
|
|
difficulties: {
|
|
tableName: "schwierigkeit",
|
|
jsonName: "difficulties",
|
|
displayName: "Schwierigkeit"
|
|
},
|
|
|
|
ingredients: {
|
|
tableName: "zutat",
|
|
jsonName: "ingredients",
|
|
displayName: "Zutaten"
|
|
},
|
|
|
|
ratingcategories: {
|
|
tableName: "wertungkategorie",
|
|
jsonName: "ratingcategories",
|
|
displayName: "Wertungskategorien"
|
|
}
|
|
|
|
}
|
|
|
|
const selectAllFromTable = (table) => {
|
|
return "SELECT * FROM "+table+" order by name;";
|
|
}
|
|
|
|
const selectAllFromTableByFieldValue = (table, field, value) => {
|
|
return "SELECT * FROM "+table+" WHERE "+field+"="+value+";";
|
|
}
|
|
|
|
const selectItemFromTable = function (table, id) {
|
|
return "SELECT * FROM "+table+" WHERE id = "+id+";";
|
|
}
|
|
|
|
const deleteItemFromTable = function (table, id) {
|
|
return "DELETE FROM "+table+" WHERE id = "+id+";";
|
|
}
|
|
|
|
const deleteNameKeyObject = function(req, res, next) {
|
|
if(DEBUG) console.log("Delte Valuelist");
|
|
let structure = getStructure(req);
|
|
let obj = req.body[structure.jsonName][0];
|
|
let query = "DELETE FROM "+structure.tableName+
|
|
" WHERE id = "+req.itemId;
|
|
pool.query(query, (err, rs) => {
|
|
if(err) {
|
|
console.log(query);
|
|
next(err)
|
|
} else {
|
|
res.status(204).send();
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
const updateNameKeyObject = function(req, res, next) {
|
|
if(DEBUG) console.log("Update Valuelist");
|
|
let structure = getStructure(req);
|
|
if(DEBUG) console.log(req.body[structure.jsonName]);
|
|
let obj = req.body['item'];
|
|
if(req.body[structure.jsonName]) {
|
|
obj = req.body[structure.jsonName][0];
|
|
}
|
|
if(!obj.beschreibung) {
|
|
obj.beschreibung = "";
|
|
}
|
|
|
|
let query = "UPDATE "+structure.tableName+
|
|
" set schluessel = '"+obj.schluessel+
|
|
"', name = '"+obj.name+
|
|
"', beschreibung = '"+obj.beschreibung+
|
|
"' WHERE id = "+req.itemId+" returning *";
|
|
pool.query(query, (err, rs) => {
|
|
if(err) {
|
|
console.log(query);
|
|
next(err)
|
|
} else {
|
|
let result = {};
|
|
result[structure['jsonName']] = rs.rows;
|
|
res.status(200).json(result);
|
|
}
|
|
});
|
|
}
|
|
|
|
const insertNameKeyObject = function(req, res, next) {
|
|
if(DEBUG) console.log("Insert Valuelist");
|
|
let structure = getStructure(req);
|
|
if(DEBUG) console.log(req.body[structure.jsonName]);
|
|
let obj = req.body[structure.jsonName][0];
|
|
|
|
let query = "INSERT INTO "+structure.tableName+" (schluessel, name, beschreibung) "+
|
|
" VALUES ('"+obj.schluessel+"','"+obj.name+"','"+obj.beschreibung+"')" +
|
|
" returning *";
|
|
if(DEBUG) console.log(query);
|
|
pool.query(query, (err, rs) => {
|
|
if(err) {
|
|
console.log(query);
|
|
next(err)
|
|
} else {
|
|
let result = {};
|
|
result[structure['jsonName']] = rs.rows;
|
|
if(DEBUG) console.log(result);
|
|
res.status(200).json(result);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
const getStructure = function(req) {
|
|
let path = req.pathCall;
|
|
let structure = dataStructure[path];
|
|
return structure;
|
|
|
|
}
|
|
|
|
const getDataStructure = function(req, res, next) {
|
|
let structure = dataStructure;
|
|
res.status(200).json(structure);
|
|
return structure;
|
|
|
|
|
|
}
|
|
|
|
|
|
const getAllFromTable = function(req, res, next) {
|
|
|
|
let structure = getStructure(req);
|
|
if(DEBUG) {
|
|
console.log("Found Structure-Object: ");
|
|
console.log(structure);
|
|
}
|
|
if(structure === undefined) return res.status(404).send();
|
|
pool.query(selectAllFromTable(structure.tableName), (err, rs) => {
|
|
if(err) next(err)
|
|
else {
|
|
if(DEBUG) console.log(res);
|
|
let result = {};
|
|
result[structure['jsonName']] = rs.rows;
|
|
console.log(result);
|
|
res.status(200).json(result);
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
const getItemFromTable = function(req, res, next) {
|
|
|
|
let structure = getStructure(req);
|
|
if(DEBUG) {
|
|
console.log("Found Structure-Object for WL-Item: ");
|
|
console.log(structure);
|
|
console.log(structure['tableName']);
|
|
}
|
|
if(structure === undefined) return res.status(404).send();
|
|
pool.query(selectItemFromTable(structure['tableName'], req.itemId), (err, rs) => {
|
|
if(err) next(err)
|
|
else {
|
|
if(DEBUG) console.log(res);
|
|
let result = {};
|
|
result = rs.rows[0];
|
|
console.log(result);
|
|
res.status(200).json(result);
|
|
}
|
|
})
|
|
}
|
|
|
|
const getUserByEmail = function(username) {
|
|
let query = "Select id, name, email, kennwort, beschreibung FROM nutzer WHERE email ilike '"+username+"'";
|
|
if(DEBUG) console.log(query);
|
|
req.db.query(query, (err, rs) => {
|
|
if (err) {
|
|
return err
|
|
}
|
|
return rs.rows[0];
|
|
})
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
query,
|
|
selectAllFromTable,
|
|
selectAllFromTableByFieldValue,
|
|
deleteItemFromTable,
|
|
updateNameKeyObject,
|
|
insertNameKeyObject,
|
|
deleteNameKeyObject,
|
|
getAllFromTable,
|
|
getItemFromTable,
|
|
getUserByEmail,
|
|
getStructure,
|
|
getDataStructure
|
|
};
|