From 2903cab2c80f52916d03126d7500a8c2999cc884 Mon Sep 17 00:00:00 2001 From: Daehyun Kim <18080546+vimkim@users.noreply.github.com> Date: Sun, 11 Feb 2024 21:08:22 +0900 Subject: [PATCH] replace all == null (google style) to isNil for clarity (#281) --- server/src/auth/auth.service.ts | 5 ++-- server/src/common/middleware/session.ts | 3 ++- server/src/problem/problem.service.ts | 5 ++-- server/src/room/room.service.ts | 11 ++++---- server/src/socket/socket.gateway.ts | 23 ++++++++-------- server/src/socket/socket.service.ts | 29 +++++++++++---------- server/src/submission/submission.service.ts | 11 ++++---- server/src/user/user.service.ts | 7 ++--- 8 files changed, 51 insertions(+), 43 deletions(-) diff --git a/server/src/auth/auth.service.ts b/server/src/auth/auth.service.ts index bd6edd1..a867148 100644 --- a/server/src/auth/auth.service.ts +++ b/server/src/auth/auth.service.ts @@ -1,7 +1,8 @@ import { BadRequestException, Injectable, Logger } from '@nestjs/common'; -import { UserService } from '../user/user.service'; +import { isNil } from 'src/common/utils'; import User from '../entities/user.entity'; import { ProviderInfo } from '../types/user'; +import { UserService } from '../user/user.service'; @Injectable() export class AuthService { @@ -23,7 +24,7 @@ export class AuthService { const user = await this.userService.findUserByProviderInfo(mockProviderInfo); - if (user == null) { + if (isNil(user)) { this.logger.debug('잘못된 로그인 요청입니다!'); throw new BadRequestException('잘못된 로그인 요청입니다!'); } diff --git a/server/src/common/middleware/session.ts b/server/src/common/middleware/session.ts index 9f62613..cf5ba04 100644 --- a/server/src/common/middleware/session.ts +++ b/server/src/common/middleware/session.ts @@ -1,10 +1,11 @@ import RedisStore from 'connect-redis'; import session from 'express-session'; import Redis from 'ioredis'; +import { isNil } from '../utils'; export const makeRedisStore = () => { const REDIS_HOST = process.env.REDIS_HOSTNAME; - if (REDIS_HOST == null) throw new Error('REDIS_HOST is not defined'); + if (isNil(REDIS_HOST)) throw new Error('REDIS_HOST is not defined'); const redis = new Redis({ host: REDIS_HOST, diff --git a/server/src/problem/problem.service.ts b/server/src/problem/problem.service.ts index a8701e4..1750fba 100644 --- a/server/src/problem/problem.service.ts +++ b/server/src/problem/problem.service.ts @@ -1,7 +1,8 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import Problem from '../entities/problem.entity'; +import { isNil } from 'src/common/utils'; import { ILike, In, Raw, Repository } from 'typeorm'; +import Problem from '../entities/problem.entity'; import { RandomProblemDto } from './dto/random.problem.dto'; import { SearchProblemDto } from './dto/search.problem.dto'; @@ -57,7 +58,7 @@ export class ProblemService { const problemEntities: Problem[] = []; for (const bojProblemId of bojProblemIds) { const problem = await this.getProblemByBojProblemId(bojProblemId); - if (problem == null) { + if (isNil(problem)) { throw new Error(`bojProblemId ${bojProblemId} not found`); } problemEntities.push(problem); diff --git a/server/src/room/room.service.ts b/server/src/room/room.service.ts index 82c3c94..9c9003c 100644 --- a/server/src/room/room.service.ts +++ b/server/src/room/room.service.ts @@ -8,6 +8,7 @@ import { } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import * as crypto from 'crypto'; +import { isNil } from 'src/common/utils'; import { SubmissionService } from 'src/submission/submission.service'; import { Repository } from 'typeorm'; import Room from '../entities/room.entity'; @@ -43,7 +44,7 @@ export class RoomService { throw new BadRequestException('이미 참가한 방이 있습니다.'); } - if (user.username == null) + if (isNil(user.username)) throw new BadRequestException('username이 없습니다.'); const code = await this.createRoomCode(user.username); @@ -81,7 +82,7 @@ export class RoomService { throw new BadRequestException('존재하지 않는 방입니다.'); } const room = roomUsers[0].room; - if (room == null) { + if (isNil(room)) { throw new InternalServerErrorException('방을 찾을 수 없습니다.'); } this.logger.debug(`user ${user.username} joining room ${room.code}...`); @@ -96,7 +97,7 @@ export class RoomService { async exitRoom(user: User) { const joinedRooms = await user.joinedRooms; - if (joinedRooms == null) { + if (isNil(joinedRooms) || joinedRooms.length === 0) { throw new InternalServerErrorException( '참가 중인 방을 찾을 수 없습니다.', ); @@ -110,14 +111,14 @@ export class RoomService { await this.roomUserRepository.remove(roomUser); const room = roomUser.room; - if (room == null) { + if (isNil(room)) { throw new InternalServerErrorException('방을 찾을 수 없습니다.'); } await this.socketService.notifyExit(user.username, room); const numberOfJoinedUsers = (await room.joinedUsers)?.length; - if (numberOfJoinedUsers == null || numberOfJoinedUsers === 0) { + if (isNil(numberOfJoinedUsers) || numberOfJoinedUsers === 0) { await this.destroyRoom(room); } } diff --git a/server/src/socket/socket.gateway.ts b/server/src/socket/socket.gateway.ts index 0343803..90c5653 100644 --- a/server/src/socket/socket.gateway.ts +++ b/server/src/socket/socket.gateway.ts @@ -1,3 +1,5 @@ +import { Logger, UseFilters } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; import { ConnectedSocket, MessageBody, @@ -9,17 +11,16 @@ import { WsException, } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; +import { isNil } from 'src/common/utils'; +import { Repository } from 'typeorm'; +import Room from '../entities/room.entity'; +import User from '../entities/user.entity'; +import { ProblemService } from '../problem/problem.service'; import { ChatEvent, MessageInterface } from '../types/message-interface'; -import { Logger, UseFilters } from '@nestjs/common'; +import { RoomInfoType } from '../types/room-info'; import { UserService } from '../user/user.service'; -import User from '../entities/user.entity'; import { WebsocketExceptionsFilter } from './socket.filter'; -import Room from '../entities/room.entity'; -import { RoomInfoType } from '../types/room-info'; import { SocketService } from './socket.service'; -import { ProblemService } from '../problem/problem.service'; -import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; @WebSocketGateway({ cors: { @@ -91,7 +92,7 @@ export class SocketGateway implements OnGatewayConnection, OnGatewayDisconnect { provider, providerId, }); - if (user == null) throw new WsException('user is null'); + if (isNil(user)) throw new WsException('user is null'); await this.socketService.gameStart(user, roomInfo); } @@ -109,7 +110,7 @@ export class SocketGateway implements OnGatewayConnection, OnGatewayDisconnect { message, ); - if (this.server == null) throw new WsException('server is null'); + if (isNil(this.server)) throw new WsException('server is null'); this.logger.debug(`--> ws: chat-message ${message.body}`); this.server.to(room.code).emit('chat-message', message); @@ -143,9 +144,9 @@ export class SocketGateway implements OnGatewayConnection, OnGatewayDisconnect { getUser(client: Socket): User { const request = client.request as any; - if (request == null) throw new WsException('request is null'); + if (isNil(request)) throw new WsException('request is null'); const user = request.user; - if (user == null) throw new WsException('user is null'); + if (isNil(user)) throw new WsException('user is null'); return user as User; } } diff --git a/server/src/socket/socket.service.ts b/server/src/socket/socket.service.ts index 5ee16ea..4506a11 100644 --- a/server/src/socket/socket.service.ts +++ b/server/src/socket/socket.service.ts @@ -1,16 +1,17 @@ import { Injectable, Logger } from '@nestjs/common'; -import Room from '../entities/room.entity'; +import { InjectRepository } from '@nestjs/typeorm'; import { WsException } from '@nestjs/websockets'; -import { RoomInfoType } from '../types/room-info'; import { Server } from 'socket.io'; +import { isNil } from 'src/common/utils'; +import { Repository } from 'typeorm'; +import * as util from 'util'; import { Status } from '../const/boj-results'; -import { ChatEvent, MessageInterface } from '../types/message-interface'; -import { ProblemType } from '../types/problem-type'; +import Room from '../entities/room.entity'; import User from '../entities/user.entity'; -import * as util from 'util'; -import { Repository } from 'typeorm'; -import { InjectRepository } from '@nestjs/typeorm'; import { ProblemService } from '../problem/problem.service'; +import { ChatEvent, MessageInterface } from '../types/message-interface'; +import { ProblemType } from '../types/problem-type'; +import { RoomInfoType } from '../types/room-info'; import { UserService } from '../user/user.service'; @Injectable() @@ -54,10 +55,10 @@ export class SocketService { const host = await room.host; const problems = await room.problems; - if (roomUsers == null) throw new WsException('roomUsers is null'); - if (host == null) throw new WsException('host is null'); - if (problems == null || problems.length === 0) - throw new WsException('problems is null'); + if (isNil(roomUsers)) throw new WsException('roomUsers is null'); + if (isNil(host)) throw new WsException('host is null'); + if (isNil(problems) || problems.length === 0) + throw new WsException('problems do not exist'); const problemTypes: ProblemType[] = problems.map((problem) => { return { @@ -144,7 +145,7 @@ export class SocketService { // check if the user is the host of the room const host = await roomUser.room.host; - if (host == null) throw new WsException('host is null'); + if (isNil(host)) throw new WsException('host is null'); if (host.id !== user.id) { throw new WsException('방장이 아닙니다.'); } @@ -154,8 +155,8 @@ export class SocketService { // update room entity properties: problems, isStarted, endAt const { problems, duration } = startingRoomInfo; - if (problems == null) throw new WsException('problems is null'); - if (duration == null) throw new WsException('duration is null'); + if (isNil(problems)) throw new WsException('problems is null'); + if (isNil(duration)) throw new WsException('duration is null'); const bojProblemIds = problems.map((problem) => problem.bojProblemId); const problemEntities = await this.problemService.getProblemsByBojProblemIds(bojProblemIds); diff --git a/server/src/submission/submission.service.ts b/server/src/submission/submission.service.ts index 32da620..b181801 100644 --- a/server/src/submission/submission.service.ts +++ b/server/src/submission/submission.service.ts @@ -7,6 +7,7 @@ import { } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import * as cheerio from 'cheerio'; +import { isNil } from 'src/common/utils'; import Room from 'src/entities/room.entity'; import { RankingResponseDto } from 'src/room-user/dto/ranking-response.dto'; import { EntityManager, Repository } from 'typeorm'; @@ -52,7 +53,7 @@ export class SubmissionService { if (!user) throw new BadRequestException('존재하지 않는 유저입니다.'); const roomUsers = await user.joinedRooms; - if (roomUsers == null || roomUsers.length === 0) + if (isNil(roomUsers) || roomUsers.length === 0) throw new BadRequestException('참여중인 방이 없습니다.'); const roomUser = roomUsers[0]; @@ -64,17 +65,17 @@ export class SubmissionService { } const endAt = room.endAt; - if (endAt == null || endAt < new Date()) { + if (isNil(endAt) || endAt < new Date()) { throw new BadRequestException('이미 종료된 방입니다.'); } const problems = await room.problems; - if (problems == null || problems.length === 0) + if (isNil(problems) || problems.length === 0) throw new BadRequestException('문제가 없는 방입니다.'); const problem = await this.problemService.getProblemByBojProblemId(bojProblemId); - if (problem == null) + if (isNil(problem)) throw new BadRequestException('존재하지 않는 문제입니다.'); if ( @@ -83,7 +84,7 @@ export class SubmissionService { throw new BadRequestException('참여중인 방에 없는 문제입니다.'); } - if (user.username == null) + if (isNil(user.username)) throw new BadRequestException('username이 없습니다.'); await this.socketService.submitCode( diff --git a/server/src/user/user.service.ts b/server/src/user/user.service.ts index 2814b35..0ea8888 100644 --- a/server/src/user/user.service.ts +++ b/server/src/user/user.service.ts @@ -4,9 +4,10 @@ import { InternalServerErrorException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { ProviderInfo } from '../types/user'; +import { isNil } from 'src/common/utils'; import { Repository } from 'typeorm'; import User from '../entities/user.entity'; +import { ProviderInfo } from '../types/user'; import { CreateUserDto } from './dto/create.user.dto'; @Injectable() @@ -48,7 +49,7 @@ export class UserService { async findJoinedRooms(user: User) { const joinedRooms = await user.joinedRooms; - if (joinedRooms == null) { + if (isNil(joinedRooms)) { throw new BadRequestException('joinedRooms is null'); } return joinedRooms; @@ -56,7 +57,7 @@ export class UserService { async getSingleJoinedRoom(user: User) { const joinedRooms = await user.joinedRooms; - if (joinedRooms == null) { + if (isNil(joinedRooms)) { throw new InternalServerErrorException('joinedRooms is null'); } if (joinedRooms.length !== 1) {