Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional endpoint to return simple dapps list #107

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading