db + saving of reserved tickets

This commit is contained in:
2023-07-26 12:24:18 +02:00
parent d0b215f022
commit ee8aa52dd8
6 changed files with 107 additions and 25 deletions

View File

@@ -14,22 +14,46 @@ class POSTHandler {
} }
handleCall ( call, data, lang ) { handleCall ( call, data, lang, session ) {
return new Promise( ( resolve, reject ) => { return new Promise( ( resolve, reject ) => {
console.log( lang ); console.log( lang );
if ( call === 'saveSeatplanDraft' ) { if ( call === 'reserveTicket' ) {
db.getJSONDataSimple( 'seatplan', data.location ).then( res => { db.getDataSimple( 'temp', 'user_id', session.id ).then( dat => {
let dat = res; let transmit = {};
dat[ 'draft' ] = data.data; if ( dat.length > 0 ) {
db.writeJSONDataSimple( 'seatplan', data.location, dat ).then( resp => { transmit = JSON.parse( dat[ 0 ].data );
resolve( resp ); } else {
transmit[ data.eventID ] = {};
}
transmit[ data.eventID ][ data.id ] = data;
db.writeDataSimple( 'temp', 'user_id', session.id, { 'user_id': session.id, 'data': JSON.stringify( transmit ), 'timestamp': new Date().toString() } ).then( ret => {
resolve( ret );
} ).catch( error => { } ).catch( error => {
reject( error ); reject( error );
} ); } );
} ).catch( error => {
reject( error );
} ); } );
} else if ( call === 'saveSeatplan' ) { } else if ( call === 'deselectTicket' ) {
db.writeJSONDataSimple( 'seatplan', data.location, { 'draft': {}, 'save': data.data } ).then( resp => { db.getDataSimple( 'temp', 'user_id', session.id ).then( dat => {
resolve( resp ); let transmit = JSON.parse( dat[ 0 ].data );
if ( transmit[ data.eventID ] ) {
if ( transmit[ data.eventID ][ data.id ] ) {
delete transmit[ data.eventID ][ data.id ];
} else {
reject( 'ERR_DATA_NONE_EXISTENT' );
}
if ( Object.keys( transmit[ data.eventID ] ).length < 1 ) {
delete transmit[ data.eventID ];
}
} else {
reject( 'ERR_DATA_NONE_EXISTENT' );
}
db.writeDataSimple( 'temp', 'user_id', session.id, { 'user_id': session.id, 'data': JSON.stringify( transmit ) } ).then( ret => {
resolve( ret );
} ).catch( error => {
reject( error );
} );
} ).catch( error => { } ).catch( error => {
reject( error ); reject( error );
} ); } );

View File

@@ -12,7 +12,7 @@ const fs = require( 'fs' );
const settings = JSON.parse( fs.readFileSync( path.join( __dirname + '/../../config/settings.config.json' ) ) ); const settings = JSON.parse( fs.readFileSync( path.join( __dirname + '/../../config/settings.config.json' ) ) );
const dbRef = { 'user': 'libreevent_users', 'admin': 'libreevent_admin', 'order': 'libreevent_orders' }; const dbRef = { 'user': 'libreevent_users', 'admin': 'libreevent_admin', 'order': 'libreevent_orders', 'users': 'libreevent_users', 'orders': 'libreevent_orders', 'temp': 'libreevent_temp' };
let dbh; let dbh;
@@ -27,19 +27,35 @@ if ( settings.db === 'mysql' ) {
} }
module.exports.getDataSimple = ( db, column, searchQuery ) => { module.exports.getDataSimple = ( db, column, searchQuery ) => {
return new Promise( resolve => { return new Promise( ( resolve, reject ) => {
dbh.query( { 'command': 'getFilteredData', 'property': column, 'searchQuery': searchQuery }, dbRef[ db ] ).then( data => { dbh.query( { 'command': 'getFilteredData', 'property': column, 'searchQuery': searchQuery }, dbRef[ db ] ).then( data => {
console.log( data ); resolve( data );
} ).catch( error => { } ).catch( error => {
console.error( error ); reject( error );
} ); } );
resolve( '$2b$05$ElMYWoMjk7567lXkIkee.e.6cxCrWU4gkfuNLB8gmGYLQQPm7gT3O' ); // resolve( '$2b$05$ElMYWoMjk7567lXkIkee.e.6cxCrWU4gkfuNLB8gmGYLQQPm7gT3O' );
} ); } );
}; };
module.exports.writeDataSimple = ( db, column, searchQuery ) => { module.exports.writeDataSimple = ( db, column, searchQuery, data ) => {
return new Promise( ( resolve, reject ) => { return new Promise( ( resolve, reject ) => {
dbh.query( { 'command': 'checkDataAvailability', 'property': column, 'searchQuery': searchQuery }, dbRef[ db ] ).then( res => {
if ( res.length > 0 ) {
dbh.query( { 'command': 'updateData', 'property': column, 'searchQuery': searchQuery, 'newValues': data }, dbRef[ db ] ).then( dat => {
resolve( dat );
} ).catch( error => {
reject( error );
} );
} else {
dbh.query( { 'command': 'addData', 'data': data }, dbRef[ db ] ).then( dat => {
resolve( dat );
} ).catch( error => {
reject( error );
} );
}
} ).catch( error => {
reject( error );
} );
} ); } );
}; };

View File

@@ -61,8 +61,9 @@ class SQLDB {
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error; if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
this.sqlConnection.query( 'CREATE TABLE libreevent_admin ( account_id INT NOT NULL AUTO_INCREMENT, email TINYTEXT, pass TEXT, permissions VARCHAR( 1000 ), PRIMARY KEY ( account_id ) );', ( error ) => { this.sqlConnection.query( 'CREATE TABLE libreevent_admin ( account_id INT NOT NULL AUTO_INCREMENT, email TINYTEXT, pass TEXT, permissions VARCHAR( 1000 ), PRIMARY KEY ( account_id ) );', ( error ) => {
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error; if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
this.sqlConnection.query( 'CREATE TABLE libreevent_temp ( entry_id INT NOT NULL AUTO_INCREMENT, user_id TINYTEXT, pass TEXT, data VARCHAR( 60000 ), PRIMARY KEY ( entry_id ) );', ( error ) => { this.sqlConnection.query( 'CREATE TABLE libreevent_temp ( entry_id INT NOT NULL AUTO_INCREMENT, user_id TINYTEXT, data VARCHAR( 60000 ), timestamp TINYTEXT, PRIMARY KEY ( entry_id ) );', ( error ) => {
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error; if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
return 'DONE';
} ); } );
} ); } );
} ); } );
@@ -101,8 +102,7 @@ class SQLDB {
- operation.matchingParam (Which properties should be matched to get the data, e.g. order.user_id=users.id) - operation.matchingParam (Which properties should be matched to get the data, e.g. order.user_id=users.id)
- addData: - addData:
- operation.columns (the columns into which the data should be inserted (as a space separated string)) - operation.data (key-value pair with all data as values and column to insert into as key)
- operation.values (the data to be inserted into the columns selected before (as a space separated string))
- updateData: - updateData:
- operation.newValues (a object with keys being the column and value being the value to be inserted into that column, values are being - operation.newValues (a object with keys being the column and value being the value to be inserted into that column, values are being
@@ -125,14 +125,22 @@ class SQLDB {
} else if ( operation.command === 'fullCustomCommand' ) { } else if ( operation.command === 'fullCustomCommand' ) {
command = operation.query; command = operation.query;
} else if ( operation.command === 'addData' ) { } else if ( operation.command === 'addData' ) {
command = 'INSERT INTO ' + table + ' (' + operation.columns + ') VALUES (' + this.sqlConnection.escape( operation.values ) + ');'; let keys = '';
let values = '';
for ( let key in operation.data ) {
keys += String( key ) + ', ';
values += this.sqlConnection.escape( String( operation.data[ key ] ) ) + ', ' ;
}
command = 'INSERT INTO ' + table + ' (' + keys.slice( 0, keys.length - 2 ) + ') VALUES (' + values.slice( 0, values.length - 2 ) + ');';
} else if ( operation.command === 'updateData' ) { } else if ( operation.command === 'updateData' ) {
if ( !operation.property || !operation.searchQuery ) reject( 'Refusing to run destructive command: Missing Constraints' ); if ( !operation.property || !operation.searchQuery ) reject( 'Refusing to run destructive command: Missing Constraints' );
else { else {
command = 'UPDATE ' + table + ' SET '; command = 'UPDATE ' + table + ' SET ';
let updatedValues = '';
for ( let value in operation.newValues ) { for ( let value in operation.newValues ) {
command += value + ' = ' + this.sqlConnection.escape( operation.newValues[ value ] ); updatedValues += value + ' = ' + this.sqlConnection.escape( operation.newValues[ value ] ) + ', ';
} }
command += updatedValues.slice( 0, updatedValues.length - 2 );
command += ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery ); command += ' WHERE ' + operation.property + ' = ' + this.sqlConnection.escape( operation.searchQuery );
} }
} else if ( operation.command === 'deleteData' ) { } else if ( operation.command === 'deleteData' ) {

View File

@@ -26,7 +26,7 @@ module.exports = ( app ) => {
} ); } );
app.post( '/API/:call', ( req, res ) => { app.post( '/API/:call', ( req, res ) => {
postHandler.handleCall( req.params.call, req.body, req.query.lang ).then( data => { postHandler.handleCall( req.params.call, req.body, req.query.lang, req.session ).then( data => {
res.send( data ); res.send( data );
} ).catch( error => { } ).catch( error => {
res.status( 500 ).send( error ); res.status( 500 ).send( error );

6
src/server/prepareDB.js Normal file
View File

@@ -0,0 +1,6 @@
const sql = require( './backend/db/mysqldb.js' );
const sqlDB = new sql();
sqlDB.connect();
// sqlDB.resetDB();
sqlDB.setupDB();

View File

@@ -317,13 +317,41 @@
if ( option.status == 'ok' ) { if ( option.status == 'ok' ) {
this.$refs[ 'component' + this.selectedSeat.componentID ][ 0 ].validateSeatSelection( this.selectedSeat, option.data ); this.$refs[ 'component' + this.selectedSeat.componentID ][ 0 ].validateSeatSelection( this.selectedSeat, option.data );
this.cartHandling( 'select', option.data ); this.cartHandling( 'select', option.data );
// Make call to server to reserve ticket to have server also keep track of reserved tickets
const options = {
method: 'post',
body: JSON.stringify( { 'id': this.selectedSeat[ 'id' ], 'component': this.selectedSeat[ 'componentID' ], 'ticketOption': option.data, 'eventID': this.event.name } ),
headers: {
'Content-Type': 'application/json',
'charset': 'utf-8'
}
};
fetch( localStorage.getItem( 'url' ) + '/API/reserveTicket', options ).then( res => {
res.text().then( text => {
console.log( text );
} );
} );
} }
// TODO: Make call to server to reserve ticket when data is returned
}, },
seatDeselected ( seat ) { seatDeselected ( seat ) {
this.selectedSeat = seat; this.selectedSeat = seat;
this.cartHandling( 'deselect' ); this.cartHandling( 'deselect' );
// TODO: Make call to server to deselect ticket
// Make call to server to deselect ticket
const options = {
method: 'post',
body: JSON.stringify( { 'id': seat[ 'id' ], 'eventID': this.event.name } ),
headers: {
'Content-Type': 'application/json',
'charset': 'utf-8'
}
};
fetch( localStorage.getItem( 'url' ) + '/API/deselectTicket', options ).then( res => {
res.text().then( text => {
console.log( text );
} );
} );
}, },
standing ( id ) { standing ( id ) {
const d = this.draggables[ id ]; const d = this.draggables[ id ];