Skip to content

Commit

Permalink
Merge pull request #216 from swm-nodriversomabus/BUS-209-MVP1-bugfix
Browse files Browse the repository at this point in the history
hotfix(BE): 채팅 전송 시간 오류 BUS-209-MVP1-bugfix
  • Loading branch information
Lemonade255 authored Nov 15, 2023
2 parents 75c05f5 + b72e269 commit 8a730a8
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import java.util.Date;

@Service
@Slf4j
@RequiredArgsConstructor
@Slf4j
public class JwtUtilService {
private final JwtProperties jwtProperties;
private final RefreshTokenService refreshTokenService;
Expand Down Expand Up @@ -70,7 +70,7 @@ public boolean verifyToken(String token) {
Jws<Claims> claimsJws = Jwts.parserBuilder()
.setSigningKey(secretKey)
.build().parseClaimsJws(token);
return claimsJws.getBody().getExpiration().after(new Date()); // 만료 시간 유혀성 검사
return claimsJws.getBody().getExpiration().after(new Date()); // 만료 시간 유효성 검사
} catch (Exception e) {
log.error(e.getMessage());
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
@Slf4j
@Tag(name = "Chat", description = "Chat API")
public class ChatController {
private final SendChatUsecase sendChatUsecase;
private final SubscribeRoomUsecase subscribeRoomUsecase;
private final SendChatUsecase sendChatUsecase;
private final GetChatListUsecase getChatListUsecase;
private final FileUploadUsecase fileUploadUsecase;

Expand Down Expand Up @@ -78,7 +78,7 @@ public void subscribe(@DestinationVariable String roomId) {
* 채팅 내역 불러오기
* @param roomId (ID)
* @param pageable (데이터)
* @return List<Chat>
* @return chat list
*/
@Operation(summary = "Get chat list", description = "채팅 목록을 불러온다.")
@GetMapping("/chat")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
unmappedTargetPolicy = ReportingPolicy.IGNORE) // 스프링과 사용할 때는 필수적으로 붙여주자, 일치하지 않는 필드는 무ㅡ
public interface ChatMapper {
@Mapping(source = "roomId", target = "roomId.chatroomId")
@Mapping(source = "senderId",target = "senderId.userId")
@Mapping(source = "senderId", target = "senderId.userId")
ChatEntity toEntity(AddChatDto addChatDto);

@Mapping(source = "createdAt", target = "createdAt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void addChat(AddChatDto addChatDto) {
@Override
public List<Chat> getChatList(UUID roomId, Pageable pageable) {
Page<ChatEntity> ret = chatRepository.findAllByRoomId_ChatroomId(roomId, pageable);
if (ret != null && ret.hasContent()){
if (ret != null && ret.hasContent()) {
return chatMapper.toDomainList(ret.getContent());
}
return new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public interface SendChatUsecase {
* @param message Message
*/
void send(String roomNumber, AddChatDto message);
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/example/api/chat/domain/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import java.util.UUID;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Chat {
private UUID roomId;
private ChatUser senderId;
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/example/api/chat/service/ChatAsyncService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.api.chat.service;


import com.example.api.chat.application.port.out.AddChatPort;
import com.example.api.chat.dto.AddChatDto;
import lombok.RequiredArgsConstructor;
Expand All @@ -11,25 +10,24 @@
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
@RequiredArgsConstructor
@Slf4j
public class ChatAsyncService {
private final AddChatPort addChatPort;

/**
* 비동기 작업 처리 -> 디비 저장
* @param addChatDto
* @return
* @param addChatDto (데이터)
* @return CompletableFuture<Void>
*/
@Async
public CompletableFuture<Void> saveChat(AddChatDto addChatDto){
public CompletableFuture<Void> saveChat(AddChatDto addChatDto) {
try {
addChatPort.addChat(addChatDto);
}catch (Exception e){ //혹시라도 문제 생겼을 경우
} catch (Exception e) { // 혹시라도 문제 생겼을 경우
log.error(e.getMessage());
Thread.currentThread().interrupt();
}
return CompletableFuture.completedFuture(null);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import java.util.UUID;

@Service
@Slf4j
@RequiredArgsConstructor
@Slf4j
@Transactional(readOnly = true)
public class ChatService implements GetChatListUsecase {
private final GetChatListPort getChatListPort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional(readOnly = true)
public class KafkaSendService implements SendChatUsecase {
private final KafkaTemplate<String, Chat> kafkaTemplate;
private final ChatMapper chatMapper;
private final ChatAsyncService chatService;
private final KafkaConsumerConfig kafkaConsumerConfig;
private final KafkaTemplate<String, Chat> kafkaTemplate;

@Override
@Transactional
public void send(String roomId, AddChatDto message) {
Chat sendChat = chatMapper.toDomain(message);
sendChat.setCreatedAt(LocalDateTime.now());
final CompletableFuture<Void> chatResult = chatService.saveChat(message);
// 비동기 작업으로 디비 저장
chatResult.thenAccept(
Expand All @@ -38,6 +40,5 @@ public void send(String roomId, AddChatDto message) {
kafkaConsumerConfig.createListenerContainerForRoom(roomId); // 혹시 몰라 컨슈머가 없을 수도 있어 추가
kafkaTemplate.send(roomId, sendChat);
log.info("kafka send FINISH");

}
}
}

0 comments on commit 8a730a8

Please sign in to comment.