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 finalJSON.account = account
// Summoner in DB // Summoner in DB
let summonerDB = await Summoner.where({ puuid: account.puuid }).first() const summonerDB = await Summoner.findOrCreate(
if (!summonerDB) { { puuid: account.puuid },
summonerDB = await Summoner.create({ puuid: account.puuid }) { puuid: account.puuid }
} )
// CURRENT GAME // CURRENT GAME
const currentGame = await Jax.Spectator.summonerID(account.id) const currentGame = await Jax.Spectator.summonerID(account.id)

View file

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

View file

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

View file

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

View file

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