Skip to content

Commit

Permalink
feat: search API.
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyyuan committed Apr 26, 2023
1 parent 516f1dc commit ab0d6b5
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 17 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
"download-git-repo": "^3.0.2",
"md5": "^2.3.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
"rxjs": "^7.2.0",
"@nestjs/typeorm": "^9.0.1",
"typeorm": "^0.3.15",
"pg": "^8.10.0"
},
"devDependencies": {
"@nestjs/cli": "^9.4.1",
Expand Down
3 changes: 1 addition & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export class AppController {
@Get('/eips/search')
@ApiOperation({ description: 'Search EIPs.' })
async search(@Query() filters: EIPsSearchFilters) {
this.logger.log(filters);
const result = await this.appService.search();
const result = await this.appService.search(filters.content);
return {
data: result,
};
Expand Down
13 changes: 12 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ import { ConfigModule } from '@nestjs/config';
import { PrismaService } from 'src/prisma.service';
import LogsMiddleware from './common/logs.middleware';
import { ScheduleModule } from '@nestjs/schedule';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [ ScheduleModule.forRoot(),ConfigModule.forRoot()],
imports: [
ScheduleModule.forRoot(),
ConfigModule.forRoot(),
TypeOrmModule.forRoot({
type: 'postgres',
url: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
}),
],
controllers: [AppController],
providers: [AppService, PrismaService],
})
Expand Down
47 changes: 41 additions & 6 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import * as fs from 'fs';
import * as download from 'download-git-repo';
import * as path from 'path';

import { DataSource } from 'typeorm';
import { InjectDataSource } from '@nestjs/typeorm';

@Injectable()
export class AppService {
constructor(private prisma: PrismaService) {
constructor(
private prisma: PrismaService,
@InjectDataSource() private readonly connection: DataSource,
) {
mailchimp.setConfig({
apiKey: process.env.MAILCHIMP_API_KEY,
server: 'us17',
Expand Down Expand Up @@ -65,12 +71,41 @@ export class AppService {
};
}

async search() {
const list = await this.prisma.$queryRaw``;
isNumeric(str: any) {
if (typeof str != 'string') return false;
return !isNaN(Number(str)) && !isNaN(parseFloat(str));
}

return {
list,
};
async search(content: string) {
const result = {};

// eip match
if (this.isNumeric(content)) {
const eipRecords = await this.connection.query(
`SELECT eip FROM "EIPs" WHERE eip='${content}'`,
);
if (eipRecords && eipRecords.length > 0) {
result['eip_list'] = eipRecords;
}
}

// title match
const titleRecords = await this.connection.query(
`SELECT eip, ts_headline(title, q), rank FROM (SELECT eip, title, q, ts_rank_cd(title_ts, q) AS rank FROM "EIPs", to_tsquery('${content}') q WHERE title_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);
if (titleRecords && titleRecords.length > 0) {
result['title_list'] = titleRecords;
}

// content match
const contentRecords = await this.connection.query(
`SELECT eip, ts_headline(content, q), rank FROM (SELECT eip, content, q, ts_rank_cd(content_ts, q) AS rank FROM "EIPs", to_tsquery('${content}') q WHERE content_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);
if (contentRecords && contentRecords.length > 0) {
result['content_list'] = contentRecords;
}

return result;
}

isEmail(email): boolean {
Expand Down
Loading

0 comments on commit ab0d6b5

Please sign in to comment.