From aca903bbdb234f3c1af5cca5812ce626685c9b04 Mon Sep 17 00:00:00 2001 From: Bobo Date: Tue, 12 Sep 2023 10:53:33 +0200 Subject: [PATCH] Additional endpoint to return simple dapps list (#107) * Additional endpoint to return simple dapps list * Property name fix --- src/controllers/DappsStakingController.ts | 16 +++++++++++- src/services/FirebaseService.ts | 32 +++++++++++++++++------ tests/services/FirebaseServiceMock.ts | 4 +++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/controllers/DappsStakingController.ts b/src/controllers/DappsStakingController.ts index b6367c4..dff175a 100644 --- a/src/controllers/DappsStakingController.ts +++ b/src/controllers/DappsStakingController.ts @@ -137,7 +137,21 @@ export class DappsStakingController extends ControllerBase implements IControlle app.route('/api/v1/:network/dapps-staking/dapps').get(async (req: Request, res: Response) => { /* - #swagger.description = 'Retrieves list of dapps registered for dapps staking' + #swagger.description = 'Retrieves list of dapps (full model) registered for dapps staking' + #swagger.tags = ['Dapps Staking'] + #swagger.parameters['network'] = { + in: 'path', + description: 'The network name. Supported networks: astar, shiden, shibuya, rocstar, development', + required: true, + enum: ['astar', 'shiden', 'shibuya', 'rocstar'] + } + */ + res.json(await this._firebaseService.getDappsFull(req.params.network as NetworkType)); + }); + + app.route('/api/v1/:network/dapps-staking/dappssimple').get(async (req: Request, res: Response) => { + /* + #swagger.description = 'Retrieves list of dapps (basic info) registered for dapps staking' #swagger.tags = ['Dapps Staking'] #swagger.parameters['network'] = { in: 'path', diff --git a/src/services/FirebaseService.ts b/src/services/FirebaseService.ts index 1eae5ee..3a04b7a 100644 --- a/src/services/FirebaseService.ts +++ b/src/services/FirebaseService.ts @@ -6,6 +6,7 @@ import { ContainerTypes } from '../containertypes'; import { Guard } from '../guard'; import { DappItem, FileInfo, NewDappItem } from '../models/Dapp'; import { NetworkType } from '../networks'; +import { CollectionReference, DocumentData, Query } from 'firebase-admin/firestore'; export interface Cache { updatedAt?: number; @@ -13,6 +14,7 @@ export interface Cache { } export interface IFirebaseService { + getDappsFull(network: NetworkType): Promise; getDapps(network: NetworkType): Promise; getDapp(address: string, network: NetworkType): Promise; registerDapp(dapp: NewDappItem, network: NetworkType): Promise; @@ -27,20 +29,22 @@ export class FirebaseService implements IFirebaseService { constructor(@inject(ContainerTypes.ApiFactory) private _apiFactory: IApiFactory) {} - public async getDapps(network: NetworkType = 'astar'): Promise { + public async getDappsFull(network: NetworkType = 'astar'): Promise { this.initApp(); const collectionKey = await this.getCollectionKey(network); const query = admin.firestore().collection(collectionKey); - const data = await query.orderBy('name').get(); - const result: DappItem[] = []; - data.forEach((x) => { - const data = x.data() as DappItem; - result.push(data); - }); + return await this.getDappsData(query); + } - return result; + public async getDapps(network: NetworkType = 'astar'): Promise { + this.initApp(); + + const collectionKey = await this.getCollectionKey(network); + const query = admin.firestore().collection(collectionKey).select('name', 'iconUrl', 'address', 'mainCategory'); + + return this.getDappsData(query); } public async getDapp(address: string, network: NetworkType): Promise { @@ -217,4 +221,16 @@ export class FirebaseService implements IFirebaseService { private decode(data: string): string { return data.split('/').join('/'); } + + private async getDappsData(query: CollectionReference | Query): Promise { + const data = await query.orderBy('name').get(); + + const result: DappItem[] = []; + data.forEach((x) => { + const data = x.data() as DappItem; + result.push(data); + }); + + return result; + } } diff --git a/tests/services/FirebaseServiceMock.ts b/tests/services/FirebaseServiceMock.ts index a369844..7f08a39 100644 --- a/tests/services/FirebaseServiceMock.ts +++ b/tests/services/FirebaseServiceMock.ts @@ -7,6 +7,10 @@ export class FirebaseServiceMock implements IFirebaseService { return []; } + public async getDappsFull(network: NetworkType): Promise { + return []; + } + public async registerDapp(dapp: NewDappItem, network: NetworkType): Promise { throw new Error('Method not implemented.'); }