From dfc7b64e76464c7066ee6ea38bc522571e5e0779 Mon Sep 17 00:00:00 2001 From: Kalane Date: Sun, 12 Sep 2021 17:05:30 +0200 Subject: [PATCH] feat: start summoner/basic endpoint --- server-v2/.env.example | 2 ++ .../Controllers/Http/SummonersController.ts | 25 ++++++++++++++++++- server-v2/app/Services/Jax/JaxConfig.ts | 2 +- server-v2/app/Services/SummonerService.ts | 11 +++----- server-v2/env.ts | 2 ++ 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/server-v2/.env.example b/server-v2/.env.example index 8ffdd9f..73ecb91 100644 --- a/server-v2/.env.example +++ b/server-v2/.env.example @@ -15,3 +15,5 @@ REDIS_CONNECTION=local REDIS_HOST=127.0.0.1 REDIS_PORT=6379 REDIS_PASSWORD= + +RIOT_API_KEY= diff --git a/server-v2/app/Controllers/Http/SummonersController.ts b/server-v2/app/Controllers/Http/SummonersController.ts index a4252a5..954735a 100644 --- a/server-v2/app/Controllers/Http/SummonersController.ts +++ b/server-v2/app/Controllers/Http/SummonersController.ts @@ -1,10 +1,33 @@ import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import Summoner from 'App/Models/Summoner' +import SummonerService from 'App/Services/SummonerService' import SummonerBasicValidator from 'App/Validators/SummonerBasicValidator' export default class SummonersController { public async basic({ request, response }: HttpContextContract) { const { summoner, region } = await request.validate(SummonerBasicValidator) - return response.json('BASIC REQUEST from ' + summoner + ' - ' + region) + const finalJSON: any = {} + + try { + const account = await SummonerService.getAccount(summoner, region) + // Check if the summoner is found + if (!account) { + return response.json(null) + } + account.region = region + finalJSON.account = account + + // Summoner in DB + const summonerDB = await Summoner.firstOrCreate({ puuid: account.puuid }) + + // Summoner names + finalJSON.account.names = await SummonerService.getAllSummonerNames(account, summonerDB) + } catch (e) { + console.log(e) + return response.json(null) + } + + return response.json(finalJSON) } public async overview({ response }: HttpContextContract) { diff --git a/server-v2/app/Services/Jax/JaxConfig.ts b/server-v2/app/Services/Jax/JaxConfig.ts index 2f5ccc8..d6d9763 100644 --- a/server-v2/app/Services/Jax/JaxConfig.ts +++ b/server-v2/app/Services/Jax/JaxConfig.ts @@ -12,7 +12,7 @@ export interface JaxConfigRequestOptions { } export const JAX_CONFIG: JaxConfig = { - key: Env.get('API_KEY') as string, + key: Env.get('RIOT_API_KEY') as string, region: 'euw1', requestOptions: { retriesBeforeAbort: 3, diff --git a/server-v2/app/Services/SummonerService.ts b/server-v2/app/Services/SummonerService.ts index 7a20116..f17f057 100644 --- a/server-v2/app/Services/SummonerService.ts +++ b/server-v2/app/Services/SummonerService.ts @@ -59,13 +59,10 @@ class SummonerService { * @param summonerDB summoner in the database */ public async getAllSummonerNames(account: SummonerDTO, summonerDB: Summoner) { - if (!summonerDB.names.find((n) => n.name === account.name)) { - await summonerDB.related('names').create({ - name: account.name, - }) - } - - return summonerDB.names + await summonerDB.related('names').firstOrCreate({ + name: account.name, + }) + return summonerDB.related('names').query().select('name', 'created_at') } /** diff --git a/server-v2/env.ts b/server-v2/env.ts index c028956..0825a6c 100644 --- a/server-v2/env.ts +++ b/server-v2/env.ts @@ -33,4 +33,6 @@ export default Env.rules({ REDIS_HOST: Env.schema.string({ format: 'host' }), REDIS_PORT: Env.schema.number(), REDIS_PASSWORD: Env.schema.string.optional(), + + RIOT_API_KEY: Env.schema.string(), })