diff --git a/app.js b/app.js index b9c5bf4..9f073b3 100644 --- a/app.js +++ b/app.js @@ -17,6 +17,7 @@ const dbhog = require('./dbhog') const PORT = process.env.PORT || 4000; + var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); var valuelistRouter = require('./routes/valuelist'); @@ -39,19 +40,6 @@ var corsOptions = { methods: ['GET', 'PUT', 'POST', 'DELETE', 'COPY'] } app.use(cors(corsOptions)); - -// Setting up passport - -/*passport.use( - new LocalStrategy( - function(username, password, done) { - - } - ); - -); -*/ - app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); @@ -66,16 +54,17 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', (req, res, next ) => { req.db = db; next(); -}) +}); app.use('/api/v1', (req, res, next ) => { req.db = dbhog; next(); -}) +}); app.use('/', indexRouter); app.use('/users', usersRouter); app.use('/wl', valuelistRouter); +app.use('/vl', valuelistRouter); app.use('/recepies', recepieRouter); app.use('/api/v1/person', personRouter); @@ -85,6 +74,7 @@ app.use('/api/v1/punkte', punkteRouter); app.use('/api/v1/haus', hausRouter); + // catch 404 and forward to error handler app.use(function(req, res, next) { next(createError(404)); diff --git a/db/index.js b/db/index.js index 37ca77a..311b2e9 100644 --- a/db/index.js +++ b/db/index.js @@ -32,32 +32,38 @@ const dataStructure = { categories: { tableName: "kategorie", - jsonName: "categories" + jsonName: "categories", + displayName: "Kategorien" }, units: { tableName: "einheit", - jsonName: "units" + jsonName: "units", + displayName: "Einheiten" }, tools: { tableName: "geraet", - jsonName: "tools" + jsonName: "tools", + displayName: "Geräte und Sonstiges" }, difficulties: { tableName: "schwierigkeit", - jsonName: "difficulties" + jsonName: "difficulties", + displayName: "Schwierigkeit" }, ingredients: { tableName: "zutat", - jsonName: "ingredients" + jsonName: "ingredients", + displayName: "Zutaten" }, ratingcategories: { tableName: "wertungkategorie", - jsonName: "ratingcategories" + jsonName: "ratingcategories", + displayName: "Wertungskategorien" } } @@ -99,7 +105,14 @@ 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[structure.jsonName][0]; + 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+ @@ -148,6 +161,15 @@ const getStructure = function(req) { } +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); @@ -168,6 +190,28 @@ const getAllFromTable = function(req, res, next) { }) } + +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); @@ -189,6 +233,8 @@ module.exports = { insertNameKeyObject, deleteNameKeyObject, getAllFromTable, + getItemFromTable, getUserByEmail, - getStructure + getStructure, + getDataStructure }; diff --git a/routes/recepie.js b/routes/recepie.js index 732feaf..70f15ae 100644 --- a/routes/recepie.js +++ b/routes/recepie.js @@ -72,7 +72,7 @@ router.get('/', function(req, res, next) { "%' OR rezept.beschreibung ilike '%"+req.query.term+"%' "+ " OR rezept.id in (select rezept from rezept_zutat where zutat in (select id from zutat where name ilike '%"+req.query.term+"%'))"; if(req.query.hashtag) query += " AND rezept.schluessel ilike '"+req.query.hashtag+"' " - if(req.query.cat) query += " AND kategorie.id ="+req.query.cat+" " + if(req.query.cat) query += " AND kategorie.id IN ("+req.query.cat+")" diff --git a/routes/valuelist.js b/routes/valuelist.js index 3345319..6343322 100644 --- a/routes/valuelist.js +++ b/routes/valuelist.js @@ -10,7 +10,9 @@ updateNameKeyObject, insertNameKeyObject, deleteNameKeyObject, getAllFromTable, -getStructure} = require('./../db/'); +getItemFromTable, +getStructure, +getDataStructure} = require('./../db/'); @@ -28,11 +30,25 @@ router.param('path', function(req, res, next, id) { next(); }); +/* OPTIONS */ + +router.get('/options', function(req, res, next) { + getDataStructure(req, res, next); +}); + + + /* GET users listing. */ router.get('/:path', function(req, res, next) { getAllFromTable(req, res, next) }); +router.get('/:path/:id', function(req, res, next) { + if (DEBUG) console.log("Get Item from WL"); + getItemFromTable(req, res, next) +}); + + router.put('/:path/:id', function(req, res, next) { if (DEBUG) console.log("Update WL"); updateNameKeyObject(req, res, next)