Skip to content

Commit

Permalink
add schools index route
Browse files Browse the repository at this point in the history
  • Loading branch information
henrygd committed Dec 27, 2024
1 parent e7b7d80 commit f4d1721
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,20 @@ Provides details of a single game.

Website: https://www.ncaa.com/game/6305900

- `GET /game/6305900` or `GET /game/6305900/boxscore` returns box score
- `GET /game/6305900` returns general information
- `GET /game/6305900/boxscore` returns box score
- `GET /game/6305900/play-by-play` returns play by play
- `GET /game/6305900/scoring-summary` returns scoring summary if available
- `GET /game/6305900/team-stats` returns team stats if available

### Schools Index

Returns a list of all schools.

Website: https://www.ncaa.com/schools-index

`GET /schools-index`

## Deployment

Use the included [docker-compose.yml](/docker-compose.yml) or run directly with docker:
Expand Down
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const validPaths = new Set([
'scoreboard',
'schedule',
'game',
'schools-index',
])

// set cache expiry to 30 min
Expand Down Expand Up @@ -68,6 +69,25 @@ export const app = new Elysia()
// set cache key
store.cacheKey = path + (page ?? '')
})
// schools-index route to return list of all schools
.get('/schools-index', async ({ store }) => {
if (cache.has(store.cacheKey)) {
return cache.get(store.cacheKey)
}
const req = await fetch('https://www.ncaa.com/json/schools')
try {
const json = (await req.json()).map((school: Record<string, string>) => ({
slug: school.slug,
name: school.name,
long: school.long_name,
}))
const data = JSON.stringify(json)
cache.set(store.cacheKey, data)
return data
} catch (err) {
throw new NotFoundError(JSON.stringify({ message: 'Resource not found' }))
}
})
// game route to retrieve game details
.get('/game/:id?/:page?', async ({ store, params: { id, page } }) => {
if (scoreboardCache.has(store.cacheKey)) {
Expand Down
7 changes: 7 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ describe('General', () => {
expect(data.meta.title).toBe('SCORING')
expect(data.meta).toContainKeys(['title', 'teams'])
})
it('schools index route returns good data', async () => {
const response = await app.handle(new Request('http://localhost/schools-index'))
expect(response.status).toBe(200)
const data = await response.json()
expect(data).toBeArray()
expect(data[0]).toContainKeys(['slug', 'name', 'long'])
})
it('re-request uses cached data', async () => {
const start = performance.now()
await app.handle(new Request('http://localhost/rankings/football/fbs/associated-press'))
Expand Down

0 comments on commit f4d1721

Please sign in to comment.