Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace all == null (google style) to isNil for clarity #281

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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('잘못된 로그인 요청입니다!');
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/common/middleware/session.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 3 additions & 2 deletions server/src/problem/problem.service.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions server/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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}...`);
Expand All @@ -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(
'참가 중인 방을 찾을 수 없습니다.',
);
Expand All @@ -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);
}
}
Expand Down
23 changes: 12 additions & 11 deletions server/src/socket/socket.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Logger, UseFilters } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import {
ConnectedSocket,
MessageBody,
Expand All @@ -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: {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
29 changes: 15 additions & 14 deletions server/src/socket/socket.service.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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('방장이 아닙니다.');
}
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions server/src/submission/submission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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];
Expand All @@ -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 (
Expand All @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -48,15 +49,15 @@ 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;
}

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) {
Expand Down