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

[BE] 채팅 연관 로직 수정(#105) #106

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import org.springframework.web.bind.annotation.ResponseBody;
import yeonba.be.chatting.dto.request.ChatPublishRequest;
import yeonba.be.chatting.dto.response.ChatMessageResponse;
import yeonba.be.chatting.dto.response.ChatRequestAcceptResponse;
import yeonba.be.chatting.dto.response.ChatRoomResponse;
import yeonba.be.chatting.service.ChatService;
import yeonba.be.util.CustomResponse;
@@ -86,16 +87,17 @@ public ResponseEntity<CustomResponse<Void>> requestChat(
@ApiResponse(responseCode = "200", description = "채팅 요청 수락 정상 처리")
@ResponseBody
@PostMapping("/notifications/{notificationId}/chat")
public ResponseEntity<CustomResponse<Void>> acceptRequestedChat(
public ResponseEntity<CustomResponse<ChatRequestAcceptResponse>> acceptRequestedChat(
@RequestAttribute("userId") long userId,
@Parameter(description = "알림 ID", example = "1")
@PathVariable long notificationId) {

chatService.acceptRequestedChat(userId, notificationId);
ChatRequestAcceptResponse response = chatService.acceptRequestedChat(userId,
notificationId);

return ResponseEntity
.ok()
.body(new CustomResponse<>());
.body(new CustomResponse<>(response));
}

@Operation(summary = "채팅방 나가기", description = "채팅방을 나갈 수 있습니다.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package yeonba.be.chatting.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ChatRequestAcceptResponse {

@Schema(
type = "number",
description = "채팅방 ID",
example = "1")
private long chatRoomId;
}
9 changes: 6 additions & 3 deletions be/src/main/java/yeonba/be/chatting/service/ChatService.java
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import org.springframework.transaction.annotation.Transactional;
import yeonba.be.chatting.dto.request.ChatPublishRequest;
import yeonba.be.chatting.dto.response.ChatMessageResponse;
import yeonba.be.chatting.dto.response.ChatRequestAcceptResponse;
import yeonba.be.chatting.dto.response.ChatRoomResponse;
import yeonba.be.chatting.entity.ChatMessage;
import yeonba.be.chatting.entity.ChatRoom;
@@ -65,7 +66,7 @@ public void publish(ChatPublishRequest request) {
new ChatMessage(chatRoom, sender, receiver, request.getContent(), request.getSentAt()));
}

@Transactional(readOnly = true)
@Transactional
public List<ChatMessageResponse> getChatMessages(long userId, long roomId) {

User user = userQuery.findById(userId);
@@ -145,7 +146,7 @@ public void requestChat(long senderId, long receiverId) {
}

@Transactional
public void acceptRequestedChat(long userId, long notificationId) {
public ChatRequestAcceptResponse acceptRequestedChat(long userId, long notificationId) {

Notification notification = notificationQuery.findById(notificationId);

@@ -159,7 +160,7 @@ public void acceptRequestedChat(long userId, long notificationId) {
User receiver = userQuery.findById(notification.getReceiver().getId());

// 본인에게 온 채팅 요청인지 검증
if (receiver.equals(userQuery.findById(userId))) {
if (!receiver.equals(userQuery.findById(userId))) {

throw new GeneralException(
NotificationException.NOT_YOUR_CHATTING_REQUEST_NOTIFICATION);
@@ -183,6 +184,8 @@ public void acceptRequestedChat(long userId, long notificationId) {
LocalDateTime.now());

eventPublisher.publishEvent(notificationSendEvent);

return new ChatRequestAcceptResponse(chatRoom.getId());
}

@Transactional
3 changes: 2 additions & 1 deletion be/src/main/java/yeonba/be/exception/ExceptionAdvice.java
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
@@ -16,7 +17,7 @@
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import yeonba.be.util.CustomResponse;

@RestControllerAdvice(annotations = {RestController.class})
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class ExceptionAdvice extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = GeneralException.class)
16 changes: 9 additions & 7 deletions be/src/main/java/yeonba/be/exception/GeneralException.java
Original file line number Diff line number Diff line change
@@ -6,13 +6,15 @@
@RequiredArgsConstructor
public class GeneralException extends RuntimeException {

private final BaseException exception;
private final BaseException exception;

public HttpStatus getHttpStatus() {
return exception.getHttpStatus();
}
public HttpStatus getHttpStatus() {

public String getExceptionReason() {
return exception.getReason();
}
return exception.getHttpStatus();
}

public String getExceptionReason() {

return exception.getReason();
}
}