Compare commits
2 Commits
ccfee4f8c0
...
3cbeb7df68
Author | SHA1 | Date |
---|---|---|
baz | 3cbeb7df68 | |
baz | 5fb39b28b3 |
|
@ -1,4 +1,4 @@
|
|||
const { Users, Games, LoggedGames } = require ('./dbObjects.js');
|
||||
const { Users, Games, LoggedGames, Changelog } = require ('./dbObjects.js');
|
||||
const fs = require('fs');
|
||||
|
||||
async function checkUserRegistration(user) {
|
||||
|
@ -85,6 +85,8 @@ async function createPlanningGameEntry(user, game, date) {
|
|||
|
||||
if (entry.status == 'planning') return false;
|
||||
|
||||
await createChangelogEntry(user, game, entry.status, 'planning');
|
||||
|
||||
entry.status = 'planning';
|
||||
|
||||
if (!date) {
|
||||
|
@ -106,6 +108,8 @@ async function createPlayingGameEntry(user, game, date) {
|
|||
|
||||
if (entry.status == 'playing') return false;
|
||||
|
||||
await createChangelogEntry(user, game, entry.status, 'playing');
|
||||
|
||||
entry.status = 'playing';
|
||||
|
||||
if (!date) {
|
||||
|
@ -127,6 +131,8 @@ async function createBeatenGameEntry(user, game, date) {
|
|||
|
||||
if (entry.status == 'beat') return false;
|
||||
|
||||
await createChangelogEntry(user, game, entry.status, 'beat');
|
||||
|
||||
entry.status = 'beat';
|
||||
|
||||
if (!date) {
|
||||
|
@ -141,6 +147,13 @@ async function createBeatenGameEntry(user, game, date) {
|
|||
return entry;
|
||||
}
|
||||
|
||||
async function createChangelogEntry(user, game, oldStatus, newStatus) {
|
||||
return await Changelog.create({ userId: user.id, gameId: game.id, newStatus: newStatus, oldStatus: oldStatus })
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
async function checkLoggedGameEntry(user, game) {
|
||||
const bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id } })
|
||||
.catch((err) => {
|
||||
|
@ -163,7 +176,14 @@ async function createLoggedGameEntry(user, game, status, date) {
|
|||
console.log(err);
|
||||
});
|
||||
|
||||
if (bg) return true;
|
||||
if (bg) {
|
||||
await Changelog.create({ userId: user.id, gameId: game.id, newStatus: status })
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -216,6 +236,13 @@ async function deleteLoggedGameId(id, user, status) {
|
|||
const entry = bg;
|
||||
await bg.destroy();
|
||||
|
||||
if (bg) {
|
||||
await Changelog.create({ userId: user.id, gameId: entry.gameId, oldStatus: status })
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
|
@ -244,6 +271,13 @@ async function deleteLoggedGameNum(num, user, status) {
|
|||
const entry = bg[num - 1];
|
||||
await bg[num - 1].destroy();
|
||||
|
||||
if (bg) {
|
||||
await Changelog.create({ userId: user.id, gameId: entry.gameId, oldStatus: status })
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ const sequelize = new Sequelize('database', 'username', 'password', {
|
|||
const db = require('./models/games.js')(sequelize, Sequelize.DataTypes);
|
||||
require('./models/users.js')(sequelize, Sequelize.DataTypes);
|
||||
require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
|
||||
require('./models/changelog.js')(sequelize, Sequelize.DataTypes);
|
||||
|
||||
const force = process.argv.includes('--force') || process.argv.includes('-f');
|
||||
|
||||
|
|
15
dbObjects.js
15
dbObjects.js
|
@ -10,15 +10,24 @@ 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 LoggedGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
|
||||
const Changelog = require('./models/changelog.js')(sequelize, Sequelize.DataTypes);
|
||||
|
||||
// Create userId foreignKey
|
||||
// Create userId foreignKey in LoggedGames
|
||||
Users.hasMany(LoggedGames);
|
||||
LoggedGames.belongsTo(Users);
|
||||
|
||||
// Create gameId foreignKey
|
||||
// Create gameId foreignKey in LoggedGames
|
||||
Games.hasMany(LoggedGames);
|
||||
LoggedGames.belongsTo(Games);
|
||||
|
||||
// Create userId foreignKey in Changelog
|
||||
Users.hasMany(Changelog);
|
||||
Changelog.belongsTo(Users);
|
||||
|
||||
// Create gameId foreignKey in Changelog
|
||||
Games.hasMany(Changelog);
|
||||
Changelog.belongsTo(Games);
|
||||
|
||||
Reflect.defineProperty(Users.prototype, 'addUser', {
|
||||
value: async function addUser(userData) {
|
||||
const user = await Users.findOne({
|
||||
|
@ -44,4 +53,4 @@ sequelize.sync({ alter: true })
|
|||
console.log(err);
|
||||
});
|
||||
|
||||
module.exports = { sequelize, Users, Games, LoggedGames };
|
||||
module.exports = { sequelize, Users, Games, LoggedGames, Changelog };
|
|
@ -0,0 +1,15 @@
|
|||
module.exports = (sequelize, DataTypes) => {
|
||||
return sequelize.define('changelog', {
|
||||
oldStatus: {
|
||||
type: DataTypes.ENUM('planning', 'playing', 'beat'),
|
||||
allowNull: true,
|
||||
},
|
||||
newStatus: {
|
||||
type: DataTypes.ENUM('planning', 'playing', 'beat'),
|
||||
allowNull: true,
|
||||
},
|
||||
}, {
|
||||
timestamps: true,
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in New Issue