Skip to content

Commit

Permalink
refactor: 이미지 수동 삭제 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Ho-Tea committed Jan 12, 2025
1 parent 0a90f70 commit ee72d70
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void deleteAllRelatedMemory(long memoryId) {
.stream()
.map(Moment::getId)
.toList();
momentImageRepository.deleteAllByMomentIdInBatch(momentIds);
momentImageRepository.deleteAllByMomentIdInBulk(momentIds);
commentRepository.deleteAllByMomentIdInBatch(momentIds);
momentRepository.deleteAllByMemoryIdInBatch(memoryId);
memoryMemberRepository.deleteAllByMemoryIdInBatch(memoryId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.staccato.moment.repository;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.staccato.moment.domain.MomentImage;

public interface MomentImageRepository extends JpaRepository<MomentImage, Long> {
Optional<MomentImage> findFirstByMomentId(long momentId);

@Modifying
@Query("DELETE FROM MomentImage mi WHERE mi.moment.id In :momentIds")
void deleteAllByMomentIdInBatch(@Param("momentIds") List<Long> momentIds);
void deleteAllByMomentIdInBulk(@Param("momentIds") List<Long> momentIds);

@Modifying
@Query("DELETE FROM MomentImage mi WHERE mi.id In :ids")
void deleteAllByIdInBulk(@Param("ids") List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.staccato.memory.repository.MemoryRepository;
import com.staccato.moment.domain.Feeling;
import com.staccato.moment.domain.Moment;
import com.staccato.moment.domain.MomentImages;
import com.staccato.moment.repository.MomentImageRepository;
import com.staccato.moment.repository.MomentRepository;
import com.staccato.moment.service.dto.request.FeelingRequest;
Expand Down Expand Up @@ -43,11 +44,6 @@ public MomentIdResponse createMoment(MomentRequest momentRequest, Member member)
return new MomentIdResponse(moment.getId());
}

private Memory getMemoryById(long memoryId) {
return memoryRepository.findById(memoryId)
.orElseThrow(() -> new StaccatoException("요청하신 추억을 찾을 수 없어요."));
}

public MomentLocationResponses readAllMoment(Member member) {
return new MomentLocationResponses(momentRepository.findAllByMemory_MemoryMembers_Member(member)
.stream()
Expand All @@ -72,21 +68,25 @@ public void updateMomentById(
Memory targetMemory = getMemoryById(momentRequest.memoryId());
validateMemoryOwner(targetMemory, member);

Moment updatedMoment = momentRequest.toMoment(targetMemory);
moment.update(updatedMoment);
Moment newMoment = momentRequest.toMoment(targetMemory);
MomentImages oringMomentImages = moment.getMomentImages();
List<Long> removalImageIds = oringMomentImages.findRemovalImageIds(newMoment.getMomentImages());
momentImageRepository.deleteAllByIdInBulk(removalImageIds);

moment.update(newMoment);
}

private Moment getMomentById(long momentId) {
return momentRepository.findById(momentId)
.orElseThrow(() -> new StaccatoException("요청하신 스타카토를 찾을 수 없어요."));
private Memory getMemoryById(long memoryId) {
return memoryRepository.findById(memoryId)
.orElseThrow(() -> new StaccatoException("요청하신 추억을 찾을 수 없어요."));
}

@Transactional
public void deleteMomentById(long momentId, Member member) {
momentRepository.findById(momentId).ifPresent(moment -> {
validateMemoryOwner(moment.getMemory(), member);
commentRepository.deleteAllByMomentIdInBatch(List.of(momentId));
momentImageRepository.deleteAllByMomentIdInBatch(List.of(momentId));
momentImageRepository.deleteAllByMomentIdInBulk(List.of(momentId));
momentRepository.deleteById(momentId);
});
}
Expand All @@ -99,6 +99,11 @@ public void updateMomentFeelingById(long momentId, Member member, FeelingRequest
moment.changeFeeling(feeling);
}

private Moment getMomentById(long momentId) {
return momentRepository.findById(momentId)
.orElseThrow(() -> new StaccatoException("요청하신 스타카토를 찾을 수 없어요."));
}

private void validateMemoryOwner(Memory memory, Member member) {
if (memory.isNotOwnedBy(member)) {
throw new ForbiddenException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.staccato.memory.domain.Memory;
import com.staccato.memory.repository.MemoryRepository;
import com.staccato.moment.domain.Moment;
import com.staccato.moment.domain.MomentImage;
import com.staccato.moment.domain.MomentImages;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -30,9 +31,9 @@ class MomentImageRepositoryTest {
@PersistenceContext
private EntityManager em;

@DisplayName("특정 스타카토의 id 여러개를 가지고 있는 모든 스타카토 이미지들을 삭제한다.")
@DisplayName("특정 스타카토의 id 여러개를 가지고 있는 모든 스타카토 이미지들을 벌크 삭제한다.")
@Test
void deleteAllByMomentIdInBatch() {
void deleteAllByMomentIdInBulk() {
// given
Memory memory = memoryRepository.save(MemoryFixture.create(LocalDate.of(2023, 12, 31), LocalDate.of(2024, 1, 10)));
Moment moment1 = momentRepository.save(MomentFixture
Expand All @@ -41,8 +42,7 @@ void deleteAllByMomentIdInBatch() {
.createWithImages(memory, LocalDateTime.of(2023, 12, 31, 22, 20), new MomentImages(List.of("url1", "url2"))));

// when
momentImageRepository.deleteAllByMomentIdInBatch(List.of(moment1.getId(), moment2.getId()));
em.flush();
momentImageRepository.deleteAllByMomentIdInBulk(List.of(moment1.getId(), moment2.getId()));
em.clear();

// then
Expand All @@ -54,4 +54,25 @@ void deleteAllByMomentIdInBatch() {
.isNotEmpty()).isFalse()
);
}

@DisplayName("특정 스타카토 이미지들을 벌크 삭제한다.")
@Test
void deleteAllByIdInBulk() {
// given
Memory memory = memoryRepository.save(MemoryFixture.create(LocalDate.of(2023, 12, 31), LocalDate.of(2024, 1, 10)));
MomentImages momentImages = new MomentImages(List.of("url1", "url2", "url3"));
Moment moment = momentRepository.save(MomentFixture.createWithImages(memory, LocalDateTime.of(2023, 12, 31, 22, 20), momentImages));

List<Long> imageIds = moment.getMomentImages()
.getImages()
.stream()
.map(MomentImage::getId)
.toList();

// when
momentImageRepository.deleteAllByIdInBulk(imageIds);

// then
assertThat(momentImageRepository.findAll()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.staccato.memory.repository.MemoryRepository;
import com.staccato.moment.domain.Feeling;
import com.staccato.moment.domain.Moment;
import com.staccato.moment.domain.MomentImage;
import com.staccato.moment.domain.MomentImages;
import com.staccato.moment.repository.MomentImageRepository;
import com.staccato.moment.repository.MomentRepository;
Expand Down Expand Up @@ -78,7 +79,8 @@ void createMomentWithMomentImages() {
// then
assertAll(
() -> assertThat(momentRepository.findById(momentId)).isNotEmpty(),
() -> assertThat(momentImageRepository.findFirstByMomentId(momentId)).isNotEmpty()
() -> assertThat(momentImageRepository.findAll().size()).isEqualTo(1),
() -> assertThat(momentImageRepository.findAll().get(0).getMoment().getId()).isEqualTo(momentId)
);
}

Expand Down Expand Up @@ -185,12 +187,19 @@ void updateMomentById() {
Moment moment = saveMomentWithImages(memory);

// when
MomentRequest momentRequest = new MomentRequest("newStaccatoTitle", "placeName", "newAddress", BigDecimal.ONE, BigDecimal.ONE, LocalDateTime.now(), memory2.getId(), List.of("https://existExample.com.jpg", "https://existExample2.com.jpg"));
MomentRequest momentRequest = new MomentRequest("newStaccatoTitle", "placeName", "newAddress", BigDecimal.ONE, BigDecimal.ONE, LocalDateTime.now(), memory2.getId(), List.of("https://existExample.com.jpg", "https://newExample.com.jpg"));
momentService.updateMomentById(moment.getId(), momentRequest, member);

// then
Moment foundedMoment = momentRepository.findById(moment.getId()).get();
assertThat(foundedMoment.getTitle()).isEqualTo("newStaccatoTitle");
List<MomentImage> images = momentImageRepository.findAll();
assertAll(
() -> assertThat(foundedMoment.getTitle()).isEqualTo("newStaccatoTitle"),
() -> assertThat(foundedMoment.getMemory().getId()).isEqualTo(memory2.getId()),
() -> assertThat(images.size()).isEqualTo(2),
() -> assertThat(images.get(0).getImageUrl()).isEqualTo("https://existExample.com.jpg"),
() -> assertThat(images.get(1).getImageUrl()).isEqualTo("https://newExample.com.jpg")
);
}

@DisplayName("본인 것이 아닌 스타카토를 수정하려고 하면 예외가 발생한다.")
Expand Down

0 comments on commit ee72d70

Please sign in to comment.