diff --git a/.env.example b/.env.example index 4c90452..26b377f 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,4 @@ DATABASE_URL=postgresql://admin:admin@localhost:5432/codebench?schema=public JWT_SECRET=secret +REDIS_HOST=127.0.0.1 +REDIS_PORT=6379 diff --git a/src/app.module.ts b/src/app.module.ts index 2172553..d488986 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -18,13 +18,13 @@ import { UsersModule } from './users/users.module'; SubmissionsModule, BullModule.forRoot({ redis: { - host: 'localhost', - port: 6378, + host: process.env.REDIS_HOST || 'localhost', + port: Number(process.env.REDIS_PORT) || 6379, }, }), RedisModule.register({ - host: 'localhost', - port: 6378, + host: process.env.REDIS_HOST || 'localhost', + port: Number(process.env.REDIS_PORT) || 6379, }), ], controllers: [AppController], diff --git a/src/users/dto/find-user.dto.ts b/src/users/dto/find-user.dto.ts index 92c211a..6ef56d3 100644 --- a/src/users/dto/find-user.dto.ts +++ b/src/users/dto/find-user.dto.ts @@ -1,9 +1,8 @@ -import { IsString } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; +import { IsString } from 'class-validator'; export class FindUserDTO { @ApiProperty() @IsString() username: string; } -export default FindUserDTO; diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index a512ce0..ed54e35 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -1,5 +1,22 @@ -import { Body, ConflictException, Controller, Post } from '@nestjs/common'; +import { + Body, + ConflictException, + Controller, + Get, + NotFoundException, + Param, + Post, + UseGuards, +} from '@nestjs/common'; +import { + ApiBearerAuth, + ApiOkResponse, + ApiOperation, + ApiParam, +} from '@nestjs/swagger'; +import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'; import { CreateUserDTO } from './dto/create-user.dto'; +import { FindUserDTO } from './dto/find-user.dto'; import { User } from './user.entity'; import { UsersService } from './users.service'; @@ -22,4 +39,24 @@ export class UsersController { return this.usersService.create(user); } + + @ApiOperation({ summary: 'Get a user' }) + @ApiParam({ name: 'username', type: FindUserDTO }) + @ApiOkResponse({ type: User, description: 'User object' }) + // @ApiBadRequestResponse({ + // type: BadRequestResponse, + // description: 'Invalid input', + // }) + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':username') + async findOne(@Param() userReq: { username: string }): Promise { + const user: User | undefined = await this.usersService.findOne(userReq); + + if (!user) { + throw new NotFoundException('User not found'); + } + + return user; + } }