Skip to content

Commit

Permalink
Merge pull request #198 from zaehuun/feat/field-be
Browse files Browse the repository at this point in the history
회원 가입 시 개발 분야 저장 / 방 정보 관리 구조 약간 수정 / 방 인원 체크 소켓 이벤트 추가
  • Loading branch information
zaehuun authored Nov 18, 2021
2 parents 479d3e7 + 7abd682 commit 5d6ad33
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
3 changes: 2 additions & 1 deletion server/api/src/domain/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { JwtStrategy } from './strategy/jwt.strategy';
import { AuthController } from './controller/auth.controller';
import { AuthService } from './service/auth.service';
import { UserRepository } from '../user/repository/user.repository';
import { DevFieldRepository } from '../field/repository/dev-field.repository';
import { HistoryModule } from '../history/history.module';

@Module({
imports: [
TypeOrmModule.forFeature([UserRepository]),
TypeOrmModule.forFeature([UserRepository, DevFieldRepository]),
PassportModule,
JwtModule.register({
secret: 'secretkey',
Expand Down
10 changes: 9 additions & 1 deletion server/api/src/domain/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ import { JwtService } from '@nestjs/jwt';
import { LocalDate } from 'js-joda';
import { Bcrypt } from 'src/utils/bcrypt';
import { UserBuilder } from '../../../builder';
import { UserException } from '../../../exception';
import { DevFieldException, UserException } from '../../../exception';
import { User } from '../../user/user.entity';
import { HistoryService } from 'src/domain/history/service/history.service';
import { UserRepository } from '../../user/repository/user.repository';
import { DevFieldRepository } from 'src/domain/field/repository/dev-field.repository';
import { LoginRequestDto } from '../dto/login-request.dto';
import { UserResponseDto } from '../dto/user-response.dto';
import { JoinRequestDto } from '../dto/join-request.dto';
import { DevField } from 'src/domain/field/dev-field.entity';

@Injectable()
export class AuthService {
constructor(
@InjectRepository(UserRepository)
private readonly userRepository: UserRepository,

@InjectRepository(DevFieldRepository)
private readonly devFieldRepository: DevFieldRepository,

private readonly historyService: HistoryService,
private jwtService: JwtService,
) {}
Expand All @@ -42,11 +47,14 @@ export class AuthService {
const { nickname, email, password } = joinRequestDto;
const isExistUser = await this.userRepository.exists(joinRequestDto);
if (isExistUser) throw UserException.userIsExist();
const devField: DevField = await this.devFieldRepository.findDevById(joinRequestDto.devFieldId);
if (!devField) throw DevFieldException.devFieldNotFound();
const user: User = new UserBuilder()
.setNickName(nickname)
.setEmail(email)
.setPassword(Bcrypt.hash(password))
.setImageURL(process.env.DEFAULT_IMG)
.setDevField(devField)
.build();
await this.userRepository.save(user);
return true;
Expand Down
32 changes: 24 additions & 8 deletions server/socket/src/gateway/room.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,44 @@ export class RoomGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
}

@SubscribeMessage('join-room')
handleJoinRoom(client: Socket, { field, img, nickname, roomId }: IRoomRequest): void {
handleJoinRoom(client: Socket, { field, img, nickname, roomId, maxHead }: IRoomRequest): void {
client.leave(client.id); //verify-room에서 생성한 룸을 삭제합니다. 이제 사용될 일이 없어서..
client.join(roomId);
if (!this.userList[roomId]) this.userList[roomId] = {};
this.userList[roomId][nickname] = {
if (!this.userList[roomId]) this.userList[roomId] = { list: {}, maxHead: maxHead };
this.userList[roomId]['list'][nickname] = {
field: field,
img: img,
};
this.server.to(roomId).emit('user-list', this.userList[roomId]);
this.server.to(roomId).emit('user-list', this.userList[roomId]['list']);
}

@SubscribeMessage('leave-room')
handleLeaveRoom(client: Socket, { nickname, roomId }: IRoomRequest): void {
client.leave(roomId);
delete this.userList[roomId][nickname];
this.server.to(roomId).emit('user-list', this.userList[roomId]);
delete this.userList[roomId]['list'][nickname];
this.server.to(roomId).emit('user-list', this.userList[roomId]['list']);
}

@SubscribeMessage('kick-room')
handleKickRoom(client: Socket, { roomId, kickNickname }: IRoomRequest): void {
if (!kickNickname) return;
delete this.userList[roomId][kickNickname];
this.server.to(roomId).emit('user-list', this.userList[roomId]);
delete this.userList[roomId]['list'][kickNickname];
this.server.to(roomId).emit('user-list', this.userList[roomId]['list']);
}

@SubscribeMessage('verify-room')
handleVerifyRoom(client: Socket, { roomId }: IRoomRequest): void {
client.join(client.id); //해당 사용자한테만 전송될 수 있게
if (!this.userList[roomId]) {
this.server.to(client.id).emit('is-verify', true);
return;
}
if (Object.keys(this.userList[roomId]['list']).length < this.userList[roomId]['maxHead']) {
this.server.to(client.id).emit('is-verify', true);
return;
}

this.server.to(client.id).emit('is-verify', false);
}

afterInit(server: Server) {
Expand Down
1 change: 1 addition & 0 deletions server/socket/src/gateway/room.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IRoomRequest {
kickNickname?: string;
field?: string;
img?: string;
maxHead: number;
}

export enum MessageType {
Expand Down

0 comments on commit 5d6ad33

Please sign in to comment.