Skip to content

Commit

Permalink
refactor : 도메인 이벤트 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom committed Jul 4, 2024
1 parent 2d0fc2e commit e2f0d80
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 134 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

public interface ArtistGenreRepository extends JpaRepository<ArtistGenre, UUID> {

List<ArtistGenre> findArtistGenresByArtistId(UUID artistId);
List<ArtistGenre> findAllByArtistId(UUID artistId);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.example.dto.artist.response.ArtistDetailResponse;
import org.example.entity.BaseEntity;
import org.example.entity.artist.Artist;
import org.example.entity.artist.ArtistGenre;
import org.example.error.ArtistError;
import org.example.event.ArtistEvent;
import org.example.event.ArtistEvent.EventType;
import org.example.exception.BusinessException;
import org.example.repository.artist.ArtistGenreRepository;
import org.example.repository.artist.ArtistRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ArtistUseCase {

private final ArtistRepository artistRepository;
private final ApplicationEventPublisher eventPublisher;
private final ArtistGenreRepository artistGenreRepository;

@Transactional
public void save(Artist artist, List<UUID> genreIds) {
artistRepository.save(artist);
eventPublisher.publishEvent(new ArtistEvent(this, artist, genreIds, EventType.SAVE));

List<ArtistGenre> artistGenres = artist.toArtistGenre(genreIds);
artistGenreRepository.saveAll(artistGenres);
}

public List<ArtistDetailResponse> findAllWithGenreNames() {
Expand All @@ -38,10 +39,26 @@ public ArtistDetailResponse findArtistDetailById(UUID id) {
}

@Transactional
public void updateArtist(UUID id, Artist newArtist, List<UUID> genreIds) {
public void updateArtist(UUID id, Artist newArtist, List<UUID> newGenreIds) {
Artist artist = findArtistById(id);
artist.changeArtist(newArtist);
eventPublisher.publishEvent(new ArtistEvent(this, artist, genreIds, EventType.UPDATE));

List<ArtistGenre> currentGenres = artistGenreRepository.findAllByArtistId(artist.getId());

List<UUID> currentGenreIds = currentGenres.stream()
.map(ArtistGenre::getGenreId)
.toList();

List<UUID> genreIdsToAdd = newGenreIds.stream()
.filter(newGenreId -> !currentGenreIds.contains(newGenreId))
.toList();
List<ArtistGenre> artistGenresToAdd = artist.toArtistGenre(genreIdsToAdd);
artistGenreRepository.saveAll(artistGenresToAdd);

List<ArtistGenre> artistGenresToRemove = currentGenres.stream()
.filter(ag -> !newGenreIds.contains(ag.getGenreId()))
.toList();
artistGenresToRemove.forEach(BaseEntity::softDelete);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public void save(Genre genre) {
genreRepository.save(genre);
}

@Transactional(readOnly = true)
public List<Genre> findAllGenres() {
return genreRepository.findAllByIsDeletedFalse();
}
Expand All @@ -38,7 +37,6 @@ public void deleteGenre(UUID id) {
genre.softDelete();
}

@Transactional(readOnly = true)
public Genre findGenreById(UUID id) {
return genreRepository.findById(id)
.orElseThrow(() -> new BusinessException(GenreError.ENTITY_NOT_FOUND_ERROR));
Expand Down

0 comments on commit e2f0d80

Please sign in to comment.