Add panel for non-admins to edit their own games
parent
3515be836d
commit
bd5b393a27
|
@ -12,6 +12,24 @@ class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function checkForAdminAccount() {
|
||||||
|
|
||||||
|
const adminUsersQuery = `SELECT *
|
||||||
|
FROM accounts.users
|
||||||
|
WHERE admin = true;`;
|
||||||
|
const adminUsers = await database.executeQuery(adminUsersQuery);
|
||||||
|
|
||||||
|
if(adminUsers.length == 0) {
|
||||||
|
const passwordHash = await generateHash('admin');
|
||||||
|
const createTempAdminQuery = `INSERT INTO accounts.users(email, password, admin)
|
||||||
|
VALUES('admin@example.com', $1, true);`;
|
||||||
|
database.executeQuery(createTempAdminQuery, [passwordHash]);
|
||||||
|
console.log("Created temp admin account 'admin@example.com' with password 'admin'.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
checkForAdminAccount();
|
||||||
|
|
||||||
|
|
||||||
passport.use(new localStrategy({
|
passport.use(new localStrategy({
|
||||||
usernameField: 'email',
|
usernameField: 'email',
|
||||||
passwordField: 'password'},
|
passwordField: 'password'},
|
||||||
|
|
|
@ -27,13 +27,13 @@ async function Initialize() {
|
||||||
|
|
||||||
|
|
||||||
async function checkForDatabaseInitialization() {
|
async function checkForDatabaseInitialization() {
|
||||||
const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`;
|
const scoresSchemaExistsQuery = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`;
|
||||||
let result = await executeQuery(query);
|
let result = await executeQuery(scoresSchemaExistsQuery);
|
||||||
|
|
||||||
const scoresSchemaExists = result.length !== 0;
|
const scoresSchemaExists = result.length !== 0;
|
||||||
|
|
||||||
if(!scoresSchemaExists) {
|
if(!scoresSchemaExists) {
|
||||||
Initialize();
|
await Initialize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checkForDatabaseInitialization();
|
checkForDatabaseInitialization();
|
||||||
|
|
|
@ -19,12 +19,12 @@ class Game {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) {
|
async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID) {
|
||||||
const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score)
|
const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score, submitter_id)
|
||||||
VALUES($1, $2, $3, $4, $5, $6, $7)
|
VALUES($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
RETURNING game_id;`;
|
RETURNING game_id;`;
|
||||||
|
|
||||||
const id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]))[0][0];
|
const id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID]))[0][0];
|
||||||
return new Game(id, date, team1ID, team2ID, team1Score, team2Score);
|
return new Game(id, date, team1ID, team2ID, team1Score, team2Score);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +71,20 @@ async function retrieve(teamID, divisionID, seasonID) {
|
||||||
return gamesList;
|
return gamesList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function retrieveByUser(userID) {
|
||||||
|
const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score
|
||||||
|
FROM scores.games
|
||||||
|
WHERE submitter_id = $1
|
||||||
|
ORDER BY game_date DESC;`;
|
||||||
|
const table = await database.executeQuery(query, [userID]);
|
||||||
|
|
||||||
|
const gamesList = [];
|
||||||
|
table.forEach((row) => {
|
||||||
|
gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2]));
|
||||||
|
});
|
||||||
|
return gamesList;
|
||||||
|
}
|
||||||
|
|
||||||
async function edit(gameID, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) {
|
async function edit(gameID, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) {
|
||||||
const query = `UPDATE scores.games
|
const query = `UPDATE scores.games
|
||||||
SET division_id = $2,
|
SET division_id = $2,
|
||||||
|
@ -100,5 +114,6 @@ async function getFromID(gameID) {
|
||||||
exports.add = add;
|
exports.add = add;
|
||||||
exports.remove = remove;
|
exports.remove = remove;
|
||||||
exports.retrieve = retrieve;
|
exports.retrieve = retrieve;
|
||||||
|
exports.retrieveByUser = retrieveByUser;
|
||||||
exports.edit = edit;
|
exports.edit = edit;
|
||||||
exports.getFromID = getFromID;
|
exports.getFromID = getFromID;
|
|
@ -65,6 +65,13 @@ export async function getGames(teamID = undefined, divisionID = undefined, seaso
|
||||||
return gamesList;
|
return gamesList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getGamesByUser() {
|
||||||
|
let URL = '/data/games?user=1';
|
||||||
|
const response = await fetch(URL);
|
||||||
|
const gamesList = await response.json();
|
||||||
|
return gamesList;
|
||||||
|
}
|
||||||
|
|
||||||
export async function getGame(gameID) {
|
export async function getGame(gameID) {
|
||||||
const response = await fetch(`/data/game?game=${gameID}`);
|
const response = await fetch(`/data/game?game=${gameID}`);
|
||||||
const game = await response.json();
|
const game = await response.json();
|
||||||
|
|
|
@ -97,7 +97,7 @@ async function editGame(id) {
|
||||||
|
|
||||||
|
|
||||||
async function listItems() {
|
async function listItems() {
|
||||||
const gamesList = await Data.getGames();
|
const gamesList = await Data.getGamesByUser();
|
||||||
|
|
||||||
await listGameHeaders();
|
await listGameHeaders();
|
||||||
|
|
||||||
|
|
|
@ -62,8 +62,9 @@ router.get('/team', function(req, res, next) {
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/games', function(req, res, next) {
|
router.get('/games', function(req, res, next) {
|
||||||
games.retrieve(req.query.team, req.query.division, req.query.season)
|
const userID = req.user[0];
|
||||||
.then(data => res.json(data));
|
if(req.query.user) games.retrieveByUser(userID).then(data => res.json(data));
|
||||||
|
else games.retrieve(req.query.team, req.query.division, req.query.season).then(data => res.json(data));
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/game', function(req, res, next) {
|
router.get('/game', function(req, res, next) {
|
||||||
|
|
|
@ -31,7 +31,7 @@ function adminLoggedIn(req, res, next) {
|
||||||
|
|
||||||
router.get('/' ,userLoggedIn, function(req, res, next) {
|
router.get('/' ,userLoggedIn, function(req, res, next) {
|
||||||
if(req.user[2]) res.render('manage', { title: 'Score Management' });
|
if(req.user[2]) res.render('manage', { title: 'Score Management' });
|
||||||
else res.render('manage/manage-nonadmin', { title: "Manage Games" });
|
else res.render('manage/manage-nonadmin', { title: "My Games" });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/game', userLoggedIn, function(req, res, next) {
|
router.get('/game', userLoggedIn, function(req, res, next) {
|
||||||
|
@ -50,6 +50,7 @@ router.post('/game', userLoggedIn, function(req, res, next) {
|
||||||
const team1Score = req.body['team1-score'];
|
const team1Score = req.body['team1-score'];
|
||||||
const team2ID = req.body['team2'];
|
const team2ID = req.body['team2'];
|
||||||
const team2Score = req.body['team2-score'];
|
const team2Score = req.body['team2-score'];
|
||||||
|
const userID = req.user[0];
|
||||||
|
|
||||||
const id = req.body['game'];
|
const id = req.body['game'];
|
||||||
const remove = req.body['remove'];
|
const remove = req.body['remove'];
|
||||||
|
@ -58,7 +59,7 @@ router.post('/game', userLoggedIn, function(req, res, next) {
|
||||||
.then(res.redirect("/manage"));
|
.then(res.redirect("/manage"));
|
||||||
else if(id) games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score)
|
else if(id) games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score)
|
||||||
.then(res.redirect('/manage'));
|
.then(res.redirect('/manage'));
|
||||||
else games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score)
|
else games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID)
|
||||||
.then(res.redirect("/manage"));
|
.then(res.redirect("/manage"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Reference in New Issue