From 0c90d1426c08eb95b54b6baffd4b7bdaa9dc3126 Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Sat, 26 Oct 2019 22:52:50 +0200 Subject: [PATCH] refactor: use a relationship between Summoner and Match in database --- server/app/Controllers/Http/SummonerController.js | 8 ++++---- server/app/Helpers/MatchHelper.js | 4 ++-- server/app/Models/Summoner.js | 3 +++ server/app/Transformers/MatchTransformer.js | 2 +- server/database/migrations/1567863677607_match_schema.js | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/server/app/Controllers/Http/SummonerController.js b/server/app/Controllers/Http/SummonerController.js index 1b8d81d..c942d2e 100644 --- a/server/app/Controllers/Http/SummonerController.js +++ b/server/app/Controllers/Http/SummonerController.js @@ -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) diff --git a/server/app/Helpers/MatchHelper.js b/server/app/Helpers/MatchHelper.js index e070225..70dc395 100644 --- a/server/app/Helpers/MatchHelper.js +++ b/server/app/Helpers/MatchHelper.js @@ -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') } } diff --git a/server/app/Models/Summoner.js b/server/app/Models/Summoner.js index 693143e..8e8d846 100644 --- a/server/app/Models/Summoner.js +++ b/server/app/Models/Summoner.js @@ -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 diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js index 5a63475..7491a83 100644 --- a/server/app/Transformers/MatchTransformer.js +++ b/server/app/Transformers/MatchTransformer.js @@ -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, diff --git a/server/database/migrations/1567863677607_match_schema.js b/server/database/migrations/1567863677607_match_schema.js index e339d5f..4641ad1 100644 --- a/server/database/migrations/1567863677607_match_schema.js +++ b/server/database/migrations/1567863677607_match_schema.js @@ -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}) }) }