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

Endpoint to fetch results by race id #56

Merged
merged 1 commit into from
Nov 4, 2024
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
29 changes: 29 additions & 0 deletions app/_controllers/result/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import {
CreateResultArgs,
CreatedResult,
IRacerHistory,
IResult,
} from "@/app/_types/result/types";
import EventService from "@/app/_services/event";
import EventDAO from "@/app/_daos/event";

const getResultService = (): ResultService => {
const resultDao = new ResultDAO(databaseClient.result);
return new ResultService(resultDao);
};

const getEventService = (): EventService => {
const eventDao = new EventDAO(databaseClient.event, databaseClient.race);
return new EventService(eventDao);
};

export async function getResultsByRiderId(
riderId: number,
): Promise<IRacerHistory | null> {
Expand All @@ -26,6 +34,27 @@ export async function getResultsByRiderId(
}
}

export async function getResultsByRaceId(
raceId: number,
): Promise<IResult[] | null> {
try {
const eventService = getEventService();
const race = await eventService.getRace({ id: raceId });

const { event } = race[0];

if (!event || !event?.id) {
throw new Error(`Failed to get event id from race id: ${raceId}`);
}

const resultService = getResultService();
const results = await resultService.getResultsByEventId(Number(event.id));
return results;
} catch (error) {
throw new Error(String(error));
}
}

export async function createResult(
resultData: CreateResultArgs,
): Promise<CreatedResult> {
Expand Down
26 changes: 26 additions & 0 deletions app/_daos/result/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ export default class ResultDAO implements IResultDAO {
}
}

async getEventResults(eventId: number) {
try {
const results = (await this.resultRepo.findMany({
where: {
eventId: eventId,
},
include: {
event: {
include: {
Race: {
include: {
raceType: true,
},
},
},
},
resultType: true,
noPlaceCodeType: true,
},
})) as IResult[];
return results;
} catch (error) {
throw new Error(getDatabaseQueryErrorMessage(String(error)));
}
}

// Public CLass Method countResultsByEventId
async countResultsByEventId(eventId: number) {
try {
Expand Down
11 changes: 11 additions & 0 deletions app/_services/result/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ export default class ResultService {
}
}

async getResultsByEventId(eventId: number): Promise<IResult[]> {
try {
const results: IResult[] = await this.resultDao.getEventResults(
Number(eventId),
);
return results;
} catch (error) {
throw new Error(String(error));
}
}

async createResult(resultData: CreateResultArgs) {
try {
const race = this.resultDao.createResult(resultData);
Expand Down
2 changes: 1 addition & 1 deletion app/_types/event/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IPickTypeRow } from "../database/types";

// Base Types
export interface BaseEvent {
id?: number;
name: string;
}

Expand All @@ -23,7 +24,6 @@ export interface IRace extends Omit<BaseRace, "name"> {

export interface IEvent extends BaseEvent {
// look for duplicate in result types
id: number;
Race?: IRace[];
}

Expand Down
1 change: 1 addition & 0 deletions app/_types/result/database/IResultDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

export interface IResultDAO {
getRiderResults(riderId: number): Promise<IResult[] | null>;
getEventResults(eventId: number): Promise<IResult[] | null>;
countResultsByEventId(eventId: number): Promise<number>;
createResult(resultData: CreateResultArgs): Promise<CreatedResult>;
assignCategoryToResult(
Expand Down
27 changes: 27 additions & 0 deletions app/api/v1/races/[id]/results/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { NextRequest, NextResponse } from "next/server";
import { getResultsNotFoundErrorMessage } from "@/app/_constants/errors";
import { getResultsByRaceId } from "@/app/_controllers/result";

export type GetRacePathParams = {
params: Promise<{
id: string;
}>;
};

export async function GET(_request: NextRequest, context: GetRacePathParams) {
const { params } = context;
const { id } = await params;

try {
const results = await getResultsByRaceId(Number(id));
if (results === null) {
return NextResponse.json(
{ error: getResultsNotFoundErrorMessage(String(id)) },
{ status: 404 },
);
}
return NextResponse.json(results, { status: 200 });
} catch (error) {
return NextResponse.json({ error: String(error) }, { status: 500 });
}
}