mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 21:07:27 +00:00
refactor: use validator for summoner/basic endpoint
This commit is contained in:
parent
25af97d4a6
commit
3bbfab9260
3 changed files with 76 additions and 22 deletions
|
|
@ -4,10 +4,15 @@ import MatchRepository from 'App/Repositories/MatchRepository'
|
||||||
import Jax from 'App/Services/Jax'
|
import Jax from 'App/Services/Jax'
|
||||||
import MatchService from 'App/Services/MatchService'
|
import MatchService from 'App/Services/MatchService'
|
||||||
import SummonerService from 'App/Services/SummonerService'
|
import SummonerService from 'App/Services/SummonerService'
|
||||||
|
import SummonerBasicValidator from 'App/Validators/SummonerBasicValidator'
|
||||||
|
|
||||||
export default class SummonersController {
|
export default class SummonersController {
|
||||||
|
/**
|
||||||
|
* Get all played seasons for a summoner
|
||||||
|
* @param puuid of the summoner
|
||||||
|
*/
|
||||||
private async getSeasons (puuid: string) {
|
private async getSeasons (puuid: string) {
|
||||||
let seasons = await MatchRepository.seasons(puuid)
|
const seasons = await MatchRepository.seasons(puuid)
|
||||||
return seasons.length ? seasons.map(s => s._id) : [10]
|
return seasons.length ? seasons.map(s => s._id) : [10]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -17,15 +22,7 @@ export default class SummonersController {
|
||||||
*/
|
*/
|
||||||
public async basic ({ request, response }: HttpContextContract) {
|
public async basic ({ request, response }: HttpContextContract) {
|
||||||
console.time('all')
|
console.time('all')
|
||||||
const summoner = request.input('summoner')
|
const { summoner, region} = await request.validate(SummonerBasicValidator)
|
||||||
const region = request.input('region')
|
|
||||||
console.log(summoner, region)
|
|
||||||
|
|
||||||
const regexSummonerName = new RegExp('^[0-9\\p{L} _\\.]+$', 'u')
|
|
||||||
if (!regexSummonerName.exec(summoner)) {
|
|
||||||
return response.json(null)
|
|
||||||
}
|
|
||||||
|
|
||||||
const finalJSON:any = {}
|
const finalJSON:any = {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -38,11 +35,6 @@ export default class SummonersController {
|
||||||
finalJSON.account = account
|
finalJSON.account = account
|
||||||
|
|
||||||
// Summoner in DB
|
// Summoner in DB
|
||||||
// const summonerDB = await Summoner.findOrCreate(
|
|
||||||
// { puuid: account.puuid },
|
|
||||||
// { puuid: account.puuid }
|
|
||||||
// )
|
|
||||||
|
|
||||||
let summonerDB = await Summoner.findOne({ puuid: account.puuid })
|
let summonerDB = await Summoner.findOne({ puuid: account.puuid })
|
||||||
if(!summonerDB) {
|
if(!summonerDB) {
|
||||||
summonerDB = await Summoner.create({ puuid: account.puuid })
|
summonerDB = await Summoner.create({ puuid: account.puuid })
|
||||||
|
|
@ -67,7 +59,6 @@ export default class SummonersController {
|
||||||
finalJSON.ranked = await SummonerService.getRanked(account, region)
|
finalJSON.ranked = await SummonerService.getRanked(account, region)
|
||||||
|
|
||||||
// SAVE IN DB
|
// SAVE IN DB
|
||||||
// await summonerDB.save()
|
|
||||||
await summonerDB.save()
|
await summonerDB.save()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('username not found')
|
console.log('username not found')
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,26 @@
|
||||||
import mongodb from '@ioc:Mongodb/Database'
|
import mongodb from '@ioc:Mongodb/Database'
|
||||||
|
import { Collection } from 'mongodb'
|
||||||
|
|
||||||
class MatchRepository {
|
class MatchRepository {
|
||||||
private season?: number
|
private season?: number
|
||||||
|
private collection: Collection
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
// TODO: keep matches collection in the repo instance
|
this.getCollection()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get MongoDB matches collection
|
||||||
|
*/
|
||||||
|
private async getCollection () {
|
||||||
|
this.collection = await mongodb.connection().collection('matches')
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic matchParams used in a lot of requests
|
* Basic matchParams used in a lot of requests
|
||||||
* @param puuid of the summoner
|
* @param puuid of the summoner
|
||||||
*/
|
*/
|
||||||
private _matchParams (puuid: string) {
|
private matchParams (puuid: string) {
|
||||||
return {
|
return {
|
||||||
summoner_puuid: puuid,
|
summoner_puuid: puuid,
|
||||||
result: { $not: { $eq: 'Remake' } },
|
result: { $not: { $eq: 'Remake' } },
|
||||||
|
|
@ -19,18 +28,17 @@ class MatchRepository {
|
||||||
season: this.season ? this.season : { $exists: true },
|
season: this.season ? this.season : { $exists: true },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Summoner's played seasons
|
* Get Summoner's played seasons
|
||||||
* @param puuid of the summoner
|
* @param puuid of the summoner
|
||||||
*/
|
*/
|
||||||
public async seasons (puuid: string) {
|
public async seasons (puuid: string) {
|
||||||
this.season = undefined
|
this.season = undefined
|
||||||
const matchesCollections = await mongodb.connection().collection('matches')
|
return this.collection.aggregate([
|
||||||
|
|
||||||
return matchesCollections.aggregate([
|
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
...this._matchParams(puuid),
|
...this.matchParams(puuid),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
55
server-new/app/Validators/SummonerBasicValidator.ts
Normal file
55
server-new/app/Validators/SummonerBasicValidator.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
|
import { rules, schema } from '@ioc:Adonis/Core/Validator'
|
||||||
|
|
||||||
|
export default class SummonerBasicValidator {
|
||||||
|
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({
|
||||||
|
summoner: schema.string({}, [
|
||||||
|
rules.regex(/^[0-9\p{L} _\.]+$/u),
|
||||||
|
]),
|
||||||
|
region: schema.string(),
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 = {}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue