mirror of
https://github.com/janishutz/libreevent.git
synced 2025-11-25 13:24:24 +00:00
seatplan saving and loading for admin & user
This commit is contained in:
43
src/server/admin/adminAPIRoutes.js
Normal file
43
src/server/admin/adminAPIRoutes.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* libreevent - adminAPIRoutes.js
|
||||
*
|
||||
* Created by Janis Hutz 07/20/2023, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
const posth = require( './api/postHandler.js' );
|
||||
const geth = require( './api/getHandler.js' );
|
||||
const postHandler = new posth();
|
||||
const getHandler = new geth();
|
||||
const path = require( 'path' );
|
||||
|
||||
// settings is missing in arguments which shouldn't pose any problem
|
||||
module.exports = ( app ) => {
|
||||
// Add specific routes here to have them be checked first to not get general handling
|
||||
|
||||
app.get( '/admin/getAPI/:call', ( req, res ) => {
|
||||
if ( req.session.loggedInAdmin ) {
|
||||
getHandler.handleCall( req.params.call, req.query ).then( data => {
|
||||
res.send( data );
|
||||
} ).catch( error => {
|
||||
res.status( 500 ).send( error );
|
||||
} );
|
||||
} else {
|
||||
res.status( 403 ).sendFile( path.join( __dirname + '/../ui/' + ( req.query.lang ?? 'en' ) + '/errors/403.html' ) );
|
||||
}
|
||||
} );
|
||||
|
||||
app.post( '/admin/API/:call', ( req, res ) => {
|
||||
if ( req.session.loggedInAdmin ) {
|
||||
postHandler.handleCall( req.params.call, req.body, req.query.lang ).then( data => {
|
||||
res.send( data );
|
||||
} ).catch( error => {
|
||||
res.status( 500 ).send( error );
|
||||
} );
|
||||
} else {
|
||||
res.status( 403 ).sendFile( path.join( __dirname + '/../ui/' + ( req.query.lang ?? 'en' ) + '/errors/403.html' ) );
|
||||
}
|
||||
} );
|
||||
};
|
||||
48
src/server/admin/api/getHandler.js
Normal file
48
src/server/admin/api/getHandler.js
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* libreevent - getHandler.js
|
||||
*
|
||||
* Created by Janis Hutz 07/20/2023, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
const db = require( '../../backend/db/db.js' );
|
||||
|
||||
class GETHandler {
|
||||
constructor () {
|
||||
|
||||
}
|
||||
|
||||
handleCall ( call, query ) {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
if ( call === 'getSeatplan' ) {
|
||||
db.getJSONDataSimple( 'seatplan', query.location ).then( data => {
|
||||
if ( Object.keys( data ).length > 0 ) {
|
||||
resolve( data[ 'save' ] );
|
||||
} else {
|
||||
reject( 'No data found for this location' );
|
||||
}
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
} else if ( call === 'getSeatplanDraft' ) {
|
||||
db.getJSONDataSimple( 'seatplan', query.location ).then( data => {
|
||||
if ( Object.keys( data ).length > 0 ) {
|
||||
if ( Object.keys( data[ 'draft' ] ).length > 0 ) {
|
||||
resolve( data[ 'draft' ] );
|
||||
} else {
|
||||
resolve( data[ 'save' ] );
|
||||
}
|
||||
} else {
|
||||
reject( 'No data found for this location' );
|
||||
}
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GETHandler;
|
||||
41
src/server/admin/api/postHandler.js
Normal file
41
src/server/admin/api/postHandler.js
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* libreevent - postHandler.js
|
||||
*
|
||||
* Created by Janis Hutz 07/20/2023, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
const db = require( '../../backend/db/db.js' );
|
||||
|
||||
class POSTHandler {
|
||||
constructor () {
|
||||
|
||||
}
|
||||
|
||||
handleCall ( call, data, lang ) {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
console.log( lang );
|
||||
if ( call === 'saveSeatplanDraft' ) {
|
||||
db.getJSONDataSimple( 'seatplan', data.location ).then( res => {
|
||||
let dat = res;
|
||||
dat[ 'draft' ] = data.data;
|
||||
db.writeJSONDataSimple( 'seatplan', data.location, dat ).then( resp => {
|
||||
resolve( resp );
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
} );
|
||||
} else if ( call === 'saveSeatplan' ) {
|
||||
db.writeJSONDataSimple( 'seatplan', data.location, { 'draft': {}, 'save': data.data } ).then( resp => {
|
||||
resolve( resp );
|
||||
} ).catch( error => {
|
||||
reject( error );
|
||||
} );
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = POSTHandler;
|
||||
Reference in New Issue
Block a user