From 8ba04b5735b2c0e39832dd773662cc085df3d50b Mon Sep 17 00:00:00 2001 From: baz Date: Sun, 11 Feb 2024 19:19:43 +0000 Subject: [PATCH] Convert BeatenGames to LoggedGames --- convertDatabase.js | 2 +- databaseHelperFunctions.js | 16 ++++++++-------- dbObjects.js | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/convertDatabase.js b/convertDatabase.js index b65b3f5..ff4dd66 100644 --- a/convertDatabase.js +++ b/convertDatabase.js @@ -1,6 +1,6 @@ const { Sequelize, DataTypes } = require('sequelize'); require('./dbObjects.js'); -const { sequelize, BeatenGames } = require ('./dbObjects.js'); +const { sequelize, LoggedGames } = require ('./dbObjects.js'); // Add a new column to the existing table (async () => { diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index 52caa65..134cc74 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -1,4 +1,4 @@ -const { Users, Games, BeatenGames } = require ('./dbObjects.js'); +const { Users, Games, LoggedGames } = require ('./dbObjects.js'); const fs = require('fs'); async function checkUserRegistration(user) { @@ -68,14 +68,14 @@ async function checkGameStorageId(id) { } async function createBeatenGameEntry(user, game) { - let bg = await BeatenGames.findOne({ where: { userId: user.id, gameId: game.id } }) + let bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id, status: 'beat' } }) .catch((err) => { console.log(err); }); if (bg) return false; - await BeatenGames.create({ userId: user.id, gameId: game.id }) + await LoggedGames.create({ userId: user.id, gameId: game.id }) .then((data) => { bg = data; }) @@ -102,7 +102,7 @@ async function getBeatenGameCount(user) { } async function deleteBeatenGameId(id, user) { - const bg = await BeatenGames.findOne({ where: { gameId: id, userId: user.id } }) + const bg = await LoggedGames.findOne({ where: { gameId: id, userId: user.id, status: 'beat' } }) .catch((err) => { console.log(err); }); @@ -116,7 +116,7 @@ async function deleteBeatenGameId(id, user) { } async function deleteBeatenGameNum(num, user) { - const bg = await BeatenGames.findAll({ where: { userId: user.id } }) + const bg = await LoggedGames.findAll({ where: { userId: user.id, status: 'beat' } }) .catch((err) => { console.log(err); }); @@ -140,7 +140,7 @@ async function getLeaderboardEntries() { const results = []; for (let i = 0; i < users.length; i++) { - const count = await BeatenGames.count({ where: { userId: users[i].id } }); + const count = await LoggedGames.count({ where: { userId: users[i].id, status: 'beat' } }); const res = await Users.findOne({ where: { id: users[i].id } }) .catch((err) => { @@ -156,7 +156,7 @@ async function getLeaderboardEntries() { } async function getRecentGameEntry(userId) { - const beatenGameEntry = await BeatenGames.findOne({ where: { userId: userId }, order: [ [ 'createdAt', 'DESC' ]] }) + const beatenGameEntry = await LoggedGames.findOne({ where: { userId: userId, status: 'beat' }, order: [ [ 'createdAt', 'DESC' ]] }) .catch((err) => { console.log(err); }); @@ -174,7 +174,7 @@ async function getRecentGameEntry(userId) { } async function getGames(id) { - const beatenGameEntry = await BeatenGames.findAll({ where: { userId: id } }) + const beatenGameEntry = await LoggedGames.findAll({ where: { userId: id, status: 'beat' } }) .catch((err) => { console.log(err); }); diff --git a/dbObjects.js b/dbObjects.js index 6d16344..87925a7 100644 --- a/dbObjects.js +++ b/dbObjects.js @@ -9,15 +9,15 @@ const sequelize = new Sequelize('database', 'username', 'password', { const Users = require('./models/users.js')(sequelize, Sequelize.DataTypes); const Games = require('./models/games.js')(sequelize, Sequelize.DataTypes); -const BeatenGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes); +const LoggedGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes); // Create userId foreignKey -Users.hasMany(BeatenGames); -BeatenGames.belongsTo(Users); +Users.hasMany(LoggedGames); +LoggedGames.belongsTo(Users); // Create gameId foreignKey -Games.hasMany(BeatenGames); -BeatenGames.belongsTo(Games); +Games.hasMany(LoggedGames); +LoggedGames.belongsTo(Games); Reflect.defineProperty(Users.prototype, 'addUser', { value: async function addUser(userData) { @@ -44,4 +44,4 @@ sequelize.sync({ alter: true }) console.log(err); }); -module.exports = { sequelize, Users, Games, BeatenGames }; \ No newline at end of file +module.exports = { sequelize, Users, Games, LoggedGames }; \ No newline at end of file