Skip to content

Commit

Permalink
fix: 보호소에 달린 후기 리스트 조회 api 에서 봉사자의 리뷰 개수를 잘못 가져오는 로직을 수정한다. (#101)
Browse files Browse the repository at this point in the history
* fix: 봉사자의 리뷰 개수를 가져오는 로직을 수정한다.

* refactor: Volunteer 클래스 메서드 순서 변경
  • Loading branch information
hseong3243 authored Nov 6, 2023
1 parent da6190d commit 0a6a636
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/clova/anifriends/domain/applicant/Applicant.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public Applicant(
validateRecruitment(recruitment);
checkConcurrency(recruitment);
this.recruitment = recruitment;
recruitment.addApplicant(this);
validateVolunteer(volunteer);
this.volunteer = volunteer;
volunteer.addApplicant(this);
this.status = ApplicantStatus.PENDING;
}

Expand All @@ -77,6 +79,10 @@ public Long getApplicantId() {
return applicantId;
}

public Review getReview() {
return review;
}

private void validateRecruitment(Recruitment recruitment) {
if (recruitment == null) {
throw new ApplicantBadRequestException("봉사는 필수 입력 항목입니다.");
Expand Down Expand Up @@ -105,4 +111,8 @@ public boolean isAttendance() {
public boolean hasReview() {
return review != null;
}

public void registerReview(Review review) {
this.review = review;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,8 @@ public void closeRecruitment() {
public List<Applicant> getApplicants() {
return Collections.unmodifiableList(applicants);
}

public void addApplicant(Applicant applicant) {
applicants.add(applicant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Review(
validateApplicant(applicant);
validateImageUrlsSize(imageUrls);
this.applicant = applicant;
this.applicant.registerReview(this);
this.content = new ReviewContent(content);
this.imageUrls = imageUrls == null ? null : imageUrls.stream()
.map(url -> new ReviewImage(this, url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public record FindShelterReviewResponse(
String volunteerName,
int temperature,
String volunteerImageUrl,
int VolunteerReviewCount) {
long VolunteerReviewCount) {

public static FindShelterReviewResponse from(Review review) {
Volunteer volunteer = review.getApplicant().getVolunteer();
Expand All @@ -31,7 +31,7 @@ public static FindShelterReviewResponse from(Review review) {
volunteer.getName(),
volunteer.getTemperature(),
volunteer.getVolunteerImageUrl(),
volunteer.getApplications().size()
volunteer.getReviewCount()
);
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/com/clova/anifriends/domain/volunteer/Volunteer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -64,7 +65,7 @@ public class Volunteer extends BaseTimeEntity {
private VolunteerName name;

@OneToMany(mappedBy = "volunteer", fetch = FetchType.LAZY)
private List<Applicant> applications = new ArrayList<>();
private List<Applicant> applicants = new ArrayList<>();

@OneToOne(mappedBy = "volunteer")
private VolunteerImage volunteerImage;
Expand Down Expand Up @@ -94,6 +95,20 @@ private LocalDate validateBirthDate(String birthDate) {
}
}

public void addApplicant(Applicant applicant) {
applicants.add(applicant);
}

public void updateVolunteerImage(VolunteerImage volunteerImage) {
this.volunteerImage = volunteerImage;
}

public long getReviewCount() {
return applicants.stream()
.filter(applicant -> Objects.nonNull(applicant.getReview()))
.count();
}

public Long getVolunteerId() {
return volunteerId;
}
Expand Down Expand Up @@ -130,11 +145,7 @@ public String getVolunteerImageUrl() {
return this.volunteerImage == null ? null : volunteerImage.getImageUrl();
}

public List<Applicant> getApplications() {
return Collections.unmodifiableList(applications);
}

public void updateVolunteerImage(VolunteerImage volunteerImage) {
this.volunteerImage = volunteerImage;
public List<Applicant> getApplicants() {
return Collections.unmodifiableList(applicants);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static FindVolunteerMyPageResponse from(Volunteer volunteer) {
volunteer.getBirthDate(),
volunteer.getPhoneNumber(),
volunteer.getTemperature(),
volunteer.getApplications().stream()
volunteer.getApplicants().stream()
.filter(applicant -> applicant.getStatus().equals(ApplicantStatus.ATTENDANCE))
.count(),
volunteer.getVolunteerImageUrl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import com.clova.anifriends.domain.review.Review;
import com.clova.anifriends.domain.review.dto.response.FindReviewResponse;
import com.clova.anifriends.domain.review.dto.response.FindShelterReviewsResponse;
import com.clova.anifriends.domain.review.dto.response.FindVolunteerReviewsResponse;
import com.clova.anifriends.domain.review.exception.ApplicantNotFoundException;
import com.clova.anifriends.domain.review.exception.ReviewBadRequestException;
import com.clova.anifriends.domain.review.dto.response.FindVolunteerReviewsResponse;
import com.clova.anifriends.domain.review.exception.ReviewNotFoundException;
import com.clova.anifriends.domain.review.repository.ReviewRepository;
import com.clova.anifriends.domain.shelter.Shelter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void findVolunteerMyPage() throws Exception {
volunteer.getBirthDate(),
volunteer.getPhoneNumber(),
volunteer.getTemperature(),
volunteer.getApplications().stream()
volunteer.getApplicants().stream()
.filter(applicant -> applicant.getStatus().equals(ApplicantStatus.ATTENDANCE))
.count(),
VolunteerImageFixture.volunteerImage(volunteer).getImageUrl());
Expand Down

0 comments on commit 0a6a636

Please sign in to comment.