From 3aa8c079792e55517134531a634e04188276938c Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 17:36:18 +0900 Subject: [PATCH 1/9] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20db=20=EC=A1=B0=ED=9A=8C=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20in=20auth.strategy.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/auth/auth.strategy.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/src/auth/auth.strategy.ts b/server/src/auth/auth.strategy.ts index e97226b..e5998a0 100644 --- a/server/src/auth/auth.strategy.ts +++ b/server/src/auth/auth.strategy.ts @@ -37,8 +37,6 @@ export class GithubStrategy extends PassportStrategy(StrategyGithub, 'github') { avatarUrl: _json!.avatar_url!, }; - this.userService.findUserByProviderInfo({ provider, providerId: id }); - return this.userService.createOrUpdateUser(user); } } From f15f30f3904d0a26e9444f62903bbe00b89f38ce Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 17:39:12 +0900 Subject: [PATCH 2/9] =?UTF-8?q?refactor:=20createOrUpdateUser=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=A1=B0=EA=B8=88=20=EB=8D=94=20=EA=B9=94=EB=81=94?= =?UTF-8?q?=ED=95=98=EA=B2=8C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/user/user.service.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/server/src/user/user.service.ts b/server/src/user/user.service.ts index e93ccc7..c808d9d 100644 --- a/server/src/user/user.service.ts +++ b/server/src/user/user.service.ts @@ -24,13 +24,9 @@ export class UserService { const { provider, providerId } = createUserDto; const user = await this.findUserByProviderInfo({ provider, providerId }); - if (user) { - // TODO: check if user info is actually updated by the ORM - await this.userRepository.update(user.id, createUserDto); - return this.userRepository.findOneBy({ id: user.id }); - } else { - return this.userRepository.create(createUserDto).save(); - } + return user + ? this.userRepository.save({ ...user, ...createUserDto }) + : this.userRepository.create(createUserDto).save(); } async findUserByProviderInfoWithRooms(providerInfo: ProviderInfo) { From 5ee62c9ea65192e987120d35dc4395b0ff5a5256 Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 17:40:41 +0900 Subject: [PATCH 3/9] =?UTF-8?q?refactor:=20createRoom=20dto=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/room/dto/create.room.dto.ts | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 server/src/room/dto/create.room.dto.ts diff --git a/server/src/room/dto/create.room.dto.ts b/server/src/room/dto/create.room.dto.ts deleted file mode 100644 index 613d4ca..0000000 --- a/server/src/room/dto/create.room.dto.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { IsNotEmpty, IsNumber } from 'class-validator'; - -export class CreateRoomDto { - @ApiProperty({ - example: 1, - required: true, - }) - @IsNumber() - @IsNotEmpty() - userId?: number; -} From 2066d641c7cdfc00aeb6b28f4e94ff32ced0e026 Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 17:44:10 +0900 Subject: [PATCH 4/9] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EB=90=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20findUserWithActiveRoomById=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/user/user.service.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/server/src/user/user.service.ts b/server/src/user/user.service.ts index c808d9d..4b41764 100644 --- a/server/src/user/user.service.ts +++ b/server/src/user/user.service.ts @@ -41,11 +41,4 @@ export class UserService { where: providerInfo, }); } - - async findUserWithActiveRoomById(id: number) { - return this.userRepository.findOne({ - where: { id }, - relations: ['joinedRooms'], - }); - } } From a12b8ab4baeb54288c6659ee19c391f0faa06501 Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 18:41:12 +0900 Subject: [PATCH 5/9] =?UTF-8?q?refactor:=20create=20room=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/room/room.controller.ts | 1 - server/src/room/room.service.ts | 26 +++++++++----------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/server/src/room/room.controller.ts b/server/src/room/room.controller.ts index b24cb36..ea3074a 100644 --- a/server/src/room/room.controller.ts +++ b/server/src/room/room.controller.ts @@ -11,7 +11,6 @@ export class RoomController { constructor(private readonly roomService: RoomService) {} - // TODO: USER가 없을 경우도 처리 @ApiResponse({ status: 400, description: '이미 참가하고 있는 방이 있는데 방 생성을 시도한 경우', diff --git a/server/src/room/room.service.ts b/server/src/room/room.service.ts index d121a39..3b4b91b 100644 --- a/server/src/room/room.service.ts +++ b/server/src/room/room.service.ts @@ -7,12 +7,11 @@ import { import { InjectRepository } from '@nestjs/typeorm'; import * as crypto from 'crypto'; import Room from 'src/entities/room.entity'; +import User from 'src/entities/user.entity'; import { RoomUserService } from 'src/roomUser/room.user.service'; import { UserService } from 'src/user/user.service'; import { Repository } from 'typeorm'; -import User from 'src/entities/user.entity'; import * as util from 'util'; -import RoomUser from '../entities/roomUser.entity'; @Injectable() export class RoomService { @@ -37,27 +36,20 @@ export class RoomService { if (!user) { throw new InternalServerErrorException('유저를 찾을 수 없습니다.'); } - if (user.joinedRooms && user.joinedRooms.length > 0) throw new BadRequestException('이미 방에 참가 중입니다.'); if (user.username == null) throw new BadRequestException('username이 없습니다.'); const roomCode = await this.createRoomCode(user.username); - - const room = this.roomRepository.create({ - code: roomCode, - host: user, - }); - - const roomUser = new RoomUser(); - - user.joinedRooms = [roomUser]; - room.joinedUsers = [roomUser]; - roomUser.room = room; - roomUser.user = user; - - await Promise.all([room.save(), roomUser.save(), user.save()]); + const room = await this.roomRepository + .create({ + code: roomCode, + host: user, + }) + .save(); + + await this.roomUserService.createRoomUser({ room, user }); return room; } From fa9d5fb1bd7c109100596af810d7b33697d8090c Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 18:47:53 +0900 Subject: [PATCH 6/9] =?UTF-8?q?refactor:=20join=20room=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/room/room.service.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/server/src/room/room.service.ts b/server/src/room/room.service.ts index 3b4b91b..c013c55 100644 --- a/server/src/room/room.service.ts +++ b/server/src/room/room.service.ts @@ -41,10 +41,10 @@ export class RoomService { if (user.username == null) throw new BadRequestException('username이 없습니다.'); - const roomCode = await this.createRoomCode(user.username); + const code = await this.createRoomCode(user.username); const room = await this.roomRepository .create({ - code: roomCode, + code, host: user, }) .save(); @@ -62,17 +62,16 @@ export class RoomService { async addUserToRoom(userSession: User, roomCode: string) { const { provider, providerId } = userSession; + const user = await this.userService.findUserByProviderInfo({ provider, providerId, }); - if (!user) throw new BadRequestException('존재하지 않는 유저입니다.'); const room = await this.roomRepository.findOne({ where: { code: roomCode }, }); - if (!room) { this.logger.debug(`room with ${roomCode} does not exist!`); throw new BadRequestException('존재하지 않는 방입니다.'); @@ -84,13 +83,7 @@ export class RoomService { ) throw new BadRequestException('이미 참가한 방입니다.'); - if (room.joinedUsers) { - room.joinedUsers.push(user); - } else { - room.joinedUsers = [user]; - } - - return room.save(); + return await this.roomUserService.createRoomUser({ room, user }); } async exitRoom(userSession: User) { @@ -100,7 +93,6 @@ export class RoomService { provider, providerId, }); - if (!user) throw new BadRequestException('존재하지 않는 유저입니다.'); this.logger.debug('user from db:', util.inspect(user)); From 5af444e2326ef3f1d42199d8ef1c0524693edf76 Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 18:52:59 +0900 Subject: [PATCH 7/9] =?UTF-8?q?refactor:=20exit=20room=EC=97=90=20return?= =?UTF-8?q?=20=EA=B0=92=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/room/room.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/room/room.service.ts b/server/src/room/room.service.ts index c013c55..780fc86 100644 --- a/server/src/room/room.service.ts +++ b/server/src/room/room.service.ts @@ -104,6 +104,6 @@ export class RoomService { throw new InternalServerErrorException('참가 중인 방이 여러 개입니다.'); const roomUser = user.joinedRooms[0]; - await roomUser.softRemove(); + return await roomUser.softRemove(); } } From 3acd06960f73478853333fcfb9a45138ae250db1 Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 19:19:53 +0900 Subject: [PATCH 8/9] =?UTF-8?q?feat:=20=EB=B0=A9=20=EC=9E=AC=EC=B0=B8?= =?UTF-8?q?=EA=B0=80=EC=9D=98=20=EA=B2=BD=EC=9A=B0=EB=8F=84=20=EA=B3=A0?= =?UTF-8?q?=EB=A0=A4=ED=95=98=EC=97=AC=20createOrRestoreRoomUser=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/room/room.service.ts | 4 ++-- server/src/roomUser/room.user.service.ts | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/server/src/room/room.service.ts b/server/src/room/room.service.ts index 780fc86..edefb2a 100644 --- a/server/src/room/room.service.ts +++ b/server/src/room/room.service.ts @@ -48,8 +48,8 @@ export class RoomService { host: user, }) .save(); + await this.roomUserService.createOrRestoreRoomUser({ room, user }); - await this.roomUserService.createRoomUser({ room, user }); return room; } @@ -83,7 +83,7 @@ export class RoomService { ) throw new BadRequestException('이미 참가한 방입니다.'); - return await this.roomUserService.createRoomUser({ room, user }); + return await this.roomUserService.createOrRestoreRoomUser({ room, user }); } async exitRoom(userSession: User) { diff --git a/server/src/roomUser/room.user.service.ts b/server/src/roomUser/room.user.service.ts index f30602d..08952ba 100644 --- a/server/src/roomUser/room.user.service.ts +++ b/server/src/roomUser/room.user.service.ts @@ -11,7 +11,18 @@ export class RoomUserService { private readonly roomUserRepository: Repository, ) {} - async createRoomUser(createRoomUserInput: CreateRoomUserInput) { - return this.roomUserRepository.create(createRoomUserInput).save(); + async createOrRestoreRoomUser(createRoomUserInput: CreateRoomUserInput) { + const { room, user } = createRoomUserInput; + const roomUser = await this.roomUserRepository.findOne({ + withDeleted: true, + where: { + room: { id: room.id }, + user: { id: user.id }, + }, + }); + + return roomUser + ? this.roomUserRepository.recover(roomUser) + : this.roomUserRepository.create(createRoomUserInput).save(); } } From 7b8302adf6385ef86cb3f43115e81bba5e244eb4 Mon Sep 17 00:00:00 2001 From: sjn0910 Date: Tue, 5 Dec 2023 19:24:43 +0900 Subject: [PATCH 9/9] =?UTF-8?q?feat:=20dto=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/roomUser/room.user.service.ts | 8 ++++---- server/src/types/roomUser.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/roomUser/room.user.service.ts b/server/src/roomUser/room.user.service.ts index 08952ba..a306b8a 100644 --- a/server/src/roomUser/room.user.service.ts +++ b/server/src/roomUser/room.user.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import RoomUser from 'src/entities/roomUser.entity'; -import { CreateRoomUserInput } from 'src/types/roomUser'; +import { RoomUserInput } from 'src/types/roomUser'; import { Repository } from 'typeorm'; @Injectable() @@ -11,8 +11,8 @@ export class RoomUserService { private readonly roomUserRepository: Repository, ) {} - async createOrRestoreRoomUser(createRoomUserInput: CreateRoomUserInput) { - const { room, user } = createRoomUserInput; + async createOrRestoreRoomUser(roomUserInput: RoomUserInput) { + const { room, user } = roomUserInput; const roomUser = await this.roomUserRepository.findOne({ withDeleted: true, where: { @@ -23,6 +23,6 @@ export class RoomUserService { return roomUser ? this.roomUserRepository.recover(roomUser) - : this.roomUserRepository.create(createRoomUserInput).save(); + : this.roomUserRepository.create(roomUserInput).save(); } } diff --git a/server/src/types/roomUser.ts b/server/src/types/roomUser.ts index 677400b..3822891 100644 --- a/server/src/types/roomUser.ts +++ b/server/src/types/roomUser.ts @@ -1,7 +1,7 @@ import Room from 'src/entities/room.entity'; import User from 'src/entities/user.entity'; -export interface CreateRoomUserInput { +export interface RoomUserInput { room: Room; user: User; }