From 1f908663841ddc7f80eb71512eee4e360e826a58 Mon Sep 17 00:00:00 2001 From: seokjin8678 Date: Mon, 13 May 2024 15:11:11 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Artist=20=EC=83=9D=EC=84=B1=20=EC=8B=9C?= =?UTF-8?q?=20=EC=A4=91=EB=B3=B5=20=EC=9D=B4=EB=A6=84=20=EA=B2=80=EC=A6=9D?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/ArtistCommandService.java | 9 +++++++++ .../artist/repository/ArtistRepository.java | 2 ++ .../festago/common/exception/ErrorCode.java | 1 + .../application/ArtistCommandServiceTest.java | 19 +++++++++++++++++-- .../repository/MemoryArtistRepository.java | 7 +++++++ 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/com/festago/artist/application/ArtistCommandService.java b/backend/src/main/java/com/festago/artist/application/ArtistCommandService.java index ec4b82c22..094535c48 100644 --- a/backend/src/main/java/com/festago/artist/application/ArtistCommandService.java +++ b/backend/src/main/java/com/festago/artist/application/ArtistCommandService.java @@ -7,6 +7,8 @@ import com.festago.artist.dto.event.ArtistDeletedEvent; import com.festago.artist.dto.event.ArtistUpdatedEvent; import com.festago.artist.repository.ArtistRepository; +import com.festago.common.exception.BadRequestException; +import com.festago.common.exception.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; @@ -21,6 +23,7 @@ public class ArtistCommandService { private final ApplicationEventPublisher eventPublisher; public Long save(ArtistCreateCommand command) { + validateSave(command); Artist artist = artistRepository.save( new Artist(command.name(), command.profileImageUrl(), command.backgroundImageUrl()) ); @@ -28,6 +31,12 @@ public Long save(ArtistCreateCommand command) { return artist.getId(); } + private void validateSave(ArtistCreateCommand command) { + if (artistRepository.existsByName(command.name())) { + throw new BadRequestException(ErrorCode.DUPLICATE_ARTIST_NAME); + } + } + public void update(ArtistUpdateCommand command, Long artistId) { Artist artist = artistRepository.getOrThrow(artistId); artist.update(command.name(), command.profileImageUrl(), command.backgroundImageUrl()); diff --git a/backend/src/main/java/com/festago/artist/repository/ArtistRepository.java b/backend/src/main/java/com/festago/artist/repository/ArtistRepository.java index daaac169b..5c0d13687 100644 --- a/backend/src/main/java/com/festago/artist/repository/ArtistRepository.java +++ b/backend/src/main/java/com/festago/artist/repository/ArtistRepository.java @@ -26,4 +26,6 @@ default Artist getOrThrow(Long artistId) { List findByIdIn(Collection artistIds); boolean existsById(Long id); + + boolean existsByName(String name); } diff --git a/backend/src/main/java/com/festago/common/exception/ErrorCode.java b/backend/src/main/java/com/festago/common/exception/ErrorCode.java index b240c25b1..146d5d174 100644 --- a/backend/src/main/java/com/festago/common/exception/ErrorCode.java +++ b/backend/src/main/java/com/festago/common/exception/ErrorCode.java @@ -46,6 +46,7 @@ public enum ErrorCode { OPEN_ID_NOT_SUPPORTED_SOCIAL_TYPE("해당 OpenId 제공자는 지원되지 않습니다."), OPEN_ID_INVALID_TOKEN("잘못된 OpenID 토큰입니다."), NOT_SUPPORT_FILE_EXTENSION("해당 파일의 확장자는 허용되지 않습니다."), + DUPLICATE_ARTIST_NAME("이미 존재하는 아티스트의 이름입니다."), // 401 EXPIRED_AUTH_TOKEN("만료된 로그인 토큰입니다."), diff --git a/backend/src/test/java/com/festago/artist/application/ArtistCommandServiceTest.java b/backend/src/test/java/com/festago/artist/application/ArtistCommandServiceTest.java index f8ce4ef82..46c166d08 100644 --- a/backend/src/test/java/com/festago/artist/application/ArtistCommandServiceTest.java +++ b/backend/src/test/java/com/festago/artist/application/ArtistCommandServiceTest.java @@ -1,20 +1,22 @@ package com.festago.artist.application; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.SoftAssertions.assertSoftly; -import static org.mockito.BDDMockito.*; +import static org.mockito.BDDMockito.mock; import com.festago.artist.domain.Artist; import com.festago.artist.dto.command.ArtistCreateCommand; import com.festago.artist.dto.command.ArtistUpdateCommand; import com.festago.artist.repository.ArtistRepository; import com.festago.artist.repository.MemoryArtistRepository; +import com.festago.common.exception.BadRequestException; +import com.festago.common.exception.ErrorCode; import com.festago.support.fixture.ArtistFixture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; -import org.mockito.BDDMockito; @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") @@ -43,6 +45,19 @@ void setUp() { assertThat(artistRepository.findById(artistId)).isPresent(); } + @Test + void 중복된_이름의_아티스트가_저장되면_예외가_발생한다() { + // given + artistRepository.save(ArtistFixture.builder().name("윤서연").build()); + ArtistCreateCommand command = new ArtistCreateCommand("윤서연", "https://image.com/image.png", + "https://image.com/image.png"); + + // when & then + assertThatThrownBy(() -> artistCommandService.save(command)) + .isInstanceOf(BadRequestException.class) + .hasMessage(ErrorCode.DUPLICATE_ARTIST_NAME.getMessage()); + } + @Test void 아티스트_정보를_변경한다() { // given diff --git a/backend/src/test/java/com/festago/artist/repository/MemoryArtistRepository.java b/backend/src/test/java/com/festago/artist/repository/MemoryArtistRepository.java index 1eba7e9c6..3e12fb6c5 100644 --- a/backend/src/test/java/com/festago/artist/repository/MemoryArtistRepository.java +++ b/backend/src/test/java/com/festago/artist/repository/MemoryArtistRepository.java @@ -4,6 +4,7 @@ import com.festago.support.AbstractMemoryRepository; import java.util.Collection; import java.util.List; +import java.util.Objects; public class MemoryArtistRepository extends AbstractMemoryRepository implements ArtistRepository { @@ -20,4 +21,10 @@ public List findByIdIn(Collection artistIds) { .filter(artist -> artistIds.contains(artist.getId())) .toList(); } + + @Override + public boolean existsByName(String name) { + return memory.values().stream() + .anyMatch(it -> Objects.equals(it.getName(), name)); + } }