Add panel for non-admins to edit their own games

main
sudoer777 2021-11-25 19:21:21 -07:00
parent 3515be836d
commit bd5b393a27
7 changed files with 54 additions and 12 deletions

View File

@ -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({
usernameField: 'email',
passwordField: 'password'},

View File

@ -27,13 +27,13 @@ async function Initialize() {
async function checkForDatabaseInitialization() {
const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`;
let result = await executeQuery(query);
const scoresSchemaExistsQuery = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`;
let result = await executeQuery(scoresSchemaExistsQuery);
const scoresSchemaExists = result.length !== 0;
if(!scoresSchemaExists) {
Initialize();
await Initialize();
}
}
checkForDatabaseInitialization();

View File

@ -19,12 +19,12 @@ class Game {
async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) {
const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score)
VALUES($1, $2, $3, $4, $5, $6, $7)
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, submitter_id)
VALUES($1, $2, $3, $4, $5, $6, $7, $8)
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);
}
@ -71,6 +71,20 @@ async function retrieve(teamID, divisionID, seasonID) {
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) {
const query = `UPDATE scores.games
SET division_id = $2,
@ -100,5 +114,6 @@ async function getFromID(gameID) {
exports.add = add;
exports.remove = remove;
exports.retrieve = retrieve;
exports.retrieveByUser = retrieveByUser;
exports.edit = edit;
exports.getFromID = getFromID;

View File

@ -65,6 +65,13 @@ export async function getGames(teamID = undefined, divisionID = undefined, seaso
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) {
const response = await fetch(`/data/game?game=${gameID}`);
const game = await response.json();

View File

@ -97,7 +97,7 @@ async function editGame(id) {
async function listItems() {
const gamesList = await Data.getGames();
const gamesList = await Data.getGamesByUser();
await listGameHeaders();

View File

@ -62,8 +62,9 @@ router.get('/team', function(req, res, next) {
})
router.get('/games', function(req, res, next) {
games.retrieve(req.query.team, req.query.division, req.query.season)
.then(data => res.json(data));
const userID = req.user[0];
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) {

View File

@ -31,7 +31,7 @@ function adminLoggedIn(req, res, next) {
router.get('/' ,userLoggedIn, function(req, res, next) {
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) {
@ -50,6 +50,7 @@ router.post('/game', userLoggedIn, function(req, res, next) {
const team1Score = req.body['team1-score'];
const team2ID = req.body['team2'];
const team2Score = req.body['team2-score'];
const userID = req.user[0];
const id = req.body['game'];
const remove = req.body['remove'];
@ -58,7 +59,7 @@ router.post('/game', userLoggedIn, function(req, res, next) {
.then(res.redirect("/manage"));
else if(id) games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score)
.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"));
});