Skip to content

Commit

Permalink
Merge pull request #199 from Dev-Beom/dev
Browse files Browse the repository at this point in the history
인원 입장 퇴장 관련 API 구성
  • Loading branch information
Dev-Beom authored Nov 18, 2021
2 parents 0eac8ae + 0a6938e commit 5b9a8ef
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
12 changes: 12 additions & 0 deletions server/api/src/domain/room/controller/room.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ export class RoomController {
return { result: await this.roomService.getRoomByUUID(uuid) };
}

@Post(':uuid/join')
@UseGuards(JwtAuthGuard)
async joinRoom(@Param('uuid') uuid: string): Promise<{ result: boolean }> {
return { result: await this.roomService.joinRoom(uuid) };
}

@Post(':uuid/leave')
@UseGuards(JwtAuthGuard)
async leaveRoom(@Param('uuid') uuid: string): Promise<{ result: boolean }> {
return { result: await this.roomService.leaveRoom(uuid) };
}

@Delete(':uuid')
@UseGuards(JwtAuthGuard)
async deleteRoom(@Req() req: Request, @Param('uuid') uuid: string): Promise<{ result: boolean }> {
Expand Down
4 changes: 4 additions & 0 deletions server/api/src/domain/room/dto/room-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export class RoomResponseDto {
agoraAppId: string;
agoraToken: string;
owner: UserResponseDto;
nowHeadcount: number;
maxHeadcount: number;

constructor(room: Room) {
const owner: User = room.owner;
Expand All @@ -26,6 +28,8 @@ export class RoomResponseDto {
this.uuid = room.uuid;
this.agoraAppId = room.agoraAppId;
this.agoraToken = room.agoraToken;
this.nowHeadcount = room.nowHeadcount;
this.maxHeadcount = room.maxHeadcount;
this.owner = new UserResponseDtoBuilder()
.setId(room.owner.id)
.setNickName(owner.nickName)
Expand Down
17 changes: 16 additions & 1 deletion server/api/src/domain/room/repository/room.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { DeleteResult, EntityRepository, Like, Repository } from 'typeorm';
import { DeleteResult, EntityRepository, Like, Repository, UpdateResult } from 'typeorm';
import { PaginationOptions } from '../../../paginate';
import { Room, RoomType } from '../room.entity';

export enum RoomProcessOption {
Join = '+ 1',
Leave = '- 1',
}

@EntityRepository(Room)
export class RoomRepository extends Repository<Room> {
async findByKeywordAndCount(options: PaginationOptions, roomType: RoomType): Promise<[Room[], number]> {
Expand All @@ -25,6 +30,16 @@ export class RoomRepository extends Repository<Room> {
.getOne();
}

async roomProcess(uuid: string, option: RoomProcessOption): Promise<UpdateResult> {
return await this.createQueryBuilder()
.update(Room)
.set({
nowHeadcount: () => `now_headcount ${option}`,
})
.where('uuid = :uuid', { uuid })
.execute();
}

async findRoomByUserEmail(email: string): Promise<Room | undefined> {
return this.createQueryBuilder('room')
.leftJoinAndSelect('room.owner', 'user')
Expand Down
25 changes: 21 additions & 4 deletions server/api/src/domain/room/service/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { InjectRepository } from '@nestjs/typeorm';
import { v4 as uuidv4 } from 'uuid';
import { RtcRole, RtcTokenBuilder } from 'agora-access-token';
import { RoomBuilder } from '../../../builder';
import { UserException, RoomException } from '../../../exception';
import { RoomException, UserException } from '../../../exception';
import { Pagination, PaginationOptions } from '../../../paginate';
import { Connection, DeleteResult, getConnection } from 'typeorm';
import { Connection, DeleteResult } from 'typeorm';
import { Room, RoomType } from '../room.entity';
import { RoomRepository } from '../repository/room.repository';
import { RoomProcessOption, RoomRepository } from '../repository/room.repository';
import { UserRepository } from '../../user/repository/user.repository';
import { CreateRoomRequestDto } from '../dto/create-room-request.dto';
import { RoomResponseDto } from '../dto/room-response.dto';
import { find } from 'rxjs';

@Injectable()
export class RoomService {
Expand All @@ -29,6 +28,24 @@ export class RoomService {
return new RoomResponseDto(findRoom);
}

async joinRoom(uuid: string): Promise<boolean> {
const findRoom = await this.roomRepository.findRoomByUUID(uuid);
if (!findRoom) throw RoomException.roomNotFound();
if (findRoom.nowHeadcount === findRoom.maxHeadcount) throw RoomException.roomFullError();
const updateResult = await this.roomRepository.roomProcess(uuid, RoomProcessOption.Join);
if (!updateResult) throw RoomException.roomJoinError();
return true;
}

async leaveRoom(uuid: string): Promise<boolean> {
const findRoom = await this.roomRepository.findRoomByUUID(uuid);
if (!findRoom) throw RoomException.roomNotFound();
if (findRoom.nowHeadcount < 1) throw RoomException.roomLeaveError();
const updateResult = await this.roomRepository.roomProcess(uuid, RoomProcessOption.Leave);
if (!updateResult) throw RoomException.roomLeaveError();
return true;
}

async getRoomListAll(options: PaginationOptions, roomType: RoomType): Promise<Pagination<Room>> {
const [results, total] = await this.roomRepository.findByKeywordAndCount(options, roomType);
return new Pagination<Room>({
Expand Down
1 change: 1 addition & 0 deletions server/api/src/domain/user/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class UserRepository extends Repository<User> {
async findUserByNickname(nickname: string): Promise<User | undefined> {
return await this.findOne({ where: { nickName: nickname } });
}

async findUserByNicknameWithDev(nickname: string): Promise<User | undefined> {
return await this.findOne({ where: { nickName: nickname }, relations: ['devField'] });
}
Expand Down
21 changes: 21 additions & 0 deletions server/api/src/exception/room.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,25 @@ export class RoomException {
message: '방 삭제중 오류가 발생했습니다.',
});
}

static roomJoinError(): HttpException {
return new InternalServerErrorException({
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
message: '방 참여중 오류가 발생했습니다.',
});
}

static roomLeaveError(): HttpException {
return new InternalServerErrorException({
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
message: '방 퇴장중 오류가 발생했습니다.',
});
}

static roomFullError(): HttpException {
return new BadRequestException({
statusCode: HttpStatus.BAD_REQUEST,
message: '방의 참여자가 가득찼습니다.',
});
}
}

0 comments on commit 5b9a8ef

Please sign in to comment.