Skip to content

Commit

Permalink
Merge pull request #325 from boostcampwm2023/BE-postNotExist-#320
Browse files Browse the repository at this point in the history
[BE/#320] 채팅방 생성시 없는 게시물 404 , 채팅 count 수정
  • Loading branch information
koomin1227 authored Dec 5, 2023
2 parents a4d31af + 1e9e254 commit 425f55d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
22 changes: 20 additions & 2 deletions BE/src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Controller, Get, Post, Body, Param, UseGuards } from '@nestjs/common';
import {
Controller,
Get,
Post,
Body,
Param,
UseGuards,
HttpException,
} from '@nestjs/common';
import { ChatService } from './chat.service';
import { AuthGuard } from '../utils/auth.guard';
import { UserHash } from '../utils/auth.decorator';
Expand Down Expand Up @@ -28,7 +36,17 @@ export class ChatController {
@Post('room')
@UseGuards(AuthGuard)
async roomCreate(@Body() body: CreateRoomDto, @UserHash() userId: string) {
return await this.chatService.createRoom(body.post_id, userId, body.writer);
const room = await this.chatService.createRoom(
body.post_id,
userId,
body.writer,
);

if (room === null) {
throw new HttpException('해당 post 는 없습니다', 404);
} else {
return room;
}
}

@Get()
Expand Down
14 changes: 12 additions & 2 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class ChatService {
chat.message = message.message;
chat.chat_room = message.room_id;
chat.is_read = is_read;
chat.count = message.count;
await this.chatRepository.save(chat);
}

Expand All @@ -54,8 +55,17 @@ export class ChatService {
chatRoom.post_id = postId;
chatRoom.writer = writerId;
chatRoom.user = userId;
const roomId = (await this.chatRoomRepository.save(chatRoom)).id;
return { room_id: roomId };

try {
const roomId = (await this.chatRoomRepository.save(chatRoom)).id;
return { room_id: roomId };
} catch (e) {
if (e.errno === 1452) {
return null;
} else {
throw new HttpException('서버 오류', 500);
}
}
}

async findRoomList(userId: string) {
Expand Down
1 change: 1 addition & 0 deletions BE/src/chat/chats.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect {
sender: sender,
message: message['message'],
is_read: true,
count: message['count'],
},
}),
);
Expand Down
5 changes: 4 additions & 1 deletion BE/src/chat/dto/chat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsBoolean, IsString } from 'class-validator';
import { IsBoolean, IsNumber, IsString } from 'class-validator';

export class ChatDto {
@IsString()
Expand All @@ -9,4 +9,7 @@ export class ChatDto {

@IsString()
room_id: number;

@IsNumber()
count: number;
}
3 changes: 3 additions & 0 deletions BE/src/entities/chat.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class ChatEntity {
@Column({ type: 'boolean', default: false })
is_read: boolean;

@Column({ type: 'int' })
count: number;

@CreateDateColumn({ type: 'timestamp', nullable: false })
create_date: Date;

Expand Down

0 comments on commit 425f55d

Please sign in to comment.