Skip to content

Commit

Permalink
Additional endpoint to return simple dapps list (#107)
Browse files Browse the repository at this point in the history
* Additional endpoint to return simple dapps list

* Property name fix
  • Loading branch information
bobo-k2 authored Sep 12, 2023
1 parent c83d54f commit aca903b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/controllers/DappsStakingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
32 changes: 24 additions & 8 deletions src/services/FirebaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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<T> {
updatedAt?: number;
data: T;
}

export interface IFirebaseService {
getDappsFull(network: NetworkType): Promise<DappItem[]>;
getDapps(network: NetworkType): Promise<DappItem[]>;
getDapp(address: string, network: NetworkType): Promise<NewDappItem | undefined>;
registerDapp(dapp: NewDappItem, network: NetworkType): Promise<DappItem>;
Expand All @@ -27,20 +29,22 @@ export class FirebaseService implements IFirebaseService {

constructor(@inject(ContainerTypes.ApiFactory) private _apiFactory: IApiFactory) {}

public async getDapps(network: NetworkType = 'astar'): Promise<DappItem[]> {
public async getDappsFull(network: NetworkType = 'astar'): Promise<DappItem[]> {
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<DappItem[]> {
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<NewDappItem | undefined> {
Expand Down Expand Up @@ -217,4 +221,16 @@ export class FirebaseService implements IFirebaseService {
private decode(data: string): string {
return data.split('&#x2F;').join('/');
}

private async getDappsData(query: CollectionReference<DocumentData> | Query<DocumentData>): Promise<DappItem[]> {
const data = await query.orderBy('name').get();

const result: DappItem[] = [];
data.forEach((x) => {
const data = x.data() as DappItem;
result.push(data);
});

return result;
}
}
4 changes: 4 additions & 0 deletions tests/services/FirebaseServiceMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export class FirebaseServiceMock implements IFirebaseService {
return [];
}

public async getDappsFull(network: NetworkType): Promise<DappItem[]> {
return [];
}

public async registerDapp(dapp: NewDappItem, network: NetworkType): Promise<DappItem> {
throw new Error('Method not implemented.');
}
Expand Down

0 comments on commit aca903b

Please sign in to comment.