diff --git a/api/controllers/agencyController.ts b/api/controllers/agencyController.ts new file mode 100644 index 0000000..6637a19 --- /dev/null +++ b/api/controllers/agencyController.ts @@ -0,0 +1,17 @@ +import { Path, GET, PathParam } from 'typescript-rest'; +import { Tags } from 'typescript-rest-swagger'; +import AgencyDto from '../models/dto/agencyDto'; +import { getAgencies } from '../services/agencyService'; + +@Path('/api/agency') +@Tags('Agency') +export default class AgencyController { + /** + * Returns Agencies. + */ + @GET + @Path('/:id/agency') + async getSubcategories(@PathParam('id') id: string): Promise { + return getAgencies(id); + } +} \ No newline at end of file diff --git a/api/controllers/index.ts b/api/controllers/index.ts index f17bfc4..9a9af6b 100644 --- a/api/controllers/index.ts +++ b/api/controllers/index.ts @@ -1,4 +1,5 @@ import healthController from './healthController'; import taxonomyController from './taxonomyController'; +import agencyController from './agencyController'; -export default [healthController, taxonomyController]; +export default [agencyController, healthController, taxonomyController]; diff --git a/api/models/dto/agencyDto.ts b/api/models/dto/agencyDto.ts new file mode 100644 index 0000000..f9558c5 --- /dev/null +++ b/api/models/dto/agencyDto.ts @@ -0,0 +1,7 @@ +export default interface AgencyDto { + id: string; + name: string; + locationId: string; + taxonomyId: string; + } + \ No newline at end of file diff --git a/api/services/agencyService.ts b/api/services/agencyService.ts new file mode 100644 index 0000000..72af8cd --- /dev/null +++ b/api/services/agencyService.ts @@ -0,0 +1,21 @@ +import { makeSCOSRequest } from './scosService'; +import getLogger from '../utils/logger'; +import AgencyDto from '../models/dto/AgencyDto'; +import { AGENCIES_TABLE } from '../utils/constants'; +import { NotFoundError } from 'typescript-rest/dist/server/model/errors'; + +const log = getLogger('agencyService'); + +async function getAgencies(id: string): Promise { + log.debug(`Getting agencies with ID ${id}`); + + const query = `SELECT * FROM ${AGENCIES_TABLE} WHERE id = '${id}'`; + const response: AgencyDto[] = await makeSCOSRequest(query); + + if (response?.length === 0) { + throw new NotFoundError(`Invalid category ID ${id}`); + } + return response; +} + +export { getAgencies };