Skip to content

Commit

Permalink
refactor: 웹소켓 API 중 서버에서 클라이언트에게 응답/알림하는 DTO 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
choyoungwoo9 committed Jul 1, 2024
1 parent 193ed66 commit 1d79137
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 69 deletions.
19 changes: 19 additions & 0 deletions backend/src/project/dto/link/LinkCreateNotify.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Link {
id: number;
url: string;
description: string;
}

export class LinkCreateNotifyDto {
domain: string;
action: string;
content: Link;

static of(id: number, url: string, description: string) {
const dto = new LinkCreateNotifyDto();
dto.domain = 'link';
dto.action = 'create';
dto.content = { id, url, description };
return dto;
}
}
17 changes: 17 additions & 0 deletions backend/src/project/dto/link/LinkDeleteNotify.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Link {
id: number;
}

export class LinkDeleteNotifyDto {
domain: string;
action: string;
content: Link;

static of(id: number) {
const dto = new LinkDeleteNotifyDto();
dto.domain = 'link';
dto.action = 'delete';
dto.content = { id };
return dto;
}
}
19 changes: 19 additions & 0 deletions backend/src/project/dto/member/MemberUpdateNotify.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MemberStatus } from "../../enum/MemberStatus.enum";

class Member{
id: number;
status: MemberStatus;
}

