You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.7 KiB
124 lines
3.7 KiB
import con from '../db/db.js';
|
|
import { Router } from "express";
|
|
import { AdminorUser } from '../server.js';
|
|
import { getAdminFromUser, getAdminID } from './adminQueries.js';
|
|
const router = Router();
|
|
|
|
// get all contests for specific Admin
|
|
router.post('/getContests', async (req,res) => {
|
|
const { email } = req.body;
|
|
|
|
// determine if email is admin or user
|
|
return AdminorUser(email).then(async (ans) => {
|
|
|
|
// if user then go into the user table and grab the foreign key for Admin
|
|
if (ans === 'Users') {
|
|
|
|
// get all the contests for that Admin
|
|
const Admin = await getAdminFromUser(email);
|
|
//if(!Admin || !Admin.AdminID) return res.status(404).json({error: 'Admin not found'});
|
|
const query = 'SELECT * FROM Contests WHERE AdminID = ?';
|
|
con.query(query, [Admin.AdminID], (err, rows) => {
|
|
if (err) {
|
|
console.error(err.message);
|
|
return res.status(500).json({ error: 'GETTING CONTESTS' });
|
|
}
|
|
if (rows.length === 0)
|
|
return res.status(404).json({ error: 'NO CONTESTS FOUND' });
|
|
return res.json(rows);
|
|
});
|
|
}
|
|
// if email is an Admin
|
|
else {
|
|
// get all contests for that Admin
|
|
const Admin_1 = await getAdminID(email);
|
|
const query_1 = 'SELECT * FROM Contests WHERE AdminID = ?';
|
|
con.query(query_1, [Admin_1.AdminID], (err_1, rows_1) => {
|
|
if (err_1) {
|
|
console.error(err_1.message);
|
|
return res.status(500).json({ error: 'GETTING CONTESTS' });
|
|
}
|
|
if (rows_1.length === 0)
|
|
return res.status(404).json({ error: 'NO CONTESTS FOUND' });
|
|
return res.json(rows_1);
|
|
});
|
|
}
|
|
}).catch((err) => {
|
|
console.error(err);
|
|
return res.status(500).json({ error: 'COULD NOT GET CONTESTS' });
|
|
});
|
|
});
|
|
|
|
router.post('/getContestByID', async (req, res) => {
|
|
const { contestID } = req.body;
|
|
if(!contestID)
|
|
return res.status(400).json({error: 'No Contest ID'});
|
|
try{
|
|
const contest = await getContestByID(contestID);
|
|
res.json(contest);
|
|
} catch(err){
|
|
if(err.message === 'Contest not found'){
|
|
return res.status(404).json({ error: 'Contest not found' });
|
|
}
|
|
else{
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
}
|
|
});
|
|
|
|
async function getContestByID(contestID){
|
|
return new Promise((resolve, reject) => {
|
|
const query = 'SELECT * FROM Contests WHERE ContestID = ?';
|
|
con.query(query, [contestID], (err, rows) => {
|
|
if(err) {
|
|
console.error(err.message);
|
|
reject(err);
|
|
}
|
|
if(rows.length === 0){
|
|
return reject(new Error('Contest not found'));
|
|
}
|
|
resolve(rows[0]);
|
|
});
|
|
});
|
|
}
|
|
|
|
// get the current active contest of an Admin
|
|
router.post('/getActiveContest', (req,res) => {
|
|
const { email } = req.body;
|
|
return AdminorUser(email).then((ans) => {
|
|
if (ans === 'Users') {
|
|
return getAdminFromUser(email).then((Admin) => {
|
|
const query = 'SELECT * FROM Contests WHERE IsActive = 1 AND AdminID = ?';
|
|
con.query(query, [Admin.AdminID], (err,rows) => {
|
|
if (err) {
|
|
console.error(err.message);
|
|
return res.status(500).json({ error: 'GETTING ACTIVE CONTEST' });
|
|
}
|
|
if (rows.length === 0)
|
|
return res.status(404).json({ error: 'NO CONTEST FOUND' });
|
|
return res.json(rows[0]);
|
|
})
|
|
});
|
|
}
|
|
else {
|
|
return getAdminID(email).then((Admin) => {
|
|
const query = 'SELECT * FROM Contests WHERE IsActive = 1 AND AdminID = ?';
|
|
con.query(query, [Admin.AdminID], (err,rows) => {
|
|
if (err) {
|
|
console.log('here');
|
|
console.error(err.message);
|
|
return res.status(500).json({ error: 'GETTING ACTIVE CONTEST' });
|
|
}
|
|
if(rows.length === 0) return res.status(404).json({ error: 'NO CONTEST FOUND' });
|
|
else return res.json(rows[0]);
|
|
});
|
|
});
|
|
}
|
|
}).catch((err) => {
|
|
console.error(err.message);
|
|
return res.status(500).json({ error: 'GETTING ACTIVE CONTEST' });
|
|
});
|
|
});
|
|
|
|
export default router; |