2023-02-17 00:50:32 +01:00
var express = require ( 'express' ) ;
var router = express . Router ( ) ;
var fetch = require ( 'cross-fetch' ) ;
const sqlite3 = require ( 'sqlite3' ) . verbose ( ) ;
let db = new sqlite3 . Database ( 'A:\\db.sqlite' ) ;
/* GET home page. */
var title = 'Webapp Mission Test'
router . get ( '/' , function ( req , res , next ) {
db . all ( 'SELECT * FROM sega_card' , ( err , rows ) => {
if ( err ) {
console . error ( err ) ;
2023-02-17 03:49:08 +01:00
res . render ( 'error' , { error : err } ) ;
2023-02-17 00:50:32 +01:00
} else {
console . log ( rows ) ;
console . log ( ` There are Currently ${ rows . length } Users Registered. ` ) ; // this check could probably be changed into an API
var params = {
totalUsers : rows . length
}
2023-02-17 03:49:08 +01:00
res . render ( 'index' , { title : title , params : params } ) ;
2023-02-17 00:50:32 +01:00
}
} ) ;
2023-02-17 03:49:08 +01:00
} ) ;
router . get ( '/user' , async function ( req , res , next ) {
try {
const cookies = req . cookies
console . log ( cookies )
if ( cookies . aime _card _id === undefined ) {
return res . render ( 'index' , { title : title } ) ;
}
res . render ( 'user' , { title : title , userdata : await getUserData ( req ) } ) ;
} catch ( err ) {
res . render ( 'error' , { error : err } ) ;
}
} ) ;
2023-02-18 11:19:30 +01:00
router . get ( '/error/test/db' , function ( req , res , next ) {
res . render ( 'error' , { error : "Error: SQLITE_BUSY: database is locked" } )
} )
2023-02-17 03:49:08 +01:00
async function getUserData ( req ) {
return new Promise ( ( resolve , reject ) => {
const cookies = req . cookies
db . all ( 'SELECT * FROM maimai2_user_detail' , ( err , rows ) => {
2023-02-17 00:50:32 +01:00
if ( err ) {
console . error ( err ) ;
2023-02-17 03:49:08 +01:00
// Return a Failed Message.
reject ( err )
2023-02-17 00:50:32 +01:00
} else {
2023-02-17 03:49:08 +01:00
// Make the Request easier to get to.
var mUser = undefined // Leave the Matched User undefined until the user is found.
for ( user of rows ) { // For Each User in Rows
if ( cookies . aime _card _id === user . aime _card _id . toString ( ) ) { // If the Access Code for the card is in the system,
mUser = user ; // set the Matched User Variable to the User, and break the for Loop.
break ;
}
}
if ( mUser ) {
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
resolve ( mUser ) ;
} else {
reject ( new Error ( 'User not found' ) ) ;
}
2023-02-17 00:50:32 +01:00
}
} ) ;
2023-02-17 03:49:08 +01:00
} ) ;
} // function is required to obtain the User Data, and create the user profile. I'm going to bed.
2023-02-17 00:50:32 +01:00
2023-02-17 03:49:08 +01:00
module . exports = router ;