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

마지막 작품 삭제시 해당하는 전시까지 삭제하는 로직 추가 #101

Merged
merged 4 commits into from
Feb 18, 2023
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
@@ -1,7 +1,11 @@
package com.yapp.artie.domain.archive.domain.artwork;

import com.yapp.artie.domain.archive.domain.exhibit.Exhibit;
import com.yapp.artie.domain.archive.domain.tag.Tag;
import com.yapp.artie.global.common.BaseEntity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
Expand All @@ -11,6 +15,7 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -38,6 +43,9 @@ public class Artwork extends BaseEntity {
@Embedded
private ArtworkContents contents;

@OneToMany(mappedBy = "artwork", cascade = CascadeType.REMOVE)
List<Tag> tags = new ArrayList<>();

public Artwork(Exhibit exhibit, boolean isMain, ArtworkContents contents) {
this.exhibit = exhibit;
this.isMain = isMain;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface TagRepository extends JpaRepository<Tag, Long> {
List<Tag> findAllByArtworkOrderBySequenceAsc(Artwork artwork);

List<Tag> findAllByArtwork(Artwork artwork);

List<Tag> findAllByName(String name);
}

Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ public List<Long> createBatch(List<String> imageUriList, Long exhibitId, Long us
@Transactional
public void delete(Long id, Long userId) {
Artwork artwork = findById(id, userId);
tagService.deleteAllByArtwork(artwork);
String imageUri = artwork.getContents().getUri();
artworkRepository.delete(artwork);

Long artworkNum = artworkRepository.countArtworkByExhibitId(artwork.getExhibit().getId());
if (artworkNum <= 1) {
exhibitService.delete(artwork.getExhibit().getId(), userId);
} else {
artworkRepository.delete(artwork);
}
s3Service.deleteObject(imageUri);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.yapp.artie.domain.archive.domain.category.Category;
import com.yapp.artie.domain.archive.domain.exhibit.Exhibit;
import com.yapp.artie.domain.archive.domain.exhibit.PinType;
import com.yapp.artie.domain.archive.dto.artwork.CreateArtworkRequestDto;
import com.yapp.artie.domain.archive.dto.cateogry.CategoryDto;
import com.yapp.artie.domain.archive.dto.cateogry.CreateCategoryRequestDto;
import com.yapp.artie.domain.archive.dto.exhibit.CalendarExhibitRequestDto;
Expand All @@ -22,6 +23,7 @@
import com.yapp.artie.domain.archive.repository.ArtworkRepository;
import com.yapp.artie.domain.archive.repository.CategoryRepository;
import com.yapp.artie.domain.archive.repository.ExhibitRepository;
import com.yapp.artie.domain.archive.repository.TagRepository;
import com.yapp.artie.domain.user.domain.User;
import com.yapp.artie.domain.user.repository.UserRepository;
import java.time.LocalDate;
Expand Down Expand Up @@ -70,6 +72,9 @@ class ExhibitServiceTest {
@Autowired
ArtworkService artworkService;

@Autowired
TagRepository tagRepository;

User createUser(String name, String uid) {
User user = new User();
user.setName(name);
Expand Down Expand Up @@ -188,11 +193,18 @@ User createUser(String name, String uid) {
defaultCategory.getId(),
LocalDate.now(), null);
Long created = exhibitService.create(exhibitRequestDto, user.getId());
exhibitService.publish(created, user.getId());
List<String> tags = new ArrayList<>();
tags.add("test-tag");
Long artworkId = artworkService.create(
new CreateArtworkRequestDto(created, "test-uri", "test-artist", null, tags), user.getId());
em.clear();

exhibitService.delete(created, user.getId());
em.flush();

assertThat(em.find(Exhibit.class, created)).isNull();
assertThat(exhibitRepository.findById(created).isEmpty()).isTrue();
assertThat(artworkRepository.findById(artworkId).isEmpty()).isTrue();
assertThat(tagRepository.findAllByName(tags.get(0)).size()).isEqualTo(0);
}

@Test
Expand Down