refactor: use a relationship between Summoner and Match in database

This commit is contained in:
Valentin Kaelin 2019-10-26 22:52:50 +02:00
parent 6c06bf3cc9
commit 0c90d1426c
5 changed files with 11 additions and 8 deletions

View file

@ -30,10 +30,10 @@ class SummonerController {
finalJSON.account = account
// Summoner in DB
let summonerDB = await Summoner.where({ puuid: account.puuid }).first()
if (!summonerDB) {
summonerDB = await Summoner.create({ puuid: account.puuid })
}
const summonerDB = await Summoner.findOrCreate(
{ puuid: account.puuid },
{ puuid: account.puuid }
)
// CURRENT GAME
const currentGame = await Jax.Spectator.summonerID(account.id)

View file

@ -83,7 +83,7 @@ class MatchHelper {
let matchesDetails = []
const matchesToGetFromRiot = []
for (let i = 0; i < gameIds.length; ++i) {
const matchSaved = await Match.where({ gameId: gameIds[i], puuid: account.puuid }).first()
const matchSaved = await summonerDB.matches().where({ gameId: gameIds[i] }).first()
if (matchSaved) {
console.log('match in mongodb')
matchesDetails.push(matchSaved)
@ -119,7 +119,7 @@ class MatchHelper {
/* Save all matches from Riot Api in db */
for (const match of matchesFromApi) {
await Match.create(match)
await summonerDB.matches().create(match)
console.log('match saved')
}
}

View file

@ -4,6 +4,9 @@
const Model = use('Model')
class Summoner extends Model {
matches() {
return this.hasMany('App/Models/Match', 'puuid', 'summoner_puuid')
}
}
module.exports = Summoner

View file

@ -103,7 +103,7 @@ class MatchTransformer extends BumblebeeTransformer {
enemyTeam.sort(MatchHelper.sortTeamByRole)
return {
puuid: account.puuid,
summoner_puuid: account.puuid,
gameId: match.gameId,
result: win,
status,

View file

@ -7,7 +7,7 @@ class MatchSchema extends Schema {
up () {
this.create('matches', (collection) => {
collection.index('gameId', {gameId: 1})
collection.index('puuid', {puuid: 1})
collection.index('summoner_puuid', {summoner_puuid: 1})
})
}