feat: more matches endpoint

This commit is contained in:
Valentin Kaelin 2020-10-11 16:35:23 +02:00
parent b045615862
commit 749e9eda05
3 changed files with 96 additions and 4 deletions

View file

@ -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)

View file

@ -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,
})
}
}

View file

@ -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 = {}
}