From e61d0e6474d0f96f39bc50aed0b67a6b84a0a2ad Mon Sep 17 00:00:00 2001 From: "jmosrael@gmail.com" Date: Sun, 12 May 2024 18:16:40 -0700 Subject: [PATCH] work on relational models. get a characters game history --- backend/src/app.ts | 35 +++++++++++++++++++++++--- frontend/src/vues/CharacterDetails.vue | 4 +-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/backend/src/app.ts b/backend/src/app.ts index a343614..73b1fa3 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -62,6 +62,24 @@ const App = sequelize.define( { timestamps: false } ) +// Bind characters to applications and picks +Character.hasMany(App, { foreignKey: 'characterId', as: 'appliedCharacter' }) +App.belongsTo(Character, { foreignKey: 'characterId', as: 'appliedCharacter' }) +Character.hasMany(Pick, { foreignKey: 'characterId', as: 'pickedCharacter' }) +Pick.belongsTo(Character, { foreignKey: 'characterId', as: 'pickedCharacter' }) + +// Bind games to applications and picks +Game.hasMany(App, { foreignKey: 'gameId', as: 'gameApplications' }) +App.belongsTo(Game, { foreignKey: 'gameId', as: 'gameApplications' }) +Game.hasMany(Pick, { foreignKey: 'gameId', as: 'gamePicks' }) +Pick.belongsTo(Game, { foreignKey: 'gameId', as: 'gamePicks' }) + +// Bind picked characters to games. +Game.belongsToMany(Character, { through: 'Apps', as: 'characterAppliedForGame' }) +Character.belongsToMany(Game, { through: 'Apps', as: 'characterAppliedForGame' }) +Game.belongsToMany(Character, { through: 'Picks', as: 'characterPickedForGame' }) +Character.belongsToMany(Game, { through: 'Picks', as: 'characterPickedForGame' }) + const app = express() const jsonParser = json() const port = 3001 @@ -89,13 +107,13 @@ app.post('/api/character', jsonParser, async (req, res) => { res.json({ characterData, pageCount }) }) -app.get('/api/game/:characterId', async (req, res) => { - res.send(await Game.findOne({ where: { id: req.params['characterId'] } })) +app.get('/api/game/:gameId', async (req, res) => { + res.send(await Game.findOne({ where: { id: req.params['gameId'] } })) }) app.post('/api/game', jsonParser, async (req, res) => { const page = req.body.page || 0 - const orderBy = req.body.orderBy || 'id' + const orderBy = req.body.orderBy || 'id' const count = req.body.count || 10 const filter = req.body.filter || '' @@ -136,6 +154,17 @@ app.post('/api/character/:characterId/picks', async (req, res) => { res.send(picks) }) +app.post('/api/character/:characterId/gameHistory', async (req, res) => { + const gameHistory = await Character.findOne({ + include: [ + { model: Game, as: 'characterPickedForGame' }, + { model: Game, as: 'characterAppliedForGame' } + ], + where: { id: req.params['characterId'] } + }) + res.send(gameHistory) +}) + app.listen(port, async () => { await sequelize.authenticate() return console.log(`Express is listening at http://localhost:${port}`) diff --git a/frontend/src/vues/CharacterDetails.vue b/frontend/src/vues/CharacterDetails.vue index ffa981b..5ff63e9 100644 --- a/frontend/src/vues/CharacterDetails.vue +++ b/frontend/src/vues/CharacterDetails.vue @@ -20,10 +20,10 @@ const character = ref({}) async function loadCharacterDetails() { const characterResponse = await axios.get(`/api/character/${characterId.value}`) - const gameDetails = await axios.post("/api/game", { - }) + const gameDetails = await axios.post(`/api/character/${characterId.value}/gameHistory`) console.log(characterResponse) + console.log(gameDetails) character.value = characterResponse.data }