-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: β¨ Chat Room Delete API (#222)
* feat: add chat-room-delete-command * feat: chat-member delete by chat-room-id query * feat: impl service logic * style: using var key-word * fix: add static factory method to the command * rename: from find-by-chat-room-id to find-by-chat-member-id in chat-member-repository * style: add chat-room-delete-status-log in the delete-service * test: chat-room-delete-service-integration-test * rename: from delete-chat-room to execute in the chat-room-delete-service * feat: chat room delete usecase * feat: chat-room-delete controller * docs: chat-room delete api swagger * chore: fix log setting simple
- Loading branch information
1 parent
0c3a98f
commit da91ad0
Showing
9 changed files
with
249 additions
and
8 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
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
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
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
19 changes: 19 additions & 0 deletions
19
...n-service/src/main/java/kr/co/pennyway/domain/context/chat/dto/ChatRoomDeleteCommand.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,19 @@ | ||
package kr.co.pennyway.domain.context.chat.dto; | ||
|
||
public record ChatRoomDeleteCommand( | ||
Long userId, | ||
Long chatRoomId | ||
) { | ||
public ChatRoomDeleteCommand { | ||
if (userId == null) { | ||
throw new IllegalArgumentException("userId must not be null"); | ||
} | ||
if (chatRoomId == null) { | ||
throw new IllegalArgumentException("chatRoomId must not be null"); | ||
} | ||
} | ||
|
||
public static ChatRoomDeleteCommand of(Long userId, Long chatRoomId) { | ||
return new ChatRoomDeleteCommand(userId, chatRoomId); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...rvice/src/main/java/kr/co/pennyway/domain/context/chat/service/ChatRoomDeleteService.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,34 @@ | ||
package kr.co.pennyway.domain.context.chat.service; | ||
|
||
import kr.co.pennyway.common.annotation.DomainService; | ||
import kr.co.pennyway.domain.context.chat.dto.ChatRoomDeleteCommand; | ||
import kr.co.pennyway.domain.domains.chatroom.service.ChatRoomRdbService; | ||
import kr.co.pennyway.domain.domains.member.exception.ChatMemberErrorCode; | ||
import kr.co.pennyway.domain.domains.member.exception.ChatMemberErrorException; | ||
import kr.co.pennyway.domain.domains.member.service.ChatMemberRdbService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@DomainService | ||
@RequiredArgsConstructor | ||
public class ChatRoomDeleteService { | ||
private final ChatMemberRdbService chatMemberRdbService; | ||
private final ChatRoomRdbService chatRoomRdbService; | ||
|
||
@Transactional | ||
public void execute(ChatRoomDeleteCommand command) { | ||
var admin = chatMemberRdbService.readChatMember(command.userId(), command.chatRoomId()) | ||
.orElseThrow(() -> new ChatMemberErrorException(ChatMemberErrorCode.NOT_FOUND)); | ||
|
||
if (!admin.isAdmin()) throw new ChatMemberErrorException(ChatMemberErrorCode.NOT_ADMIN); | ||
|
||
var chatRoom = admin.getChatRoom(); | ||
|
||
chatMemberRdbService.deleteAllByChatRoomId(chatRoom.getId()); | ||
chatRoomRdbService.delete(chatRoom); | ||
|
||
log.info("μ±ν λ°©μ΄ μμ λμμ΅λλ€. chatRoom: {}", chatRoom); | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
.../kr/co/pennyway/domain/context/chat/integration/ChatRoomDeleteServiceIntegrationTest.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,151 @@ | ||
package kr.co.pennyway.domain.context.chat.integration; | ||
|
||
import kr.co.pennyway.domain.common.repository.ExtendedRepositoryFactory; | ||
import kr.co.pennyway.domain.config.DomainServiceIntegrationProfileResolver; | ||
import kr.co.pennyway.domain.config.DomainServiceTestInfraConfig; | ||
import kr.co.pennyway.domain.config.JpaTestConfig; | ||
import kr.co.pennyway.domain.context.chat.dto.ChatRoomDeleteCommand; | ||
import kr.co.pennyway.domain.context.chat.service.ChatRoomDeleteService; | ||
import kr.co.pennyway.domain.context.common.fixture.ChatRoomFixture; | ||
import kr.co.pennyway.domain.context.common.fixture.UserFixture; | ||
import kr.co.pennyway.domain.domains.chatroom.domain.ChatRoom; | ||
import kr.co.pennyway.domain.domains.chatroom.repository.ChatRoomRepository; | ||
import kr.co.pennyway.domain.domains.chatroom.service.ChatRoomRdbService; | ||
import kr.co.pennyway.domain.domains.member.domain.ChatMember; | ||
import kr.co.pennyway.domain.domains.member.exception.ChatMemberErrorException; | ||
import kr.co.pennyway.domain.domains.member.repository.ChatMemberRepository; | ||
import kr.co.pennyway.domain.domains.member.service.ChatMemberRdbService; | ||
import kr.co.pennyway.domain.domains.member.type.ChatMemberRole; | ||
import kr.co.pennyway.domain.domains.user.domain.User; | ||
import kr.co.pennyway.domain.domains.user.repository.UserRepository; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@Slf4j | ||
@EnableAutoConfiguration | ||
@SpringBootTest(classes = {ChatRoomDeleteService.class, ChatMemberRdbService.class, ChatRoomRdbService.class}) | ||
@EntityScan(basePackageClasses = {User.class, ChatRoom.class, ChatMember.class}) | ||
@EnableJpaRepositories( | ||
basePackageClasses = {UserRepository.class, ChatRoomRepository.class, ChatMemberRepository.class}, | ||
repositoryFactoryBeanClass = ExtendedRepositoryFactory.class | ||
) | ||
@ActiveProfiles(resolver = DomainServiceIntegrationProfileResolver.class) | ||
@Import(value = {JpaTestConfig.class}) | ||
public class ChatRoomDeleteServiceIntegrationTest extends DomainServiceTestInfraConfig { | ||
@Autowired | ||
private ChatRoomDeleteService chatRoomDeleteService; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private ChatMemberRepository chatMemberRepository; | ||
|
||
@Autowired | ||
private ChatRoomRepository chatRoomRepository; | ||
|
||
@AfterEach | ||
void tearDown() { | ||
chatMemberRepository.deleteAll(); | ||
chatRoomRepository.deleteAll(); | ||
userRepository.deleteAll(); | ||
} | ||
|
||
@Test | ||
@DisplayName("κ΄λ¦¬μλ μ±ν λ°©μ μμ ν μ μλ€.") | ||
void shouldChatRoomDeletedWhenAdminExecute() { | ||
// given | ||
var user = userRepository.save(UserFixture.GENERAL_USER.toUser()); | ||
var chatRoom = chatRoomRepository.save(ChatRoomFixture.PUBLIC_CHAT_ROOM.toEntityWithId(1L)); | ||
var admin = chatMemberRepository.save(ChatMember.of(user, chatRoom, ChatMemberRole.ADMIN)); | ||
|
||
ChatRoomDeleteCommand command = ChatRoomDeleteCommand.of(user.getId(), chatRoom.getId()); | ||
|
||
// when | ||
chatRoomDeleteService.execute(command); | ||
|
||
// then | ||
var chatMembers = chatMemberRepository.findAll(); | ||
assertThat(chatMembers).hasSize(1); | ||
assertTrue(chatMembers.stream().noneMatch(ChatMember::isActive)); | ||
assertThat(chatRoomRepository.findById(chatRoom.getId())).isEmpty(); | ||
} | ||
|
||
@Test | ||
@DisplayName("μ‘΄μ¬νμ§ μλ λ©€λ²κ° μ±ν λ°© μμ λ₯Ό μλνλ©΄ μμΈκ° λ°μνλ€.") | ||
void shouldThrowExceptionWhenNonExistMemberExecute() { | ||
// given | ||
var user = userRepository.save(UserFixture.GENERAL_USER.toUser()); | ||
var chatRoom = chatRoomRepository.save(ChatRoomFixture.PUBLIC_CHAT_ROOM.toEntityWithId(2L)); | ||
|
||
ChatRoomDeleteCommand command = ChatRoomDeleteCommand.of(user.getId(), chatRoom.getId()); | ||
|
||
// when & then | ||
assertThatThrownBy(() -> chatRoomDeleteService.execute(command)) | ||
.isInstanceOf(ChatMemberErrorException.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("μΌλ° μ¬μ©μλ μ±ν λ°©μ μμ ν μ μλ€.") | ||
void shouldThrowExceptionWhenGeneralUserExecute() { | ||
// given | ||
var user = userRepository.save(UserFixture.GENERAL_USER.toUser()); | ||
var chatRoom = chatRoomRepository.save(ChatRoomFixture.PUBLIC_CHAT_ROOM.toEntityWithId(3L)); | ||
chatMemberRepository.save(ChatMember.of(user, chatRoom, ChatMemberRole.MEMBER)); | ||
|
||
ChatRoomDeleteCommand command = ChatRoomDeleteCommand.of(user.getId(), chatRoom.getId()); | ||
|
||
// when & then | ||
assertThatThrownBy(() -> chatRoomDeleteService.execute(command)) | ||
.isInstanceOf(ChatMemberErrorException.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("μ±ν λ°©μ μν λͺ¨λ λ©€λ²κ° μμ λλ€.") | ||
void shouldAllChatMembersDeletedWhenExecute() { | ||
// given | ||
var chatRoom = chatRoomRepository.save(ChatRoomFixture.PUBLIC_CHAT_ROOM.toEntityWithId(4L)); | ||
var users = createUsers(10); | ||
var admin = chatMemberRepository.save(ChatMember.of(users.get(0), chatRoom, ChatMemberRole.ADMIN)); | ||
var members = createGeneralChatMembers(users.subList(1, users.size()), chatRoom); | ||
|
||
ChatRoomDeleteCommand command = ChatRoomDeleteCommand.of(admin.getUser().getId(), chatRoom.getId()); | ||
|
||
// when | ||
chatRoomDeleteService.execute(command); | ||
|
||
// then | ||
var chatMembers = chatMemberRepository.findAll(); | ||
assertThat(chatMembers).hasSize(10); | ||
assertTrue(chatMembers.stream().noneMatch(ChatMember::isActive)); | ||
assertThat(chatRoomRepository.findById(chatRoom.getId())).isEmpty(); | ||
} | ||
|
||
private List<User> createUsers(int count) { | ||
return IntStream.range(0, count) | ||
.mapToObj(i -> userRepository.save(UserFixture.GENERAL_USER.toUser())) | ||
.toList(); | ||
} | ||
|
||
private List<ChatMember> createGeneralChatMembers(List<User> users, ChatRoom chatRoom) { | ||
return users.stream() | ||
.map(user -> chatMemberRepository.save(ChatMember.of(user, chatRoom, ChatMemberRole.MEMBER))) | ||
.toList(); | ||
} | ||
} |