Skip to content

Commit

Permalink
Merge pull request #154 from boostcampwm2023/feat/re-enter-room
Browse files Browse the repository at this point in the history
feat/re-enter-room : 방 재참가 고려
  • Loading branch information
sjn0910 authored Dec 6, 2023
2 parents 1e2a6b3 + 7b8302a commit 99265cc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 61 deletions.
2 changes: 0 additions & 2 deletions server/src/auth/auth.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
12 changes: 0 additions & 12 deletions server/src/room/dto/create.room.dto.ts

This file was deleted.

1 change: 0 additions & 1 deletion server/src/room/room.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class RoomController {

constructor(private readonly roomService: RoomService) {}

// TODO: USER가 없을 경우도 처리
@ApiResponse({
status: 400,
description: '이미 참가하고 있는 방이 있는데 방 생성을 시도한 경우',
Expand Down
40 changes: 12 additions & 28 deletions server/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
const code = await this.createRoomCode(user.username);
const room = await this.roomRepository
.create({
code,
host: user,
})
.save();
await this.roomUserService.createOrRestoreRoomUser({ room, user });

await Promise.all([room.save(), roomUser.save(), user.save()]);
return room;
}

Expand All @@ -70,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('존재하지 않는 방입니다.');
Expand All @@ -92,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.createOrRestoreRoomUser({ room, user });
}

async exitRoom(userSession: User) {
Expand All @@ -108,7 +93,6 @@ export class RoomService {
provider,
providerId,
});

if (!user) throw new BadRequestException('존재하지 않는 유저입니다.');

this.logger.debug('user from db:', util.inspect(user));
Expand All @@ -120,6 +104,6 @@ export class RoomService {
throw new InternalServerErrorException('참가 중인 방이 여러 개입니다.');

const roomUser = user.joinedRooms[0];
await roomUser.softRemove();
return await roomUser.softRemove();
}
}
17 changes: 14 additions & 3 deletions server/src/roomUser/room.user.service.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -11,7 +11,18 @@ export class RoomUserService {
private readonly roomUserRepository: Repository<RoomUser>,
) {}

async createRoomUser(createRoomUserInput: CreateRoomUserInput) {
return this.roomUserRepository.create(createRoomUserInput).save();
async createOrRestoreRoomUser(roomUserInput: RoomUserInput) {
const { room, user } = roomUserInput;
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(roomUserInput).save();
}
}
2 changes: 1 addition & 1 deletion server/src/types/roomUser.ts
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 3 additions & 14 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -45,11 +41,4 @@ export class UserService {
where: providerInfo,
});
}

async findUserWithActiveRoomById(id: number) {
return this.userRepository.findOne({
where: { id },
relations: ['joinedRooms'],
});
}
}

0 comments on commit 99265cc

Please sign in to comment.