Skip to content

Commit

Permalink
feat(express): add optional language support on existing endpoints
Browse files Browse the repository at this point in the history
closes #12
  • Loading branch information
BenShelton committed Jun 10, 2021
1 parent 1ceda51 commit 7426ec0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
8 changes: 6 additions & 2 deletions packages/express/src/router/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ router.get('/image', (req, res) => {
})

router.get('/video', async (req, res) => {
const { type, doc, track, issue } = req.query as Partial<Download.Video.QueryParams>
const { type, doc, track, issue, languageId } = req.query as Partial<Download.Video.QueryParams>
if (!type || !doc || !track || !issue) return res.status(401).json({ message: 'Type, Doc, Track and Issue are required' })
const stream = await getVideoStream({ type, doc, track, issue })
const language = Number(languageId || 0)
if (isNaN(language)) {
return res.status(401).json({ message: 'LanguageId must be a number' })
}
const stream = await getVideoStream({ type, doc, track, issue, languageId: language })
if (!stream) return res.status(404).json({ message: 'No Video Found' })
return stream.pipe(res)
})
Expand Down
26 changes: 20 additions & 6 deletions packages/express/src/router/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ function addVideoURL (video: VideoDTO): VideoDTOWithURL {
}

router.get('/watchtower', async (req, res) => {
const { date } = req.query as Partial<Media.Watchtower.QueryParams>
const { date, languageId } = req.query as Partial<Media.Watchtower.QueryParams>
if (!isValidDate(date)) return res.status(401).json({ message: 'Invalid Date' })

const language = Number(languageId || 0)
if (isNaN(language)) {
return res.status(401).json({ message: 'LanguageId must be a number' })
}

const db = new CatalogDatabase(CATALOG_PATH)
const publication = await db.getPublication(date, DOWNLOAD_DIR, 'wt')
const publication = await db.getPublication(date, DOWNLOAD_DIR, 'wt', language)
if (!publication) return res.status(404).json({ message: 'No Watchtower Found' })

const images = (await publication.getImages(date))
Expand All @@ -52,11 +57,16 @@ router.get('/watchtower', async (req, res) => {
})

router.get('/oclm', async (req, res) => {
const { date } = req.query as Partial<Media.OCLM.QueryParams>
const { date, languageId } = req.query as Partial<Media.OCLM.QueryParams>
if (!isValidDate(date)) return res.status(401).json({ message: 'Invalid Date' })

const language = Number(languageId || 0)
if (isNaN(language)) {
return res.status(401).json({ message: 'LanguageId must be a number' })
}

const db = new CatalogDatabase(CATALOG_PATH)
const publication = await db.getPublication(date, DOWNLOAD_DIR, 'oclm')
const publication = await db.getPublication(date, DOWNLOAD_DIR, 'oclm', language)
if (!publication) return res.status(404).json({ message: 'No OCLM Workbook Found' })

const images = (await publication.getImages(date))
Expand All @@ -75,16 +85,20 @@ router.get('/oclm', async (req, res) => {
})

router.get('/details', async (req, res) => {
const { type, doc, issue, track } = req.query as Partial<Media.Details.QueryParams>
const { type, doc, issue, track, languageId } = req.query as Partial<Media.Details.QueryParams>
if (type !== 'doc' && type !== 'pub') {
return res.status(401).json({ message: 'Type must be one of "doc" or "pub"' })
}
if (doc === undefined || issue === undefined || track === undefined) {
return res.status(401).json({ message: 'Doc, Issue & Track are required' })
}
const language = Number(languageId || 0)
if (isNaN(language)) {
return res.status(401).json({ message: 'LanguageId must be a number' })
}

const db = new CatalogDatabase(CATALOG_PATH)
const details = await db.getMediaDetails({ type, doc, issue, track })
const details = await db.getMediaDetails({ type, doc, issue, track, languageId: language })
if (!details) return res.status(404).json({ message: 'No Media Details Found' })

const response: Media.Details.Response = {
Expand Down
4 changes: 4 additions & 0 deletions packages/express/types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export namespace Download {
doc: string
track: string
issue: string
languageId?: string
}
}
}
Expand All @@ -37,6 +38,7 @@ export namespace Media {
export namespace Watchtower {
export interface QueryParams {
date: string
languageId?: string
}
export interface Response {
message: {
Expand All @@ -48,6 +50,7 @@ export namespace Media {
export namespace OCLM {
export interface QueryParams {
date: string
languageId?: string
}
export interface Response {
message: {
Expand All @@ -62,6 +65,7 @@ export namespace Media {
doc: string | number
issue: number
track: number
languageId?: string
}
export interface Response {
message: {
Expand Down

0 comments on commit 7426ec0

Please sign in to comment.