Convert BeatenGames to LoggedGames

This commit is contained in:
baz 2024-02-11 19:19:43 +00:00
parent 15a1a950c2
commit 8ba04b5735
3 changed files with 15 additions and 15 deletions

View File

@ -1,6 +1,6 @@
const { Sequelize, DataTypes } = require('sequelize'); const { Sequelize, DataTypes } = require('sequelize');
require('./dbObjects.js'); require('./dbObjects.js');
const { sequelize, BeatenGames } = require ('./dbObjects.js'); const { sequelize, LoggedGames } = require ('./dbObjects.js');
// Add a new column to the existing table // Add a new column to the existing table
(async () => { (async () => {

View File

@ -1,4 +1,4 @@
const { Users, Games, BeatenGames } = require ('./dbObjects.js'); const { Users, Games, LoggedGames } = require ('./dbObjects.js');
const fs = require('fs'); const fs = require('fs');
async function checkUserRegistration(user) { async function checkUserRegistration(user) {
@ -68,14 +68,14 @@ async function checkGameStorageId(id) {
} }
async function createBeatenGameEntry(user, game) { 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) => { .catch((err) => {
console.log(err); console.log(err);
}); });
if (bg) return false; if (bg) return false;
await BeatenGames.create({ userId: user.id, gameId: game.id }) await LoggedGames.create({ userId: user.id, gameId: game.id })
.then((data) => { .then((data) => {
bg = data; bg = data;
}) })
@ -102,7 +102,7 @@ async function getBeatenGameCount(user) {
} }
async function deleteBeatenGameId(id, 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) => { .catch((err) => {
console.log(err); console.log(err);
}); });
@ -116,7 +116,7 @@ async function deleteBeatenGameId(id, user) {
} }
async function deleteBeatenGameNum(num, 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) => { .catch((err) => {
console.log(err); console.log(err);
}); });
@ -140,7 +140,7 @@ async function getLeaderboardEntries() {
const results = []; const results = [];
for (let i = 0; i < users.length; i++) { 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 } }) const res = await Users.findOne({ where: { id: users[i].id } })
.catch((err) => { .catch((err) => {
@ -156,7 +156,7 @@ async function getLeaderboardEntries() {
} }
async function getRecentGameEntry(userId) { 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) => { .catch((err) => {
console.log(err); console.log(err);
}); });
@ -174,7 +174,7 @@ async function getRecentGameEntry(userId) {
} }
async function getGames(id) { async function getGames(id) {
const beatenGameEntry = await BeatenGames.findAll({ where: { userId: id } }) const beatenGameEntry = await LoggedGames.findAll({ where: { userId: id, status: 'beat' } })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
}); });

View File

@ -9,15 +9,15 @@ const sequelize = new Sequelize('database', 'username', 'password', {
const Users = require('./models/users.js')(sequelize, Sequelize.DataTypes); const Users = require('./models/users.js')(sequelize, Sequelize.DataTypes);
const Games = require('./models/games.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 // Create userId foreignKey
Users.hasMany(BeatenGames); Users.hasMany(LoggedGames);
BeatenGames.belongsTo(Users); LoggedGames.belongsTo(Users);
// Create gameId foreignKey // Create gameId foreignKey
Games.hasMany(BeatenGames); Games.hasMany(LoggedGames);
BeatenGames.belongsTo(Games); LoggedGames.belongsTo(Games);
Reflect.defineProperty(Users.prototype, 'addUser', { Reflect.defineProperty(Users.prototype, 'addUser', {
value: async function addUser(userData) { value: async function addUser(userData) {
@ -44,4 +44,4 @@ sequelize.sync({ alter: true })
console.log(err); console.log(err);
}); });
module.exports = { sequelize, Users, Games, BeatenGames }; module.exports = { sequelize, Users, Games, LoggedGames };