-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 채팅 조회
- Loading branch information
Showing
23 changed files
with
399 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/hyunsolution/dangu/chatlog/domain/ChatLogRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.hyunsolution.dangu.chatlog.domain; | ||
|
||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface ChatLogRepository extends JpaRepository<ChatLog, Long> { | ||
@Query("select cl from ChatLog cl where cl.workspace.id =:workspaceId and cl.user.id =:userId") | ||
Optional<ChatLog> findByWorkspaceIdAndUserId(Long workspaceId, Long userId); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/hyunsolution/dangu/chatlog/exception/ChatLogError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.hyunsolution.dangu.chatlog.exception; | ||
|
||
import com.hyunsolution.dangu.common.exception.BaseErrorCode; | ||
import com.hyunsolution.dangu.common.exception.ExceptionDto; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ChatLogError implements BaseErrorCode { | ||
CHAT_LOG_NOT_FOUND("CHAT_LOG_400_1", HttpStatus.BAD_REQUEST, "채팅 기록을 찾을 수 없습니다."), | ||
; | ||
private final String code; | ||
private final HttpStatus httpStatus; | ||
private final String message; | ||
|
||
@Override | ||
public ExceptionDto getErrorReason() { | ||
return ExceptionDto.builder().code(code).message(message).httpStatus(httpStatus).build(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/hyunsolution/dangu/chatlog/exception/ChatLogNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.hyunsolution.dangu.chatlog.exception; | ||
|
||
import com.hyunsolution.dangu.common.exception.CustomException; | ||
|
||
public class ChatLogNotFoundException extends CustomException { | ||
public static final ChatLogNotFoundException EXCEPTION = new ChatLogNotFoundException(); | ||
|
||
private ChatLogNotFoundException() { | ||
super(ChatLogError.CHAT_LOG_NOT_FOUND); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/hyunsolution/dangu/chatting/controller/ChattingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.hyunsolution.dangu.chatting.controller; | ||
|
||
import com.hyunsolution.dangu.chatting.dto.response.GetChatRoomsResponse; | ||
import com.hyunsolution.dangu.chatting.dto.response.GetChattingsResponse; | ||
import com.hyunsolution.dangu.chatting.service.ChattingService; | ||
import com.hyunsolution.dangu.common.apiResponse.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ChattingController { | ||
private final ChattingService chattingService; | ||
|
||
@GetMapping("/chattings/{chatRoomId}") | ||
@Operation( | ||
summary = "채팅방에 대한 채팅을 조회한다.", | ||
description = "워크스페이스(채팅방)에 대한 채팅을 조회한다. 워크스페이스 당 채팅방 하나 이기 때문에 워크스페이스를 채팅방이랑 같다고 생각") | ||
public ApiResponse<List<GetChattingsResponse>> getChattings( | ||
@Parameter(hidden = true) @RequestHeader("Authorization") Long userId, | ||
@PathVariable Long chatRoomId) { | ||
List<GetChattingsResponse> responses = chattingService.getChattings(userId, chatRoomId); | ||
return ApiResponse.success(responses); | ||
} | ||
|
||
@GetMapping("/chattings") | ||
@Operation(summary = "채팅방 목록을 조회한다.", description = "자신이 들어가 있는 채팅방 목록을 조회한다.") | ||
public ApiResponse<List<GetChatRoomsResponse>> getChatRooms( | ||
@Parameter(hidden = true) @RequestHeader("Authorization") Long userId) { | ||
List<GetChatRoomsResponse> responses = chattingService.getChatRooms(userId); | ||
return ApiResponse.success(responses); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/hyunsolution/dangu/chatting/domain/ChattingRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.hyunsolution.dangu.chatting.domain; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface ChattingRepository extends JpaRepository<Chatting, Long> { | ||
@Query("select c from Chatting c where c.workspace.id =:workspaceId") | ||
List<Chatting> findByWorkspaceId(Long workspaceId); | ||
|
||
@Query( | ||
value = | ||
"select c.content from chatting c where c.id = (" | ||
+ "select MAX(id) from chatting where workspace_id = :workspaceId)", | ||
nativeQuery = true) | ||
String findLastChattingContentByWorkspaceId(Long workspaceId); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/hyunsolution/dangu/chatting/dto/response/GetChatRoomsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.hyunsolution.dangu.chatting.dto.response; | ||
|
||
import java.util.List; | ||
|
||
public record GetChatRoomsResponse( | ||
Long chatRoomId, String lastMessage, List<String> otherPerson, int unReadCount) { | ||
public static GetChatRoomsResponse of( | ||
Long chatRoomId, String lastMessage, List<String> otherPerson, int unReadCount) { | ||
return new GetChatRoomsResponse(chatRoomId, lastMessage, otherPerson, unReadCount); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/hyunsolution/dangu/chatting/dto/response/GetChattingsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.hyunsolution.dangu.chatting.dto.response; | ||
|
||
public record GetChattingsResponse(String content, Long chattingId, boolean isOwn) { | ||
public static GetChattingsResponse of(String content, Long chattingId, boolean isOwn) { | ||
return new GetChattingsResponse(content, chattingId, isOwn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/com/hyunsolution/dangu/chatting/service/ChattingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.hyunsolution.dangu.chatting.service; | ||
|
||
import com.hyunsolution.dangu.chatlog.domain.ChatLog; | ||
import com.hyunsolution.dangu.chatlog.domain.ChatLogRepository; | ||
import com.hyunsolution.dangu.chatlog.exception.ChatLogNotFoundException; | ||
import com.hyunsolution.dangu.chatting.domain.Chatting; | ||
import com.hyunsolution.dangu.chatting.domain.ChattingRepository; | ||
import com.hyunsolution.dangu.chatting.dto.response.GetChatRoomsResponse; | ||
import com.hyunsolution.dangu.chatting.dto.response.GetChattingsResponse; | ||
import com.hyunsolution.dangu.workspace.domain.Workspace; | ||
import com.hyunsolution.dangu.workspace.domain.WorkspaceRepository; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChattingService { | ||
private final ChattingRepository chattingRepository; | ||
private final ChatLogRepository chatLogRepository; | ||
private final WorkspaceRepository workspaceRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public List<GetChattingsResponse> getChattings(Long loginUserId, Long chatRoomId) { | ||
List<Chatting> chattings = chattingRepository.findByWorkspaceId(chatRoomId); | ||
return chattings.stream() | ||
.map( | ||
chatting -> { | ||
boolean isOwn = isOwn(loginUserId, chatting.getSender().getId()); | ||
return GetChattingsResponse.of( | ||
chatting.getContent(), chatting.getId(), isOwn); | ||
}) | ||
.toList(); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<GetChatRoomsResponse> getChatRooms(Long userId) { | ||
List<Workspace> chatRooms = workspaceRepository.findByParticipantUserId(userId); | ||
return chatRooms.stream() | ||
.filter(chatRoom -> chatRoom.getParticipants().size() > 1) | ||
.map( | ||
chatRoom -> | ||
GetChatRoomsResponse.of( | ||
chatRoom.getId(), | ||
getLastMessage(chatRoom.getId()), | ||
getOtherPerson(chatRoom, userId), | ||
getUnReadCount(chatRoom.getId(), userId))) | ||
.toList(); | ||
} | ||
|
||
private boolean isOwn(Long loginUserId, Long chattingUserId) { | ||
return loginUserId.equals(chattingUserId); | ||
} | ||
|
||
private List<String> getOtherPerson(Workspace chatRoom, Long loginUserId) { | ||
return chatRoom.getParticipants().stream() | ||
.filter(participant -> !participant.getUser().getId().equals(loginUserId)) | ||
.map(participant -> participant.getUser().getUid()) | ||
.toList(); | ||
} | ||
|
||
private String getLastMessage(Long chatRoomId) { | ||
return chattingRepository.findLastChattingContentByWorkspaceId(chatRoomId); | ||
} | ||
|
||
private int getUnReadCount(Long chatRoomId, Long userId) { | ||
ChatLog chatLog = | ||
chatLogRepository | ||
.findByWorkspaceIdAndUserId(chatRoomId, userId) | ||
.orElseThrow(() -> ChatLogNotFoundException.EXCEPTION); | ||
int total = chattingRepository.findByWorkspaceId(chatRoomId).size(); | ||
int read = chatLog.getReadCount(); | ||
return total - read; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/hyunsolution/dangu/common/config/AsyncConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.hyunsolution.dangu.common.config; | ||
|
||
import java.util.concurrent.Executor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
@EnableAsync | ||
@Configuration | ||
@Profile("!test") | ||
public class AsyncConfig implements AsyncConfigurer { | ||
@Override | ||
public Executor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(16); | ||
executor.setMaxPoolSize(25); | ||
executor.setQueueCapacity(10); | ||
executor.setKeepAliveSeconds(60); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/hyunsolution/dangu/common/event/CreateWorkspaceEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.hyunsolution.dangu.common.event; | ||
|
||
import com.hyunsolution.dangu.user.domain.User; | ||
import com.hyunsolution.dangu.workspace.domain.Workspace; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class CreateWorkspaceEvent extends DomainEvent { | ||
private Workspace workspace; | ||
private User user; | ||
|
||
public static CreateWorkspaceEvent of(Workspace workspace, User user) { | ||
return new CreateWorkspaceEvent(workspace, user); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/hyunsolution/dangu/common/event/DomainEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.hyunsolution.dangu.common.event; | ||
|
||
import java.time.LocalDateTime; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class DomainEvent { | ||
private final LocalDateTime publishAt; | ||
|
||
public DomainEvent() { | ||
this.publishAt = LocalDateTime.now(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/hyunsolution/dangu/common/event/EventPublish.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.hyunsolution.dangu.common.event; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface EventPublish {} |
Oops, something went wrong.