From 8d4c658412d32a5ec00b45a6dfc34bcccd832b9e Mon Sep 17 00:00:00 2001 From: Yeongbin Im <56269396+yeongbinim@users.noreply.github.com> Date: Wed, 22 Nov 2023 22:58:38 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20categories=20api=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EC=95=84=EC=9D=B4=EB=94=94=20=EA=B0=80=EB=93=9C=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20(#166)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/app.module.ts | 3 +- BE/src/auth/auth.controller.ts | 2 +- BE/src/auth/auth.module.ts | 4 ++- BE/src/{ => auth}/google.strategy.ts | 2 +- BE/src/categories/categories.controller.ts | 32 +++++++++++++------ BE/src/categories/categories.module.ts | 4 ++- BE/src/categories/categories.service.ts | 12 ++++--- .../dto/request/create-categories.dto.ts | 7 ---- .../dto/request/get-categories.dto.ts | 10 ------ .../dto/request/update-categories.dto.ts | 7 ---- .../categories/dto/response/category.dto.ts | 6 ---- BE/src/main.ts | 2 +- 12 files changed, 40 insertions(+), 51 deletions(-) rename BE/src/{ => auth}/google.strategy.ts (93%) delete mode 100644 BE/src/categories/dto/request/get-categories.dto.ts diff --git a/BE/src/app.module.ts b/BE/src/app.module.ts index 19439ec..daedccf 100644 --- a/BE/src/app.module.ts +++ b/BE/src/app.module.ts @@ -10,7 +10,6 @@ import { CategoriesModule } from './categories/categories.module'; import { MatesModule } from './mates/mates.module'; import { UsersModule } from './users/users.module'; import { UsersModel } from './users/entity/users.entity'; -import { GoogleStrategy } from './google.strategy'; import { PassportModule } from '@nestjs/passport'; import { AuthModule } from './auth/auth.module'; @@ -40,6 +39,6 @@ import { AuthModule } from './auth/auth.module'; AuthModule, ], controllers: [AppController], - providers: [AppService, GoogleStrategy], + providers: [AppService], }) export class AppModule {} diff --git a/BE/src/auth/auth.controller.ts b/BE/src/auth/auth.controller.ts index cbe9646..4007083 100644 --- a/BE/src/auth/auth.controller.ts +++ b/BE/src/auth/auth.controller.ts @@ -29,10 +29,10 @@ export class AuthController { @Get('logout') @UseGuards(AccessTokenGuard) + @ApiBearerAuth() @ApiOperation({ summary: '로그아웃' }) @ApiResponse({ status: 200, description: '로그아웃 성공' }) @ApiResponse({ status: 401, description: '인증 실패' }) - @ApiBearerAuth() logout(@User('id') userId: number, @Res() res: Response) { console.log(`${userId}를 로그아웃 시키는 로직`); res.redirect('/'); diff --git a/BE/src/auth/auth.module.ts b/BE/src/auth/auth.module.ts index 674645e..84457b5 100644 --- a/BE/src/auth/auth.module.ts +++ b/BE/src/auth/auth.module.ts @@ -4,6 +4,7 @@ import { AuthService } from './auth.service'; import { AuthController } from './auth.controller'; import { UsersModule } from 'src/users/users.module'; import { JwtModule } from '@nestjs/jwt'; +import { GoogleStrategy } from './google.strategy'; @Module({ imports: [ @@ -19,6 +20,7 @@ import { JwtModule } from '@nestjs/jwt'; UsersModule, ], controllers: [AuthController], - providers: [AuthService], + providers: [AuthService, GoogleStrategy], + exports: [AuthService], }) export class AuthModule {} diff --git a/BE/src/google.strategy.ts b/BE/src/auth/google.strategy.ts similarity index 93% rename from BE/src/google.strategy.ts rename to BE/src/auth/google.strategy.ts index 0b01348..fdf44a4 100644 --- a/BE/src/google.strategy.ts +++ b/BE/src/auth/google.strategy.ts @@ -9,7 +9,7 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { super({ clientID: config.get('GOOGLE_CLIENT_ID'), clientSecret: config.get('GOOGLE_CLIENT_SECRET'), - callbackURL: 'http://localhost:3000/auth/google', + callbackURL: config.get('GOOGLE_CALLBACK_URL'), scope: ['email'], }); } diff --git a/BE/src/categories/categories.controller.ts b/BE/src/categories/categories.controller.ts index 62cf9ed..97f1625 100644 --- a/BE/src/categories/categories.controller.ts +++ b/BE/src/categories/categories.controller.ts @@ -6,20 +6,22 @@ import { Param, Patch, Post, - Headers, + UseGuards, } from '@nestjs/common'; import { ApiBadRequestResponse, + ApiBearerAuth, ApiCreatedResponse, ApiOperation, ApiParam, ApiTags, } from '@nestjs/swagger'; import { CategoriesService } from './categories.service'; -import { CategoryGetDto } from './dto/request/get-categories.dto'; import { CategoryCreateDto } from './dto/request/create-categories.dto'; import { CategoryUpdateDto } from './dto/request/update-categories.dto'; import { CategoryDto } from './dto/response/category.dto'; +import { AccessTokenGuard } from 'src/auth/guard/bearer-token.guard'; +import { User } from 'src/users/decorator/user.decorator'; @ApiTags('Categories') @Controller('categories') @@ -27,6 +29,8 @@ export class CategoriesController { constructor(private readonly categoriesService: CategoriesService) {} @Get() + @UseGuards(AccessTokenGuard) + @ApiBearerAuth() @ApiOperation({ summary: '카테고리 조회' }) @ApiCreatedResponse({ type: [CategoryDto], @@ -35,14 +39,15 @@ export class CategoriesController { @ApiBadRequestResponse({ description: '잘못된 요청입니다.', }) - getCategories( - @Headers('authorization') CategoryGetDto, - ): Promise { + getCategories(@User('id') user_id: number): Promise { // TODO: 유저 id를 받아올 방식 정하기 + user_id; return this.categoriesService.findByUserId(1); } @Post() + @UseGuards(AccessTokenGuard) + @ApiBearerAuth() @ApiOperation({ summary: '카테고리 생성' }) @ApiCreatedResponse({ type: CategoryDto, @@ -52,12 +57,15 @@ export class CategoriesController { description: '잘못된 요청입니다.', }) createCategories( + @User('id') user_id: number, @Body() categoriesData: CategoryCreateDto, ): Promise { - return this.categoriesService.create(categoriesData); + return this.categoriesService.create(user_id, categoriesData); } @Patch(':category_id') + @UseGuards(AccessTokenGuard) + @ApiBearerAuth() @ApiOperation({ summary: '카테고리 수정' }) @ApiParam({ name: 'category_id', @@ -73,13 +81,16 @@ export class CategoriesController { description: '잘못된 요청입니다.', }) updateCategories( + @User('id') user_id: number, @Body() categoriesData: CategoryUpdateDto, @Param('category_id') category_id: number, ): Promise { - return this.categoriesService.update(categoriesData, category_id); + return this.categoriesService.update(user_id, categoriesData, category_id); } @Delete(':category_id') + @UseGuards(AccessTokenGuard) + @ApiBearerAuth() @ApiOperation({ summary: '카테고리 삭제' }) @ApiParam({ name: 'category_id', @@ -93,7 +104,10 @@ export class CategoriesController { @ApiBadRequestResponse({ description: '잘못된 요청입니다.', }) - deleteCategories(@Param('category_id') category_id: number): Promise { - return this.categoriesService.remove(category_id); + deleteCategories( + @User('id') user_id: number, + @Param('category_id') category_id: number, + ): Promise { + return this.categoriesService.remove(user_id, category_id); } } diff --git a/BE/src/categories/categories.module.ts b/BE/src/categories/categories.module.ts index e70f545..dd76ea3 100644 --- a/BE/src/categories/categories.module.ts +++ b/BE/src/categories/categories.module.ts @@ -3,9 +3,11 @@ import { CategoriesService } from './categories.service'; import { CategoriesController } from './categories.controller'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Categories } from './categories.entity'; +import { AuthModule } from 'src/auth/auth.module'; +import { UsersModule } from 'src/users/users.module'; @Module({ - imports: [TypeOrmModule.forFeature([Categories])], + imports: [TypeOrmModule.forFeature([Categories]), AuthModule, UsersModule], providers: [CategoriesService], controllers: [CategoriesController], }) diff --git a/BE/src/categories/categories.service.ts b/BE/src/categories/categories.service.ts index 8920bbc..49992d3 100644 --- a/BE/src/categories/categories.service.ts +++ b/BE/src/categories/categories.service.ts @@ -14,11 +14,13 @@ export class CategoriesService { private categoriesRepository: Repository, ) {} - async create(categoriesData: CategoryCreateDto): Promise { - const { user_id, ...data } = categoriesData; + async create( + user_id: number, + categoriesData: CategoryCreateDto, + ): Promise { const user = { id: user_id } as UsersModel; const category = this.categoriesRepository.create({ - ...data, + ...categoriesData, user_id: user, }); const savedCategory = await this.categoriesRepository.save(category); @@ -41,6 +43,7 @@ export class CategoriesService { } async update( + user_id: number, categoriesData: CategoryUpdateDto, id: number, ): Promise { @@ -54,7 +57,7 @@ export class CategoriesService { return this.entityToDto(updatedCategory); } - async remove(id: number): Promise { + async remove(user_id: number, id: number): Promise { const result = await this.categoriesRepository.delete(id); if (result.affected === 0) { throw new NotFoundException('해당 카테고리가 존재하지 않습니다.'); @@ -64,7 +67,6 @@ export class CategoriesService { entityToDto(category: Categories): CategoryDto { const categoryDto: CategoryDto = { id: category.id, - user_id: category.user_id.id, name: category.name, color_code: category.color_code, }; diff --git a/BE/src/categories/dto/request/create-categories.dto.ts b/BE/src/categories/dto/request/create-categories.dto.ts index fb95527..ef6e754 100644 --- a/BE/src/categories/dto/request/create-categories.dto.ts +++ b/BE/src/categories/dto/request/create-categories.dto.ts @@ -1,13 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; export class CategoryCreateDto { - @ApiProperty({ - type: 'number', - example: '1', - description: '유저 id', - }) - user_id: number; - @ApiProperty({ type: 'string', example: '백준', diff --git a/BE/src/categories/dto/request/get-categories.dto.ts b/BE/src/categories/dto/request/get-categories.dto.ts deleted file mode 100644 index 7635428..0000000 --- a/BE/src/categories/dto/request/get-categories.dto.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; - -export class CategoryGetDto { - @ApiProperty({ - type: 'number', - example: '1', - description: '유저 id', - }) - user_id: number; -} diff --git a/BE/src/categories/dto/request/update-categories.dto.ts b/BE/src/categories/dto/request/update-categories.dto.ts index 3b1ce51..15fab05 100644 --- a/BE/src/categories/dto/request/update-categories.dto.ts +++ b/BE/src/categories/dto/request/update-categories.dto.ts @@ -1,13 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; export class CategoryUpdateDto { - @ApiProperty({ - type: 'number', - example: '1', - description: '유저 id', - }) - user_id: number; - @ApiProperty({ type: 'string', example: '백준', diff --git a/BE/src/categories/dto/response/category.dto.ts b/BE/src/categories/dto/response/category.dto.ts index 27b2fae..770e5e8 100644 --- a/BE/src/categories/dto/response/category.dto.ts +++ b/BE/src/categories/dto/response/category.dto.ts @@ -7,12 +7,6 @@ export class CategoryDto { description: '카테고리 id', }) id: number; - @ApiProperty({ - type: 'number', - example: '1', - description: '유저 id', - }) - user_id: number; @ApiProperty({ type: 'string', diff --git a/BE/src/main.ts b/BE/src/main.ts index dbf54dc..1ecb4ad 100644 --- a/BE/src/main.ts +++ b/BE/src/main.ts @@ -30,6 +30,6 @@ async function bootstrap() { app.useGlobalPipes(new ValidationPipe()); - await app.listen(configService.get('PORT')); + await app.listen(configService.get('PORT') || 3000); } bootstrap();