-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
채팅 데이터 생성 및 모임마다의 전체 조회 기능 구현
- Loading branch information
Showing
11 changed files
with
252 additions
and
25 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
backend/src/main/java/mouda/backend/chat/controller/ChatController.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,44 @@ | ||
package mouda.backend.chat.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import mouda.backend.chat.dto.request.ChatCreateRequest; | ||
import mouda.backend.chat.dto.response.ChatFindUnloadedResponse; | ||
import mouda.backend.chat.service.ChatService; | ||
import mouda.backend.common.RestResponse; | ||
import mouda.backend.member.domain.Member; | ||
|
||
@RestController | ||
@RequestMapping("/v1/chat") | ||
@RequiredArgsConstructor | ||
public class ChatController { | ||
|
||
private final ChatService chatService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> createChat( | ||
@Valid @RequestBody ChatCreateRequest chatCreateRequest, | ||
Member member | ||
) { | ||
chatService.createChat(chatCreateRequest, member); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<RestResponse<ChatFindUnloadedResponse>> findUnloadedChats( | ||
@RequestParam Long recentChatId, | ||
@RequestParam Long moimId, | ||
Member member | ||
) { | ||
ChatFindUnloadedResponse unloadedChats = chatService.findUnloadedChats(recentChatId, moimId, member); | ||
return ResponseEntity.ok(new RestResponse<>(unloadedChats)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/mouda/backend/chat/dto/request/ChatCreateRequest.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,28 @@ | ||
package mouda.backend.chat.dto.request; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import mouda.backend.chat.domain.Chat; | ||
import mouda.backend.member.domain.Member; | ||
import mouda.backend.moim.domain.Moim; | ||
|
||
public record ChatCreateRequest( | ||
@NotNull | ||
Long moimId, | ||
|
||
@NotBlank | ||
String content | ||
) { | ||
public Chat toEntity(Moim moim, Member member) { | ||
return Chat.builder() | ||
.content(content) | ||
.date(LocalDate.now()) | ||
.time(LocalTime.now()) | ||
.member(member) | ||
.moim(moim) | ||
.build(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/mouda/backend/chat/dto/response/ChatFindDetailResponse.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,28 @@ | ||
package mouda.backend.chat.dto.response; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
|
||
import lombok.Builder; | ||
import mouda.backend.chat.domain.Chat; | ||
|
||
@Builder | ||
public record ChatFindDetailResponse( | ||
long chatId, | ||
String content, | ||
long memberId, | ||
String nickname, | ||
LocalDate date, | ||
LocalTime time | ||
) { | ||
public static ChatFindDetailResponse toResponse(Chat chat) { | ||
return ChatFindDetailResponse.builder() | ||
.chatId(chat.getId()) | ||
.content(chat.getContent()) | ||
.memberId(chat.getMember().getId()) | ||
.nickname(chat.getMember().getNickname()) | ||
.date(chat.getDate()) | ||
.time(chat.getTime()) | ||
.build(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/mouda/backend/chat/dto/response/ChatFindUnloadedResponse.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,9 @@ | ||
package mouda.backend.chat.dto.response; | ||
|
||
import java.util.List; | ||
|
||
public record ChatFindUnloadedResponse( | ||
long loginMemberId, | ||
List<ChatFindDetailResponse> chats | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/mouda/backend/chat/exception/ChatErrorMessage.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,14 @@ | ||
package mouda.backend.chat.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ChatErrorMessage { | ||
|
||
MOIM_NOT_FOUND("존재하지 않는 모임에 채팅할 수 없습니다."), | ||
; | ||
|
||
private final String message; | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/mouda/backend/chat/exception/ChatException.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,12 @@ | ||
package mouda.backend.chat.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import mouda.backend.exception.MoudaException; | ||
|
||
public class ChatException extends MoudaException { | ||
|
||
public ChatException(HttpStatus httpStatus, ChatErrorMessage chatErrorMessage) { | ||
super(httpStatus, chatErrorMessage.getMessage()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/mouda/backend/chat/repository/ChatRepository.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,15 @@ | ||
package mouda.backend.chat.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import mouda.backend.chat.domain.Chat; | ||
|
||
public interface ChatRepository extends JpaRepository<Chat, Long> { | ||
|
||
@Query("SELECT c FROM Chat c WHERE c.moim.id = :moimId AND c.id > :chatId") | ||
List<Chat> findAllUnloadedChats(@Param("moimId") long moimId, @Param("chatId") long chatId); | ||
} |
41 changes: 41 additions & 0 deletions
41
backend/src/main/java/mouda/backend/chat/service/ChatService.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,41 @@ | ||
package mouda.backend.chat.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mouda.backend.chat.domain.Chat; | ||
import mouda.backend.chat.dto.request.ChatCreateRequest; | ||
import mouda.backend.chat.dto.response.ChatFindDetailResponse; | ||
import mouda.backend.chat.dto.response.ChatFindUnloadedResponse; | ||
import mouda.backend.chat.exception.ChatErrorMessage; | ||
import mouda.backend.chat.exception.ChatException; | ||
import mouda.backend.chat.repository.ChatRepository; | ||
import mouda.backend.member.domain.Member; | ||
import mouda.backend.moim.domain.Moim; | ||
import mouda.backend.moim.repository.MoimRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChatService { | ||
|
||
private final ChatRepository chatRepository; | ||
private final MoimRepository moimRepository; | ||
|
||
public void createChat(ChatCreateRequest chatCreateRequest, Member member) { | ||
Moim moim = moimRepository.findById(chatCreateRequest.moimId()) | ||
.orElseThrow(() -> new ChatException(HttpStatus.BAD_REQUEST, ChatErrorMessage.MOIM_NOT_FOUND)); | ||
Chat chat = chatCreateRequest.toEntity(moim, member); | ||
chatRepository.save(chat); | ||
} | ||
|
||
public ChatFindUnloadedResponse findUnloadedChats(long recentChatId, long moimId, Member member) { | ||
List<ChatFindDetailResponse> chats = chatRepository.findAllUnloadedChats(moimId, recentChatId).stream() | ||
.map(ChatFindDetailResponse::toResponse) | ||
.toList(); | ||
|
||
return new ChatFindUnloadedResponse(member.getId(), chats); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
backend/src/test/java/mouda/backend/chat/repository/ChatRepositoryTest.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,54 @@ | ||
package mouda.backend.chat.repository; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
|
||
import mouda.backend.chat.domain.Chat; | ||
import mouda.backend.fixture.ChatFixture; | ||
import mouda.backend.fixture.MemberFixture; | ||
import mouda.backend.fixture.MoimFixture; | ||
import mouda.backend.member.domain.Member; | ||
import mouda.backend.member.repository.MemberRepository; | ||
import mouda.backend.moim.domain.Moim; | ||
import mouda.backend.moim.repository.MoimRepository; | ||
|
||
@DataJpaTest | ||
class ChatRepositoryTest { | ||
|
||
@Autowired | ||
private ChatRepository chatRepository; | ||
|
||
@Autowired | ||
private MoimRepository moimRepository; | ||
|
||
@Autowired | ||
private MemberRepository memberRepository; | ||
|
||
@DisplayName("모임 아이디가 동일하고 채팅 아이디가 더 큰 채팅 리스트가 조회된다.") | ||
@Test | ||
void test() { | ||
Moim moim = MoimFixture.getCoffeeMoim(); | ||
moimRepository.save(moim); | ||
|
||
Member member1 = MemberFixture.getAnna(); | ||
Member member2 = MemberFixture.getHogee(); | ||
|
||
memberRepository.save(member1); | ||
memberRepository.save(member2); | ||
|
||
Chat anna = ChatFixture.getChatWithMemberAtMoim(member1, moim); | ||
chatRepository.save(anna); | ||
Chat hogee = ChatFixture.getChatWithMemberAtMoim(member2, moim); | ||
chatRepository.save(hogee); | ||
|
||
List<Chat> chats = chatRepository.findAllUnloadedChats(1L, 1L); | ||
|
||
assertThat(chats).hasSize(1); | ||
} | ||
} |
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