Skip to content

Commit

Permalink
Added: Search API for Tags and Body (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
radhay-samagra authored Jul 12, 2022
1 parent c7a28d5 commit d778fd1
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
pgdata
dist
dist
node_modules/
2 changes: 2 additions & 0 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ model Template {
Audit Audit[]
ExecutionAudit ExecutionAudit[]
transformers TransformerPathMapping[]
@@index([tag])
}

model Audit {
Expand Down
2 changes: 2 additions & 0 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { EjsService } from './engines/ejs/ejs.service';
import { VMService } from './core/lambda/vm.service';
import { SingletonServiceModule } from './singletonService.module';
import { I18nController } from './core/i18n/i18n/i18n.controller';
import { SearchController } from './core/search/search.controller';

@Module({
imports: [SingletonServiceModule],
Expand All @@ -23,6 +24,7 @@ import { I18nController } from './core/i18n/i18n/i18n.controller';
TemplateController,
LambdaService,
I18nController,
SearchController,
],
exports: [VMService],
providers: [
Expand Down
4 changes: 2 additions & 2 deletions api/src/core/i18n/i18n/i18n.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
import { Body, Controller, Post } from '@nestjs/common';
import { Template, TemplateType, Prisma } from '@prisma/client';
import { PrismaService } from '../../../prisma.service';
import { TransformerService } from '../../transformer/transformer.service';
import { RenderDto, RenderDtoTest, RenderResponse } from '../../dto/render';
import { RenderDto, RenderResponse } from '../../dto/render';
import { JsTLService } from 'src/engines/jstl/jstl.service';
import { TemplateService } from '../../template/template.service';
import { JinjaService } from 'src/engines/jinja/jinja.service';
Expand Down
18 changes: 18 additions & 0 deletions api/src/core/search/search.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { SearchController } from './search.controller';

describe('SearchController', () => {
let controller: SearchController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [SearchController],
}).compile();

controller = module.get<SearchController>(SearchController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
21 changes: 21 additions & 0 deletions api/src/core/search/search.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable prettier/prettier */
import { Controller, Get, Query } from '@nestjs/common';
import { Template } from '@prisma/client';
import { TemplateService } from '../template/template.service';

@Controller('search')
export class SearchController {
constructor(
private readonly templateService: TemplateService,
){}

@Get('tag')
async searchTag(@Query() queryString: string): Promise<Template[]>{
return this.templateService.searchTag(queryString);
}

@Get('body')
async searchTemplateBody(@Query() queryString: string): Promise<Template[]>{
return this.templateService.searchBody(queryString);
}
}
35 changes: 35 additions & 0 deletions api/src/core/template/template.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,39 @@ export class TemplateService {
where,
});
}

async searchTag(queryString: string): Promise<Template[]> {
return this.prisma.template.findMany({
take: 200,
where: {
tag: {
has: queryString,
},
},
});
}

async searchBody(queryString: string): Promise<Template[]> {
return this.prisma.template.findMany({
take: 200,
where: {
OR: [
{
body: {
contains: queryString,
mode: 'insensitive',
},
bodyI18n: {
every: {
body: {
contains: queryString,
mode: 'insensitive',
},
},
},
},
],
},
});
}
}

0 comments on commit d778fd1

Please sign in to comment.