Skip to content

Commit

Permalink
fix: add search function (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirily11 authored May 23, 2023
1 parent cfac1ee commit ae0d200
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@aws-sdk/client-s3": "^3.335.0",
"@aws-sdk/s3-request-presigner": "^3.335.0",
"@golevelup/nestjs-rabbitmq": "^3.6.0",
"@juicyllama/nestjs-redoc": "^2.3.10",
"@nestjs/common": "^9.4.1",
"@nestjs/config": "^2.3.2",
"@nestjs/core": "^9.4.1",
Expand Down
123 changes: 117 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/common/utils/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export const config = {
preSignedUrlExpiration: 60 * 60 * 24, // 24 hours
jwtTokenExpiration: 60 * 60 * 24 * 7, // 7 days
videoLockForSaleDuration: 1, // 30 minutes,
searchVideoLimit: 10, // number of videos returned when searching
};
65 changes: 41 additions & 24 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { config } from 'dotenv';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { AuthService } from './auth/auth.service';
import { RedocModule, RedocOptions } from '@juicyllama/nestjs-redoc';

/**
* This function will build the API document.
* You can check the documentation at path /docs.
* For example: `http://localhost:3000/docs`
* @returns {DocumentBuilder}
*/
function buildApiDocument() {
return new DocumentBuilder()
.setTitle('Video Trading server')
.setDescription('API for Api Trading server')
.addBearerAuth(
// defines the authentication type
// we have defined the permission role called `admin` in this example,
// and to use this admin tag, you can include `@ApiBearerAuth('admin')` in your controller
// and there will be an authentication section shown in the redoc document.
{
description: 'Regular user permission',
type: 'http',
name: 'user',
},
'user',
)
.setVersion('1.0')
.build();
}

async function bootstrap() {
// Create the NestJS application.
Expand All @@ -23,30 +49,21 @@ async function bootstrap() {
// make requests to our API
app.enableCors();

// Setup swagger
// Swagger is a tool that helps us document our API
// It also provides a nice UI that we can use to test our API
const docConfig = new DocumentBuilder()
.setTitle('Video Trading API')
.setVersion('1.0')
.addTag('video')
.addTag('transcoding')
.addTag('playlist')
.addBearerAuth(
{
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
name: 'JWT',
description: 'Enter JWT token',
in: 'header',
},
'JWT-auth', // This name here is important for matching up with @ApiBearerAuth() in your controller!
)
.build();
// Creates the API document configuration object
const config = buildApiDocument();
// Creates the API document using the configuration object
const document = SwaggerModule.createDocument(app, config);

const document = SwaggerModule.createDocument(app, docConfig);
SwaggerModule.setup('api', app, document);
// Setups the redoc document
const redocOptions: RedocOptions = {
// These options are from the redoc document.
// More info: https://github.com/Redocly/redoc
sortPropsAlphabetically: true,
hideDownloadButton: true,
hideHostname: false,
};
// Defines the path where the redoc document will be available
await RedocModule.setup('/docs', app, document, redocOptions);

// get auth token from mondule

Expand Down
1 change: 1 addition & 0 deletions src/video/dto/list-video.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class ListVideoDto {}
13 changes: 13 additions & 0 deletions src/video/dto/search-video.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class SearchVideoResponse {
@ApiProperty()
id: string;

@ApiProperty()
title: string;

@ApiProperty()
thumbnail: string;
}
9 changes: 9 additions & 0 deletions src/video/video.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { PublishVideoDto } from './dto/publish-video.dto';
import { UpdateVideoDto } from './dto/update-video.dto';
import { VideoService } from './video.service';
import { Environments } from '../common/environment';
import { SearchVideoResponse } from './dto/search-video.dto';

@Controller('video')
@ApiTags('video')
Expand Down Expand Up @@ -318,4 +319,12 @@ export class VideoController {
) {
return await this.videoService.findMyVideoDetailById(id, req.user.userId);
}

@Get('search/:keyword')
@ApiOkResponse({
description: 'Search videos by keyword',
})
async searchVideos(@Param('keyword') keyword: string) {
return await this.videoService.searchVideos(keyword);
}
}
Loading

1 comment on commit ae0d200

@vercel
Copy link

@vercel vercel bot commented on ae0d200 May 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.