diff --git a/src/app.module.ts b/src/app.module.ts index 26fda79..edd0a7b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -30,6 +30,7 @@ import { JunkDrawerModule } from './junk-drawer/junk-drawer.module'; import { EmailModule } from './email/email.module'; import { ContactModule } from './contact/contact.module'; import { HoardingModule } from './hoarding/hoarding.module'; +import { NamesModule } from './names/names.module'; @Module({ imports: [ @@ -106,6 +107,7 @@ import { HoardingModule } from './hoarding/hoarding.module'; EmailModule, ContactModule, HoardingModule, + NamesModule, ], controllers: [AppController], providers: [ diff --git a/src/names/names.controller.ts b/src/names/names.controller.ts new file mode 100644 index 0000000..f07c35d --- /dev/null +++ b/src/names/names.controller.ts @@ -0,0 +1,26 @@ +import { Controller, Get, Param } from '@nestjs/common'; +import { NamesService } from './names.service'; + +@Controller('names') +export class NamesController { + constructor(private readonly namesService: NamesService) {} + + @Get() + async getNameList() { + return await this.namesService.getNameList(); + } + + @Get(':name') + async getNameInformation(@Param('name') name: string) { + return { + popularity: await this.namesService.getSsaNameData(name), + otherSites: { + behindTheName: `https://www.behindthename.com/name/${name.toLocaleLowerCase()}`, + babynames: `https://www.babynames.com/name/${name.toLocaleLowerCase()}`, + }, + behindTheName: { + synonyms: await this.namesService.getBtnSynonyms(name), + }, + }; + } +} diff --git a/src/names/names.module.ts b/src/names/names.module.ts new file mode 100644 index 0000000..e27b2c2 --- /dev/null +++ b/src/names/names.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { NamesService } from './names.service'; +import { NamesController } from './names.controller'; +import { MinioService } from 'src/minio/minio.service'; + +@Module({ + providers: [MinioService, NamesService], + controllers: [NamesController], +}) +export class NamesModule {} diff --git a/src/names/names.service.ts b/src/names/names.service.ts new file mode 100644 index 0000000..2a0a753 --- /dev/null +++ b/src/names/names.service.ts @@ -0,0 +1,41 @@ +import { Injectable } from '@nestjs/common'; +import { MinioService } from 'src/minio/minio.service'; + +@Injectable() +export class NamesService { + constructor(private readonly minioService: MinioService) {} + + async getNameList(): Promise { + return JSON.parse( + ( + await this.minioService.getCachedBuffer( + 'cdn-source', + 'baby-name-data/list.json', + ) + ).toString(), + ); + } + + async getSsaNameData(name: string): Promise { + return JSON.parse( + ( + await this.minioService.getCachedBuffer( + 'cdn-source', + `baby-name-data/individual/${name}.json`, + ) + ).toString(), + ); + } + + async getBtnSynonyms(name: string): Promise { + const synonymData = JSON.parse( + ( + await this.minioService.getCachedBuffer( + 'cdn-source', + `baby-name-data/btn_synonyms.json`, + ) + ).toString(), + ); + return synonymData[name]?.synonyms || []; + } +}