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

Refactored play APIs to the RESTful form #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions server/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import fs from 'fs';
import { RoundRepository } from './repository/round';
import { PaginationParams, pagination } from './middlewares/pagination';
import { CSPRCloudAPIClient } from './cspr-cloud/api-client';
import { PlayEventPayload, isPlayDeploy, isEvent, isPlayEventPayload } from './events';
import { trackPlay } from './event-handler';
import { raw } from 'mysql2';
import { isPlayDeploy } from './events';
import { Play } from './entity/play.entity';

const app: Express = express();
Expand All @@ -31,6 +29,7 @@ const wss = new WebSocket.Server({ server });

interface FindPlaysQuery extends PaginationParams {
player_account_hash: string;
deploy_hash: string;
}

async function initAPI() {
Expand All @@ -41,6 +40,9 @@ async function initAPI() {

const csprCloudClient = new CSPRCloudAPIClient(config.csprCloudApiUrl, config.csprCloudAccessKey);

/**
* @deprecated Use GET /plays?player_account_hash=...
*/
app.get('/playsByPlayer', pagination(), async (req: Request<never, never, never, FindPlaysQuery>, res: Response) => {
const [plays, total] = await playsRepository.findByPlayer(req.query.player_account_hash, {
limit: req.query.limit,
Expand All @@ -53,7 +55,16 @@ async function initAPI() {
});

app.get('/plays', pagination(), async (req: Request<never, never, never, FindPlaysQuery>, res: Response) => {
const [plays, total] = await playsRepository.getPaginatedPlays({
const filters = {};
if (req.query.player_account_hash) {
filters['playerAccountHash'] = req.query.player_account_hash;
}

if (req.query.deploy_hash) {
filters['deployHash'] = req.query.deploy_hash;
}

const [plays, total] = await playsRepository.getPaginatedPlays(filters, {
limit: req.query.limit,
offset: req.query.offset,
});
Expand Down Expand Up @@ -89,6 +100,9 @@ async function initAPI() {
}
});

/**
* @deprecated Use GET /plays?deploy_hash=...
*/
app.get('/playByDeployHash', async (req: Request, res: Response) => {
if (req.query.deployHash === null) {
res.status(400).send('No deploy hash provided');
Expand Down
9 changes: 8 additions & 1 deletion server/src/repository/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export class PlayRepository {
this.repo = dataSource.getRepository(Play);
}

/**
* @deprecated Use getPaginatedPlays
*/
findByPlayer(playerAccountHash: string, paginationParams: { limit: number; offset: number }) {
return this.repo.findAndCount({
where: {
Expand All @@ -17,6 +20,9 @@ export class PlayRepository {
});
}

/**
* @deprecated Use getPaginatedPlays
*/
findByDeployHash(deployHash: string): Promise<Play | null> {
return this.repo.findOne({
where: {
Expand All @@ -25,8 +31,9 @@ export class PlayRepository {
});
}

getPaginatedPlays(paginationParams: { limit: number; offset: number }): Promise<[Play[], number]> {
getPaginatedPlays(filters, paginationParams: { limit: number; offset: number }): Promise<[Play[], number]> {
return this.repo.findAndCount({
where: filters,
take: paginationParams.limit,
skip: paginationParams.offset,
order: {
Expand Down
5 changes: 5 additions & 0 deletions server/src/repository/round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export class RoundRepository {
this.repo = dataSource.getRepository(Round);
}

/**
* @todo Switch to paginationParams (same as in the play repository)
* @param limit
* @param offset
*/
async getRounds(limit: number, offset: number): Promise<[Round[], number]> {
const queryBuilder = this.repo
.createQueryBuilder('p')
Expand Down