Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] feat: Artist 생성 시 중복 이름 검증 추가 (#963) #964

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,13 +23,20 @@ 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())
);
eventPublisher.publishEvent(new ArtistCreatedEvent(artist));
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ default Artist getOrThrow(Long artistId) {
List<Artist> findByIdIn(Collection<Long> artistIds);

boolean existsById(Long id);

boolean existsByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("만료된 로그인 토큰입니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Artist> implements ArtistRepository {

Expand All @@ -20,4 +21,10 @@ public List<Artist> findByIdIn(Collection<Long> 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));
}
}
Loading