1
0
mirror of https://dev.s-ul.net/Galexion/MaiMaiDXNet.git synced 2024-11-24 06:40:14 +01:00
MaiMaiDXNet/routes/index.js
2023-05-07 01:20:04 -04:00

70 lines
2.4 KiB
JavaScript

var express = require('express');
var router = express.Router();
var fetch = require('cross-fetch');
const sqlite3 = require('sqlite3').verbose();
var config = require('../config.json');
let db = new sqlite3.Database(config.DXMemorialImageDirectory + '\\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);
res.render('error', { error: err });
} 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
}
res.render('index', { title: title, params: params });
}
});
});
router.get('/user', async function (req, res, next) {
try{
const cookies = req.cookies
console.log(cookies)
if (cookies.aime_card_id === undefined) {
return res.redirect("/")
}
res.render('user', { title: title, userdata: await getUserData(req) });
} catch (err) {
res.render('error', { error: err });
}
});
router.get('/error/test/db', function (req,res,next) {
res.render('error', { error: "Error: SQLITE_BUSY: database is locked"})
})
async function getUserData(req) {
return new Promise((resolve, reject) => {
const cookies = req.cookies
db.all('SELECT * FROM maimai2_user_detail', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
reject(err)
} else {
// 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'));
}
}
});
});
} // function is required to obtain the User Data, and create the user profile. I'm going to bed.
module.exports = router;