Skip to content

Commit

Permalink
feat: 변경 발생 이벤트를 브로드캐스트 하는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeonghak committed Nov 20, 2024
1 parent 16e301a commit 8893437
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 215 deletions.
1 change: 1 addition & 0 deletions apps/server/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
'class-methods-use-this': 'off', // nest not use this in class
'import/no-extraneous-dependencies': 'off',
'import/extensions': 'off',
'no-await-in-loop': 'off',

'prettier/prettier': ['error', { endOfLine: 'auto' }],
},
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { ProjectModule } from '@/project/project.module';
provide: APP_PIPE,
useValue: new ValidationPipe({
whitelist: true,
transform: true,
}),
},
],
Expand Down
8 changes: 4 additions & 4 deletions apps/server/src/project/controller/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ export class ProjectController {
let response;
switch (event) {
case EventType.CREATE_TASK:
// response = await this.taskService.create(user.id, taskEvent);
response = await this.taskService.create(user.id, projectId, taskEvent);
break;
case EventType.DELETE_TASK:
// response = await this.taskService.delete(user.id, taskEvent);
response = await this.taskService.delete(user.id, projectId, taskEvent);
break;
case EventType.UPDATE_POSITION:
// response = await this.taskService.move(user.id, taskEvent);
response = await this.taskService.move(user.id, projectId, taskEvent);
break;
case EventType.INSERT_TITLE:
case EventType.DELETE_TITLE:
Expand All @@ -115,6 +115,6 @@ export class ProjectController {
default:
throw new BadRequestException('올바르지 않은 이벤트 타입입니다.');
}
return response;
return new BaseResponse(200, '이벤트 처리 완료했습니다.', response);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Controller, Get, Query, Req, Res, UseGuards } from '@nestjs/common';
import { Request, Response } from 'express';
import { AccessTokenGuard } from '@/account/guard/accessToken.guard';
import { TaskService } from '@/task/service/task.service';
import { AuthUser } from '@/account/decorator/authUser.decorator';
import { Account } from '@/account/entity/account.entity';
import { CustomResponse } from '@/task/domain/custom-response.interface';
import { BroadcastService } from '@/task/service/broadcast.service';

@UseGuards(AccessTokenGuard)
@Controller('snapshot')
export class SnapshotController {
constructor(private taskService: TaskService) {}
@Controller('event')
export class EventController {
constructor(private broadcastService: BroadcastService) {}

@Get()
polling(
Expand All @@ -21,10 +21,10 @@ export class SnapshotController {
const customResponse = res as CustomResponse;
customResponse.userId = user.id;

this.taskService.addConnection(projectId, customResponse);
this.broadcastService.addConnection(projectId, customResponse);

req.socket.on('close', () => {
this.taskService.removeConnection(projectId, customResponse);
this.broadcastService.removeConnection(projectId, customResponse);
});
}
}
50 changes: 5 additions & 45 deletions apps/server/src/task/controller/task.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { TaskService } from '@/task/service/task.service';
import { UpdateTaskRequest } from '@/task/dto/update-task-request.dto';
import { MoveTaskRequest } from '@/task/dto/move-task-request.dto';
import { CreateTaskRequest } from '@/task/dto/create-task-request.dto';
import { AuthUser } from '@/account/decorator/authUser.decorator';
import { Account } from '@/account/entity/account.entity';
import { AccessTokenGuard } from '@/account/guard/accessToken.guard';
Expand All @@ -23,48 +10,21 @@ import { BaseResponse } from '@/common/BaseResponse';
export class TaskController {
constructor(private taskService: TaskService) {}

@Post()
async create(@AuthUser() user: Account, @Body() body: CreateTaskRequest) {
return new BaseResponse(
200,
'태스크가 정상적으로 생성되었습니다.',
await this.taskService.create(body)
);
}

@Get()
async getAll(@Query('projectId') projectId: number) {
async getAll(@AuthUser() user: Account, @Query('projectId') projectId: number) {
return new BaseResponse(
200,
'태스크 목록이 정상적으로 조회되었습니다.',
await this.taskService.getAll(projectId)
);
}

@Patch(':id/position')
async move(@Param('id') id: number, @Body() moveTaskRequest: MoveTaskRequest) {
return new BaseResponse(
200,
'태스크가 정상적으로 이동되었습니다.',
await this.taskService.move(id, moveTaskRequest)
await this.taskService.getAll(user.id, projectId)
);
}

@Get(':id')
async get(@Param('id') id: number) {
async get(@AuthUser() user: Account, @Param('id') id: number) {
return new BaseResponse(
200,
'태스크가 정상적으로 조회되었습니다.',
await this.taskService.get(id)
);
}

@Delete(':id')
async delete(@Param('id') id: number) {
return new BaseResponse(
200,
'태스크가 정상적으로 삭제되었습니다.',
await this.taskService.delete(id)
await this.taskService.get(user.id, id)
);
}
}
42 changes: 0 additions & 42 deletions apps/server/src/task/domain/snapshot.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/server/src/task/domain/update-information.type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type UpdateInformation = {
export type UpdateInformation = {
position: number;

content: string;
Expand Down
11 changes: 0 additions & 11 deletions apps/server/src/task/dto/create-task-request.dto.ts

This file was deleted.

15 changes: 0 additions & 15 deletions apps/server/src/task/dto/move-task-request.dto.ts

This file was deleted.

13 changes: 13 additions & 0 deletions apps/server/src/task/dto/task-event-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { EventType } from '@/task/domain/eventType.enum';
import { TaskEvent } from '@/task/dto/task-event.dto';

export class TaskEventResponse {
taskId: number;

event: EventType;

constructor(taskEvent: TaskEvent) {
this.taskId = taskEvent.taskId;
this.event = taskEvent.event;
}
}
3 changes: 2 additions & 1 deletion apps/server/src/task/dto/task-event.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IsNumber, IsNotEmpty, IsString, IsEnum } from 'class-validator';
import { EventType } from '../domain/eventType.enum';
import { EventType } from '@/task/domain/eventType.enum';
import { UpdateInformation } from '@/task/domain/update-information.type';

export class TaskEvent {
@IsNumber()
Expand Down
15 changes: 0 additions & 15 deletions apps/server/src/task/dto/update-task-request.dto.ts

This file was deleted.

41 changes: 41 additions & 0 deletions apps/server/src/task/service/broadcast.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Injectable } from '@nestjs/common';
import { CustomResponse } from '@/task/domain/custom-response.interface';
import { BaseResponse } from '@/common/BaseResponse';

@Injectable()
export class BroadcastService {
private connections: Map<number, CustomResponse[]> = new Map();

addConnection(projectId: number, res: CustomResponse) {
if (!this.connections.has(projectId)) {
this.connections.set(projectId, [res]);
} else {
this.connections.get(projectId).push(res);
}
}

removeConnection(projectId: number, res: CustomResponse) {
const filteredConnections = this.connections
.get(projectId)
.filter((r) => r.userId !== res.userId);

this.connections.set(projectId, filteredConnections);
}

sendConnection(userId: number, projectId: number, event: any) {
const connections = this.connections.get(projectId);
if (!connections) {
return;
}

connections.forEach((res) => {
if (res.userId !== userId) {
res.json(new BaseResponse(200, '이벤트가 발생했습니다.', event));
}
});
this.connections.set(
projectId,
connections.filter((res) => res.userId === userId)
);
}
}
Loading

0 comments on commit 8893437

Please sign in to comment.