diff --git a/src/app/teams/controller.ts b/src/app/teams/controller.ts index 48dc5fe..2ee402c 100644 --- a/src/app/teams/controller.ts +++ b/src/app/teams/controller.ts @@ -26,11 +26,16 @@ import { export class TeamsController extends Controller { /** * Retrieves team information - * @param conference conference abbreviation filter + * @param conference Optional conference abbreviation filter + * @param year Optional year filter to get historical conference affiliations + * @isInt year */ @Get() - public async getTeams(@Query() conference?: string): Promise { - return await getTeams(conference); + public async getTeams( + @Query() conference?: string, + @Query() year?: number, + ): Promise { + return await getTeams(conference, year); } /** diff --git a/src/app/teams/service.ts b/src/app/teams/service.ts index df508bb..e715b7f 100644 --- a/src/app/teams/service.ts +++ b/src/app/teams/service.ts @@ -10,15 +10,27 @@ import { Venue, } from './types'; -export const getTeams = async (conference?: string): Promise => { +export const getTeams = async (conference?: string, year?: number): Promise => { let query = kdb .selectFrom('team') .leftJoin('venue', 'team.venueId', 'venue.id') - .leftJoin('conferenceTeam', (join) => - join - .onRef('team.id', '=', 'conferenceTeam.teamId') - .on('conferenceTeam.endYear', 'is', null), - ) + .leftJoin('conferenceTeam', (join) => { + if (year) { + return join + .onRef('team.id', '=', 'conferenceTeam.teamId') + .on('conferenceTeam.startYear', '<=', year) + .on((eb) => + eb.or([ + eb('conferenceTeam.endYear', 'is', null), + eb('conferenceTeam.endYear', '>=', year), + ]), + ); + } else { + return join + .onRef('team.id', '=', 'conferenceTeam.teamId') + .on('conferenceTeam.endYear', 'is', null); + } + }) .leftJoin('conference', 'conferenceTeam.conferenceId', 'conference.id') .select([ 'team.id', @@ -60,6 +72,10 @@ export const getTeams = async (conference?: string): Promise => { ); } + if (year) { + query = query.where('conferenceTeam.id', 'is not', null); + } + const teams = await query.execute(); return teams.map( (t): Team => ({