Skip to content

Commit 12d8acc

Browse files
committed
feat: add GET user/:id route
1 parent aae67e5 commit 12d8acc

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/users/dto/find-user.dto.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { IsString } from 'class-validator';
21
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsString } from 'class-validator';
33

44
export class FindUserDTO {
55
@ApiProperty()
66
@IsString()
77
username: string;
88
}
9-
export default FindUserDTO;

src/users/users.controller.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
import { Body, ConflictException, Controller, Post } from '@nestjs/common';
1+
import {
2+
Body,
3+
ConflictException,
4+
Controller,
5+
Get,
6+
NotFoundException,
7+
Param,
8+
Post,
9+
UseGuards,
10+
} from '@nestjs/common';
11+
import {
12+
ApiBearerAuth,
13+
ApiOkResponse,
14+
ApiOperation,
15+
ApiParam,
16+
} from '@nestjs/swagger';
17+
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
218
import { CreateUserDTO } from './dto/create-user.dto';
19+
import { FindUserDTO } from './dto/find-user.dto';
320
import { User } from './user.entity';
421
import { UsersService } from './users.service';
522

@@ -22,4 +39,24 @@ export class UsersController {
2239

2340
return this.usersService.create(user);
2441
}
42+
43+
@ApiOperation({ summary: 'Get a user' })
44+
@ApiParam({ name: 'username', type: FindUserDTO })
45+
@ApiOkResponse({ type: User, description: 'User object' })
46+
// @ApiBadRequestResponse({
47+
// type: BadRequestResponse,
48+
// description: 'Invalid input',
49+
// })
50+
@ApiBearerAuth()
51+
@UseGuards(JwtAuthGuard)
52+
@Get(':username')
53+
async findOne(@Param() userReq: { username: string }): Promise<User> {
54+
const user: User | undefined = await this.usersService.findOne(userReq);
55+
56+
if (!user) {
57+
throw new NotFoundException('User not found');
58+
}
59+
60+
return user;
61+
}
2562
}

0 commit comments

Comments
 (0)