Skip to content

Commit

Permalink
fix: added the ability to query teams by year
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueSCar committed Aug 20, 2024
1 parent fe6af67 commit 34bd30b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/app/teams/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Team[]> {
return await getTeams(conference);
public async getTeams(
@Query() conference?: string,
@Query() year?: number,
): Promise<Team[]> {
return await getTeams(conference, year);
}

/**
Expand Down
28 changes: 22 additions & 6 deletions src/app/teams/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ import {
Venue,
} from './types';

export const getTeams = async (conference?: string): Promise<Team[]> => {
export const getTeams = async (conference?: string, year?: number): Promise<Team[]> => {
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',
Expand Down Expand Up @@ -60,6 +72,10 @@ export const getTeams = async (conference?: string): Promise<Team[]> => {
);
}

if (year) {
query = query.where('conferenceTeam.id', 'is not', null);
}

const teams = await query.execute();
return teams.map(
(t): Team => ({
Expand Down

0 comments on commit 34bd30b

Please sign in to comment.