From 473708d056bdfb6156563b391dbee33cb90140cf Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 27 Dec 2023 23:30:01 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9C=A0=EC=A0=80=20-=20=EB=83=A5?= =?UTF-8?q?=EC=9D=B4=20=EB=A7=A4=ED=95=91=20=ED=85=8C=EC=9D=B4=EB=B8=94=20?= =?UTF-8?q?api=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/common.controller.ts | 9 +++++++- src/common/common.service.ts | 31 ++++++++------------------ src/common/entities/user-cat.entity.ts | 22 +++++++++++++++++- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/common/common.controller.ts b/src/common/common.controller.ts index aec0556..9e6207d 100644 --- a/src/common/common.controller.ts +++ b/src/common/common.controller.ts @@ -1,6 +1,11 @@ import { Body, Controller, Get, Patch, UseGuards } from '@nestjs/common'; import { CommonService } from './common.service'; -import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'; +import { + ApiBearerAuth, + ApiOkResponse, + ApiOperation, + ApiTags, +} from '@nestjs/swagger'; import { CatDTO } from './dto/cat.dto'; import { AccessoryDTO } from './dto/accessory.dto'; import { UserCatDto } from './dto/user-cat.dto'; @@ -48,6 +53,7 @@ export class CommonController { description: '유저냥이 정보 조회 성공', type: [UserCatDto], }) + @ApiBearerAuth() @Get('user-cat') @UseGuards(AccessGuard) async getUserCatData(@AuthUser() { id }: Payload) { @@ -62,6 +68,7 @@ export class CommonController { description: '유저냥이 악세사리 변경 성공', type: [UserCatDto], }) + @ApiBearerAuth() @Patch('accessory') @UseGuards(AccessGuard) async updateUserCatAccessory( diff --git a/src/common/common.service.ts b/src/common/common.service.ts index 7a10033..9835f63 100644 --- a/src/common/common.service.ts +++ b/src/common/common.service.ts @@ -38,29 +38,16 @@ export class CommonService { } async findUserCats(id: string): Promise { - const userCats = await this.dataSource - .getRepository(UserCatEntity) - .find({ where: { userId: id } }); - - const dtoPromises = userCats.map(async (userCatEntity) => { - const catDto = new CatDTO( - await this.dataSource.getRepository(Cats).findOne({ - where: { id: userCatEntity.catId }, - }), - ); - - const accessoryDto = new AccessoryDTO( - userCatEntity.accessoryId - ? await this.dataSource - .getRepository(Accessories) - .findOne({ where: { id: userCatEntity.accessoryId } }) - : undefined, - ); - - return new UserCatDto(catDto, accessoryDto); + const userCats = await this.dataSource.getRepository(UserCatEntity).find({ + where: { userId: id }, + relations: { + cat: true, + accessory: true, + }, }); - - return await Promise.all(dtoPromises); + return userCats.map( + (userCat) => new UserCatDto(userCat.cat, userCat.accessory), + ); } async updateUserCatAccessory(id: string, userCatPatchDto: UserCatPatchDto) { diff --git a/src/common/entities/user-cat.entity.ts b/src/common/entities/user-cat.entity.ts index bfbcd4b..e63043c 100644 --- a/src/common/entities/user-cat.entity.ts +++ b/src/common/entities/user-cat.entity.ts @@ -1,5 +1,8 @@ -import { Column, Entity } from 'typeorm'; +import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm'; import { BaseEntity } from './base.entity'; +import { User } from 'src/users/entities/user.entity'; +import { Cats } from './cats.entity'; +import { Accessories } from './accessories.entity'; @Entity({ name: 'user_cat' }) export class UserCatEntity extends BaseEntity { @@ -11,6 +14,12 @@ export class UserCatEntity extends BaseEntity { }) userId: string; + @ManyToOne(() => User, (user) => user.id, { + createForeignKeyConstraints: false, + }) + @JoinColumn({ name: 'user_id' }) + user: User; + @Column({ name: 'cat_id', comment: '고양이아이디', @@ -19,6 +28,12 @@ export class UserCatEntity extends BaseEntity { }) catId: string; + @ManyToOne(() => Cats, (cats) => cats.id, { + createForeignKeyConstraints: false, + }) + @JoinColumn({ name: 'cat_id' }) + cat: Cats; + @Column({ name: 'accessory_id', comment: '악세사리아이디', @@ -26,4 +41,9 @@ export class UserCatEntity extends BaseEntity { nullable: true, }) accessoryId: string; + @ManyToOne(() => Accessories, (accessories) => accessories.id, { + createForeignKeyConstraints: false, + }) + @JoinColumn({ name: 'accessory_id' }) + accessory: Accessories; }