diff --git a/server/app/Controllers/Http/MatchesController.ts b/server/app/Controllers/Http/MatchesController.ts index 3c8afcd..6ab9273 100644 --- a/server/app/Controllers/Http/MatchesController.ts +++ b/server/app/Controllers/Http/MatchesController.ts @@ -1,6 +1,7 @@ import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' import Match from 'App/Models/Match' import MatchPlayerRankParser from 'App/Parsers/MatchPlayerRankParser' +import MatchRepository from 'App/Repositories/MatchRepository' import DetailedMatchSerializer from 'App/Serializers/DetailedMatchSerializer' import MatchPlayerRankSerializer from 'App/Serializers/MatchPlayerRankSerializer' import { SerializedMatch } from 'App/Serializers/SerializedTypes' @@ -16,7 +17,7 @@ export default class MatchesController { */ public async index({ request, response }: HttpContextContract) { const { puuid, region, lastMatchId, season } = await request.validate(MatchesIndexValidator) - const matchIds = await MatchService.getMatchIdsToFetch(lastMatchId, puuid, season) + const matchIds = await MatchRepository.getNextMatchIds({ lastMatchId, puuid, season }) let matches: SerializedMatch[] = [] if (matchIds.length > 0) { diff --git a/server/app/Controllers/Http/SummonersController.ts b/server/app/Controllers/Http/SummonersController.ts index 7d803e0..2519971 100644 --- a/server/app/Controllers/Http/SummonersController.ts +++ b/server/app/Controllers/Http/SummonersController.ts @@ -89,19 +89,7 @@ export default class SummonersController { const { puuid, region, season } = await request.validate(SummonerOverviewValidator) const finalJSON: any = {} - // Summoner in DB - const summonerDB = await Summoner.firstOrCreate({ puuid: puuid }) - - // MATCHES BASIC - const matchListQuery = summonerDB.related('matchList').query().select('matchId') - if (season) { - matchListQuery - .join('matches', 'summoner_matchlist.match_id', 'matches.id') - .where('matches.season', season) - } - const matchlist = await matchListQuery.orderBy('matchId', 'desc').limit(10) - const matchIds = matchlist.map((m) => m.matchId) - + const matchIds = await MatchRepository.getNextMatchIds({ puuid, season }) finalJSON.matchesDetails = await MatchService.getMatches(region, matchIds, puuid) console.time('STATS') diff --git a/server/app/Repositories/MatchRepository.ts b/server/app/Repositories/MatchRepository.ts index 4c11229..0c11903 100644 --- a/server/app/Repositories/MatchRepository.ts +++ b/server/app/Repositories/MatchRepository.ts @@ -1,10 +1,12 @@ import Database from '@ioc:Adonis/Lucid/Database' +import SummonerMatchlist from 'App/Models/SummonerMatchlist' export interface SelectFilters { puuid: string season?: number limit?: number queue?: number + lastMatchId?: string } class MatchRepository { @@ -26,6 +28,30 @@ class MatchRepository { return query } + /** + * Get the list of matchIds from the next matches to fetch for a specific Summoner + */ + public async getNextMatchIds(filters: SelectFilters) { + const matchListQuery = SummonerMatchlist.query() + .select('matchId') + .where('summoner_puuid', filters.puuid) + + if (filters.lastMatchId) { + matchListQuery.andWhere('match_id', '<', filters.lastMatchId) + } + + if (filters.season) { + matchListQuery + .join('matches', 'summoner_matchlist.match_id', 'matches.id') + .where('matches.season', filters.season) + } + + const limit = filters.limit ?? 10 + + const matchlist = await matchListQuery.orderBy('matchId', 'desc').limit(limit) + return matchlist.map((m) => m.matchId) + } + public async gamemodes(puuid: string) { const query = ` SELECT DISTINCT diff --git a/server/app/Services/MatchService.ts b/server/app/Services/MatchService.ts index 6e4e995..4173f78 100644 --- a/server/app/Services/MatchService.ts +++ b/server/app/Services/MatchService.ts @@ -8,7 +8,6 @@ import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer' import { SerializedMatch } from 'App/Serializers/SerializedTypes' import Match from 'App/Models/Match' import { notEmpty, tutorialQueues } from 'App/helpers' -import SummonerMatchlist from 'App/Models/SummonerMatchlist' class MatchService { /** @@ -83,25 +82,6 @@ class MatchService { return currentMatchListIds.reverse() } - /** - * Get the list of matchIds from the next matches to fetch for a specific Summoner - */ - public async getMatchIdsToFetch(lastMatchId: string, puuid: string, season?: number) { - const matchListQuery = SummonerMatchlist.query() - .select('matchId') - .where('summoner_puuid', puuid) - .andWhere('match_id', '<', lastMatchId) - - if (season) { - matchListQuery - .join('matches', 'summoner_matchlist.match_id', 'matches.id') - .where('matches.season', season) - } - - const matchlist = await matchListQuery.orderBy('matchId', 'desc').limit(10) - return matchlist.map((m) => m.matchId) - } - /** * Fetch list of matches for a specific Summoner */