diff --git a/client/src/store/modules/summoner.js b/client/src/store/modules/summoner.js index 744a250..306d2f7 100644 --- a/client/src/store/modules/summoner.js +++ b/client/src/store/modules/summoner.js @@ -172,13 +172,21 @@ export const actions = { commit('SUMMONER_NOT_PLAYING') } }, - async moreMatches({ commit }) { + async moreMatches({ commit, rootState }) { commit('MATCHES_LOADING') - const account = state.basic.account const gameIds = state.basic.matchList.slice(state.overview.matchIndex, state.overview.matchIndex + 10).map(({ gameId }) => gameId) - const resp = await axios(({ url: 'match', data: { account, gameIds }, method: 'POST' })).catch(() => { }) + const resp = await axios(({ + url: 'match', + data: { + puuid: state.basic.account.puuid, + accountId: state.basic.account.accountId, + region: rootState.regionsList[rootState.settings.region], + gameIds + }, + method: 'POST' + })).catch(() => { }) console.log('---MATCHES INFOS---') console.log(resp.data) const newMatches = createMatchData(resp.data.matches) diff --git a/server-new/app/Controllers/Http/MatchesController.ts b/server-new/app/Controllers/Http/MatchesController.ts index f6fa260..9a74267 100644 --- a/server-new/app/Controllers/Http/MatchesController.ts +++ b/server-new/app/Controllers/Http/MatchesController.ts @@ -1,4 +1,31 @@ -// import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import Summoner from 'App/Models/Summoner' +import MatchService from 'App/Services/MatchService' +import StatsService from 'App/Services/StatsService' +import MatchesIndexValidator from 'App/Validators/MatchesIndexValidator' export default class MatchesController { + /** + * POST - Return data from matches searched by gameIds + * @param ctx + */ + public async index ({ request, response }: HttpContextContract) { + console.log('More Matches Request') + const { puuid, accountId, region, gameIds } = await request.validate(MatchesIndexValidator) + + const summonerDB = await Summoner.findOne({ puuid }) + if (!summonerDB) { + return response.json(null) + } + const matches = await MatchService.getMatches(puuid, accountId, region, gameIds, summonerDB) + + await summonerDB.save() + + const stats = await StatsService.getSummonerStats(puuid) + + return response.json({ + matches, + stats, + }) + } } diff --git a/server-new/app/Validators/MatchesIndexValidator.ts b/server-new/app/Validators/MatchesIndexValidator.ts new file mode 100644 index 0000000..26c8dc5 --- /dev/null +++ b/server-new/app/Validators/MatchesIndexValidator.ts @@ -0,0 +1,57 @@ +import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import { schema } from '@ioc:Adonis/Core/Validator' + +export default class MatchesIndexValidator { + constructor (private ctx: HttpContextContract) { + } + + /** + * Defining a schema to validate the "shape", "type", "formatting" and "integrity" of data. + * + * For example: + * 1. The username must be of data type string. But then also, it should + * not contain special characters or numbers. + * ``` + * schema.string({}, [ rules.alpha() ]) + * ``` + * + * 2. The email must be of data type string, formatted as a valid + * email. But also, not used by any other user. + * ``` + * schema.string({}, [ + * rules.email(), + * rules.unique({ table: 'users', column: 'email' }), + * ]) + * ``` + */ + public schema = schema.create({ + puuid: schema.string(), + accountId: schema.string(), + region: schema.string(), + gameIds: schema.array().members( + schema.number() + ), + }) + + /** + * The `schema` first gets compiled to a reusable function and then that compiled + * function validates the data at runtime. + * + * Since, compiling the schema is an expensive operation, you must always cache it by + * defining a unique cache key. The simplest way is to use the current request route + * key, which is a combination of the route pattern and HTTP method. + */ + public cacheKey = this.ctx.routeKey + + /** + * Custom messages for validation failures. You can make use of dot notation `(.)` + * for targeting nested fields and array expressions `(*)` for targeting all + * children of an array. For example: + * + * { + * 'profile.username.required': 'Username is required', + * 'scores.*.number': 'Define scores as valid numbers' + * } + */ + public messages = {} +}