Skip to content

Commit

Permalink
fix: 봉사자 이미지 업데이트 시 기존 이미지 엔티티가 제거되지 않는 문제를 수정한다. (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushedrumex authored Dec 27, 2023
1 parent cf33e79 commit 307d604
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/clova/anifriends/domain/shelter/Shelter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import java.util.Optional;
Expand Down Expand Up @@ -54,8 +55,8 @@ public class Shelter extends BaseTimeEntity {
@Embedded
private ShelterDeviceToken deviceToken;

@OneToOne(mappedBy = "shelter", fetch = FetchType.LAZY,
cascade = CascadeType.ALL, orphanRemoval = true)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "shelter_image_id")
private ShelterImage image;

public Shelter(
Expand Down Expand Up @@ -92,11 +93,11 @@ public void updateShelter(
}

private ShelterImage updateImage(String imageUrl) {
if(nonNull(imageUrl)) {
if(imageUrl.isBlank()) {
if (nonNull(imageUrl)) {
if (imageUrl.isBlank()) {
return null;
}
if(nonNull(image) && image.isSameWith(imageUrl)) {
if (nonNull(image) && image.isSameWith(imageUrl)) {
return image;
}
return new ShelterImage(this, imageUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.clova.anifriends.domain.common.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
Expand All @@ -25,8 +23,7 @@ public class ShelterImage extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long shelterImageId;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shelter_id")
@OneToOne(mappedBy = "image")
private Shelter shelter;

@Column(name = "image_url")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
Expand Down Expand Up @@ -81,7 +82,8 @@ public class Volunteer extends BaseTimeEntity {
@OneToMany(mappedBy = "volunteer", fetch = FetchType.LAZY)
private List<Applicant> applicants = new ArrayList<>();

@OneToOne(mappedBy = "volunteer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "volunteer_image_id")
private VolunteerImage image;

public Volunteer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import com.clova.anifriends.global.exception.ErrorCode;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
Expand All @@ -27,8 +25,7 @@ public class VolunteerImage extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long volunteerImageId;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "volunteer_id")
@OneToOne(mappedBy = "image")
private Volunteer volunteer;

@Column(name = "image_url")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.clova.anifriends.domain.common.CustomPasswordEncoder;
import com.clova.anifriends.domain.shelter.Shelter;
import com.clova.anifriends.domain.shelter.dto.response.RegisterShelterResponse;
import com.clova.anifriends.domain.shelter.support.ShelterFixture;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -53,4 +54,39 @@ void updateShelterPassword() {
assertThat(isPasswordUpdated).isTrue();
}
}

@Nested
@DisplayName("updateShelterInfo 메서드 호출 시")
class UpdateShelterInfo {

@Test
@DisplayName("성공: 이미지 url 입력값이 들어왔고, 기존의 봉사자 이미지와 다르다면 shelterImage 가 새롭게 생성된다.")
void updateShelterInfoWhenDifferentFromCurrentImage() {
// given
Shelter shelter = ShelterFixture.shelter();
shelterRepository.save(shelter);

String beforeImage = "beforeImage.jpg";
String afterImage = "afterImage.jpg";

shelterService.updateShelter(shelter.getShelterId(), shelter.getName(),
beforeImage, shelter.getAddress(), shelter.getAddressDetail(),
shelter.getPhoneNumber(), shelter.getSparePhoneNumber(),
shelter.isOpenedAddress());

// when
shelterService.updateShelter(shelter.getShelterId(), shelter.getName(),
afterImage, shelter.getAddress(), shelter.getAddressDetail(),
shelter.getPhoneNumber(), shelter.getSparePhoneNumber(),
shelter.isOpenedAddress());

// then
Shelter updatedShelter = entityManager.createQuery(
"select s from Shelter s join fetch s.image where s.shelterId = :shelterId",
Shelter.class)
.setParameter("shelterId", shelter.getShelterId())
.getSingleResult();
assertThat(updatedShelter.getImage()).isEqualTo(afterImage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ void updateVolunteerInfo() {

//then
Volunteer updatedVolunteer
= entityManager.find(Volunteer.class, givenVolunteerId);
= entityManager.createQuery(
"select v from Volunteer v join fetch v.image where v.volunteerId = :volunteerId",
Volunteer.class)
.setParameter("volunteerId", givenVolunteerId)
.getSingleResult();
assertThat(updatedVolunteer.getName()).isEqualTo(newName);
assertThat(updatedVolunteer.getGender()).isEqualTo(newGender);
assertThat(updatedVolunteer.getBirthDate()).isEqualTo(newBirthDate);
Expand All @@ -90,5 +94,29 @@ void updateVolunteerInfoButNoMoreVolunteerImageEntity() {
.getResultList();
assertThat(result).hasSize(1);
}

@Test
@DisplayName("성공: 이미지 url 입력값이 들어왔고, 기존의 봉사자 이미지와 다르다면 volunteerImage 가 새롭게 생성된다.")
void updateVolunteerInfoWhenDifferentFromCurrentImage() {
//given
String beforeImage = "beforeImage.jpg";
String afterImage = "afterImage.jpg";
volunteerService.updateVolunteerInfo(givenVolunteerId, volunteer.getName(),
volunteer.getGender(), volunteer.getBirthDate(), volunteer.getPhoneNumber(),
beforeImage);

//when
volunteerService.updateVolunteerInfo(givenVolunteerId, volunteer.getName(),
volunteer.getGender(), volunteer.getBirthDate(), volunteer.getPhoneNumber(),
afterImage);

//then
Volunteer updatedVolunteer = entityManager.createQuery(
"select v from Volunteer v join fetch v.image where v.volunteerId = :volunteerId",
Volunteer.class)
.setParameter("volunteerId", givenVolunteerId)
.getSingleResult();
assertThat(updatedVolunteer.getVolunteerImageUrl()).isEqualTo(afterImage);
}
}
}

0 comments on commit 307d604

Please sign in to comment.