export class MemberUpdateNotifyDto {
domain: string;
action: string;
content: Member;
static of(id: number, status: MemberStatus){
const dto = new MemberUpdateNotifyDto();
dto.domain = 'member';
dto.action = 'update';
dto.content = {id, status};
return dto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Matches,
ValidateNested,
} from 'class-validator';
import { MemberStatus } from '../enum/MemberStatus.enum';
import { MemberStatus } from '../../enum/MemberStatus.enum';

class Content {
@IsNumber()
Expand Down
19 changes: 19 additions & 0 deletions backend/src/project/dto/memo/MemoColorUpdateNotify.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { memoColor } from "../../entity/memo.entity";

class Memo{
id: number;
color: memoColor;
}

export class MemoColorUpdateNotifyDto {
domain: string;
action: string;
content: Memo;
static of(id: number, color: memoColor){
const dto = new MemoColorUpdateNotifyDto();
dto.domain = 'memo';
dto.action = 'colorUpdate';
dto.content = {id, color};
return dto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Matches,
ValidateNested,
} from 'class-validator';
import { memoColor } from '../entity/memo.entity';
import { memoColor } from '../../entity/memo.entity';

class Content {
@IsNumber()
Expand Down
31 changes: 31 additions & 0 deletions backend/src/project/dto/memo/MemoCreateNotify.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Memo, memoColor } from '../../entity/memo.entity';

class MemoDto {
id: number;
title: string;
content: string;
createdAt: Date;
author: string;
color: memoColor;
}

export class MemoCreateNotifyDto {
domain: string;
action: string;
content: MemoDto;
static of(memo: Memo, author: string) {
const dto = new MemoCreateNotifyDto();
dto.domain = 'memo';
dto.action = 'create';
const { id, title, content, color } = memo;
dto.content = {
id,
title,
content,
color,
createdAt: memo.created_at,
author,
};
return dto;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Type } from 'class-transformer';
import { IsEnum, IsNotEmpty, Matches, ValidateNested } from 'class-validator';
import { memoColor } from '../entity/memo.entity';
import { memoColor } from '../../entity/memo.entity';

class Color {
@IsEnum(memoColor)
Expand Down
16 changes: 16 additions & 0 deletions backend/src/project/dto/memo/MemoDeleteNotify.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Memo {
id: number;
}

export class MemoDeleteNotifyDto {
domain: string;
action: string;
content: Memo;
static of(id: number) {
const dto = new MemoDeleteNotifyDto();
dto.domain = 'memo';
dto.action = 'delete';
dto.content = { id };
return dto;
}
}
36 changes: 17 additions & 19 deletions backend/src/project/ws-controller/ws-project-link.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Injectable } from '@nestjs/common';
import { plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
import { LinkCreateRequestDto } from '../dto/LinkCreateRequest.dto';
import { LinkDeleteRequestDto } from '../dto/LinkDeleteRequest.dto';
import { LinkCreateNotifyDto } from '../dto/link/LinkCreateNotify.dto';
import { LinkCreateRequestDto } from '../dto/link/LinkCreateRequest.dto';
import { LinkDeleteNotifyDto } from '../dto/link/LinkDeleteNotify.dto';
import { LinkDeleteRequestDto } from '../dto/link/LinkDeleteRequest.dto';
import { ProjectService } from '../service/project.service';
import { ClientSocket } from '../type/ClientSocket.type';
import { getRecursiveErrorMsgList } from '../util/validation.util';


@Injectable()
export class WsProjectLinkController {
constructor(private readonly projectService: ProjectService) {}
Expand All @@ -24,15 +25,16 @@ export class WsProjectLinkController {
content.url,
content.description,
);
client.nsp.to('landing').emit('landing', {
domain: 'link',
action: 'create',
content: {
id: createLink.id,
url: createLink.url,
description: createLink.description,
},
});
client.nsp
.to('landing')
.emit(
'landing',
LinkCreateNotifyDto.of(
createLink.id,
createLink.url,
createLink.description,
),
);
}

async deleteLink(client: ClientSocket, data: any) {
Expand All @@ -48,13 +50,9 @@ export class WsProjectLinkController {
content.id,
);
if (isDeleted) {
client.nsp.to('landing').emit('landing', {
domain: 'link',
action: 'delete',
content: {
id: content.id,
},
});
client.nsp
.to('landing')
.emit('landing', LinkDeleteNotifyDto.of(content.id));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import { plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
import { MemberUpdateRequestDto } from '../dto/MemberUpdateRequest.dto';
import { MemberUpdateNotifyDto } from '../dto/member/MemberUpdateNotify.dto';
import { MemberUpdateRequestDto } from '../dto/member/MemberUpdateRequest.dto';
import { ClientSocket } from '../type/ClientSocket.type';
import { getRecursiveErrorMsgList } from '../util/validation.util';

Expand Down Expand Up @@ -31,13 +32,11 @@ export class WsProjectMemberController {
}

private sendMemberStatusUpdate(client: ClientSocket) {
client.nsp.to('landing').emit('landing', {
domain: 'member',
action: 'update',
content: {
id: client.member.id,
status: client.status,
},
});
client.nsp
.to('landing')
.emit(
'landing',
MemberUpdateNotifyDto.of(client.member.id, client.status),
);
}
}
48 changes: 18 additions & 30 deletions backend/src/project/ws-controller/ws-project-memo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { Injectable } from '@nestjs/common';
import { plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
import { MemberService } from 'src/member/service/member.service';
import { MemoColorUpdateRequestDto } from '../dto/MemoColorUpdateRequest.dto';
import { MemoCreateRequestDto } from '../dto/MemoCreateRequest.dto';
import { MemoDeleteRequestDto } from '../dto/MemoDeleteRequest.dto';
import { MemoColorUpdateNotifyDto } from '../dto/memo/MemoColorUpdateNotify.dto';
import { MemoColorUpdateRequestDto } from '../dto/memo/MemoColorUpdateRequest.dto';
import { MemoCreateNotifyDto } from '../dto/memo/MemoCreateNotify.dto';
import { MemoCreateRequestDto } from '../dto/memo/MemoCreateRequest.dto';
import { MemoDeleteNotifyDto } from '../dto/memo/MemoDeleteNotify.dto';
import { MemoDeleteRequestDto } from '../dto/memo/MemoDeleteRequest.dto';
import { ProjectService } from '../service/project.service';
import { ClientSocket } from '../type/ClientSocket.type';
import { getRecursiveErrorMsgList } from '../util/validation.util';
Expand All @@ -29,18 +32,9 @@ export class WsProjectMemoController {
content.color,
);
const { username } = await this.memberService.getMember(client.member.id);
client.nsp.to('landing').emit('landing', {
domain: 'memo',
action: 'create',
content: {
id: createdMemo.id,
title: createdMemo.title,
content: createdMemo.content,
createdAt: createdMemo.created_at,
author: username,
color: createdMemo.color,
},
});
client.nsp
.to('landing')
.emit('landing', MemoCreateNotifyDto.of(createdMemo, username));
}

async deleteMemo(client: ClientSocket, data: any) {
Expand All @@ -53,13 +47,9 @@ export class WsProjectMemoController {
const { content } = data as MemoDeleteRequestDto;
const isDeleted = await this.projectService.deleteMemo(content.id);
if (isDeleted) {
client.nsp.to('landing').emit('landing', {
domain: 'memo',
action: 'delete',
content: {
id: content.id,
},
});
client.nsp
.to('landing')
.emit('landing', MemoDeleteNotifyDto.of(content.id));
}
}

Expand Down Expand Up @@ -89,14 +79,12 @@ export class WsProjectMemoController {
}

if (isUpdated) {
client.nsp.to('landing').emit('landing', {
domain: 'memo',
action: 'colorUpdate',
content: {
id: content.id,
color: content.color,
},
});
client.nsp
.to('landing')
.emit(
'landing',
MemoColorUpdateNotifyDto.of(content.id, content.color),
);
}
}
}
13 changes: 5 additions & 8 deletions backend/src/project/ws-controller/ws-project.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InitLandingResponseDto } from '../dto/InitLandingResponse.dto';
import { MemberUpdateNotifyDto } from '../dto/member/MemberUpdateNotify.dto';
import { MemberStatus } from '../enum/MemberStatus.enum';
import { ProjectService } from '../service/project.service';
import { ClientSocket } from '../type/ClientSocket.type';
Expand Down Expand Up @@ -43,13 +44,9 @@ export class WsProjectController {
client.nsp
.to('landing')
.except(client.id)
.emit('landing', {
domain: 'member',
action: 'update',
content: {
id: client.member.id,
status: client.status,
},
});
.emit(
'landing',
MemberUpdateNotifyDto.of(client.member.id, client.status),
);
}
}

0 comments on commit 1d79137

Please sign in to comment.