Skip to content

Commit

Permalink
feat: 유저 - 냥이 매핑 테이블 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondanythings committed Dec 27, 2023
1 parent 9ab8ed2 commit 473708d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
9 changes: 8 additions & 1 deletion src/common/common.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -48,6 +53,7 @@ export class CommonController {
description: '유저냥이 정보 조회 성공',
type: [UserCatDto],
})
@ApiBearerAuth()
@Get('user-cat')
@UseGuards(AccessGuard)
async getUserCatData(@AuthUser() { id }: Payload) {
Expand All @@ -62,6 +68,7 @@ export class CommonController {
description: '유저냥이 악세사리 변경 성공',
type: [UserCatDto],
})
@ApiBearerAuth()
@Patch('accessory')
@UseGuards(AccessGuard)
async updateUserCatAccessory(
Expand Down
31 changes: 9 additions & 22 deletions src/common/common.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,16 @@ export class CommonService {
}

async findUserCats(id: string): Promise<UserCatDto[]> {
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) {
Expand Down
22 changes: 21 additions & 1 deletion src/common/entities/user-cat.entity.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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: '고양이아이디',
Expand All @@ -19,11 +28,22 @@ 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: '악세사리아이디',
unique: false,
nullable: true,
})
accessoryId: string;
@ManyToOne(() => Accessories, (accessories) => accessories.id, {
createForeignKeyConstraints: false,
})
@JoinColumn({ name: 'accessory_id' })
accessory: Accessories;
}

0 comments on commit 473708d

Please sign in to comment.