work on relational models. get a characters game history
This commit is contained in:
@@ -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}`)
|
||||
|
||||
Reference in New Issue
Block a user