Skip to content

Commit

Permalink
✨ feat: 사진 작가 응답에 리뷰 수와 평균 별점 추가
Browse files Browse the repository at this point in the history
✨ feat: 사진 작가 응답에 리뷰 수와 평균 별점 추가
  • Loading branch information
sunnyineverywhere authored Sep 19, 2023
2 parents 441db43 + 9b9b44d commit 287c4de
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
6 changes: 5 additions & 1 deletion snapspot-api/src/main/java/snap/api/heart/HeartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import snap.api.heart.dto.HeartSuccessResponseDto;
import snap.api.photographer.dto.response.PhotographerResponseDto;
import snap.api.photographer.dto.response.PhotographerSimpleDto;
import snap.api.review.service.ReviewService;
import snap.domains.heart.service.HeartDomainService;
import snap.domains.member.entity.Member;
import snap.domains.photographer.entity.Photographer;
Expand All @@ -17,9 +18,12 @@
public class HeartService {

private final HeartDomainService heartDomainService;
private final ReviewService reviewService;

public List<PhotographerSimpleDto> heartListByMember(Member member){
return heartDomainService.findHeartByMember(member).stream().map(PhotographerSimpleDto::new).toList();
return heartDomainService.findHeartByMember(member).stream()
.map(photographer -> new PhotographerSimpleDto(photographer,
reviewService.findReviewInfoByPhotographer(photographer))).toList();
}

public HeartSuccessResponseDto heartCreate(Member member, Long photographerId){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -12,6 +13,7 @@
import snap.api.photographer.dto.response.PhotographerWithHeartDto;
import snap.api.photographer.dto.response.PhotographerSimpleDto;
import snap.api.photographer.service.PhotographerService;
import snap.api.review.service.ReviewService;
import snap.domains.member.entity.Member;
import snap.domains.photographer.entity.Photographer;
import snap.dto.request.PhotographerFilterReq;
Expand All @@ -26,10 +28,11 @@
public class PhotographerController {

private final PhotographerService photographerService;
private final ReviewService reviewService;

@GetMapping("/me")
public ResponseEntity<PhotographerResponseDto> photographerInfoFind(@AuthPhotographer Photographer photographer) {
return new ResponseEntity<>(new PhotographerResponseDto(photographer), HttpStatus.OK);
return new ResponseEntity<>(new PhotographerResponseDto(photographer, reviewService.findReviewInfoByPhotographer(photographer)), HttpStatus.OK);
}

@PutMapping("/me")
Expand All @@ -54,7 +57,7 @@ public ResponseEntity<List<PhotographerSimpleDto>> photographerFindByTag(@Reques
}

@GetMapping
public ResponseEntity<List<PhotographerSimpleDto>> photographerList(PhotographerFilterReq filterReq, Pageable pageable){
public ResponseEntity<List<PhotographerSimpleDto>> photographerList(PhotographerFilterReq filterReq, @PageableDefault(size = 6) Pageable pageable){
return new ResponseEntity<>(photographerService.findByFilter(filterReq, pageable), HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import snap.api.member.dto.MemberResponseDto;
import snap.api.review.dto.PhotographerReviewResponseDto;
import snap.api.spot.dto.AreaResponseDto;
import snap.domains.photographer.entity.Photographer;
import snap.domains.photographer.entity.PhotographerArea;
Expand All @@ -27,9 +28,10 @@ public class PhotographerResponseDto {
private SnsDto sns;
private SpecialListDto specialList;
private TagsDto tags;
private PhotographerReviewResponseDto review;

@Builder
public PhotographerResponseDto(Photographer entity) {
public PhotographerResponseDto(Photographer entity, PhotographerReviewResponseDto review) {
this.member = new MemberResponseDto(entity.getMember());
this.photographerId = entity.getPhotographerId();
this.lowestPay = entity.getLowestPay();
Expand All @@ -43,5 +45,6 @@ public PhotographerResponseDto(Photographer entity) {
this.sns = new SnsDto(entity.getSns());
this.specialList = new SpecialListDto(entity.getSpecialList());
this.tags = new TagsDto(entity.getTags());
this.review = review;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import snap.api.review.dto.PhotographerReviewResponseDto;
import snap.api.spot.dto.AreaResponseDto;
import snap.domains.photographer.entity.Photographer;
import snap.domains.photographer.entity.PhotographerArea;
Expand All @@ -22,14 +23,11 @@ public class PhotographerSimpleDto {
private List<AreaResponseDto> areas;
private SpecialListDto specialList;
private TagsDto tags;

/**
* TODO: review 브랜치 머지 후 별점, 리뷰 수 필드 추가
*
*/
private Integer totalReview;
private Double averageScore;

@Builder
public PhotographerSimpleDto(Photographer entity) {
public PhotographerSimpleDto(Photographer entity, PhotographerReviewResponseDto review) {
this.photographerId = entity.getPhotographerId();
this.nickname = entity.getMember().getNickname();
this.lowestPay = entity.getLowestPay();
Expand All @@ -39,5 +37,7 @@ public PhotographerSimpleDto(Photographer entity) {
.collect(Collectors.toList());
this.specialList = new SpecialListDto(entity.getSpecialList());
this.tags = new TagsDto(entity.getTags());
this.totalReview = review.getTotalReview();
this.averageScore = review.getAverageScore();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import snap.api.photographer.dto.response.PhotographerResponseDto;
import snap.api.photographer.dto.response.PhotographerSearchResponseDto;
import snap.api.photographer.dto.response.PhotographerWithHeartDto;
import snap.api.review.dto.PhotographerReviewResponseDto;
import snap.api.review.service.ReviewService;
import snap.domains.heart.service.HeartDomainService;
import snap.domains.member.entity.Member;
import snap.api.photographer.dto.response.PhotographerSimpleDto;
Expand All @@ -32,22 +34,23 @@ public class PhotographerService {
private final PhotographerScheduleDomainService photographerScheduleDomainService;
private final PhotographerImageDomainService photographerImageDomainService;
private final HeartDomainService heartDomainService;
private final ReviewService reviewService;

public PhotographerWithHeartDto findPhotographer(Long photographerId, Member member) {
Photographer photographer = photographerDomainService.findById(photographerId);
return new PhotographerWithHeartDto(new PhotographerResponseDto(photographer),
return new PhotographerWithHeartDto(new PhotographerResponseDto(photographer, findReview(photographer)),
heartDomainService.existsHeart(member, photographer));
}

public PhotographerSearchResponseDto findBySearch(String word) {
List<PhotographerSimpleDto> nicknameResult =
photographerDomainService.findByNickname(word).stream()
.map(PhotographerSimpleDto::new)
.map(photographer -> new PhotographerSimpleDto(photographer, findReview(photographer)))
.collect(Collectors.toList());

List<PhotographerSimpleDto> areaResult =
photographerAreaDomainService.findPhotographerListByArea(word).stream()
.map(PhotographerSimpleDto::new)
.map(photographer -> new PhotographerSimpleDto(photographer, findReview(photographer)))
.collect(Collectors.toList());

return new PhotographerSearchResponseDto(nicknameResult, areaResult);
Expand All @@ -56,7 +59,7 @@ public PhotographerSearchResponseDto findBySearch(String word) {
public List<PhotographerSimpleDto> findByTag(String tag){
List<Photographer> photographerList = photographerTagDomainService.findPhotographerListByTag(tag);
return photographerList.stream()
.map(PhotographerSimpleDto::new)
.map(photographer -> new PhotographerSimpleDto(photographer, findReview(photographer)))
.collect(Collectors.toList());
}

Expand All @@ -67,13 +70,13 @@ public List<PhotographerNameResponseDto> findAllNames(){

public List<PhotographerResponseDto> findAllPhotographers(Pageable pageable){
return photographerDomainService.findAllToPage(pageable)
.map(PhotographerResponseDto::new)
.map(photographer -> new PhotographerResponseDto(photographer, findReview(photographer)))
.getContent();
}

public List<PhotographerSimpleDto> findByFilter(PhotographerFilterReq filterReq, Pageable pageable){
return photographerDomainService.findAllByFilter(filterReq, pageable)
.map(PhotographerSimpleDto::new)
.map(photographer -> new PhotographerSimpleDto(photographer, findReview(photographer)))
.getContent();
}

Expand Down Expand Up @@ -101,10 +104,14 @@ public PhotographerResponseDto updatePhotographerInfo(Photographer photographer,
photographerTagDomainService.updateTag(photographer, dto.getTag());


return new PhotographerResponseDto(photographer);
return new PhotographerResponseDto(photographer, findReview(photographer));
}

public Photographer findPhotographerEntity(Long photographerId) {
return photographerDomainService.findById(photographerId);
}

public PhotographerReviewResponseDto findReview(Photographer photographer){
return reviewService.findReviewInfoByPhotographer(photographer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PhotographerReviewResponseDto {
private PhotographerResponseDto photographer;
private Integer totalReview;
private Double averageScore;
private List<ReviewResponseDto> reviews;

public PhotographerReviewResponseDto(Photographer photographer, List<Review> entities) {
this.photographer = new PhotographerResponseDto(photographer);
public PhotographerReviewResponseDto(List<Review> entities) {
this.totalReview = entities.size();
this.averageScore = (entities.stream().mapToDouble(Review::getScore).sum() / entities.size());
this.reviews = entities.stream().map(ReviewResponseDto::new).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void createReview(Member member, ReviewRequestDto request) {
@Transactional(readOnly = true)
public PhotographerReviewResponseDto findReviewInfoByPhotographer(Photographer photographer) {
List<Review> reviewList = reviewDomainService.findReviewListByPhotographer(photographer);
return new PhotographerReviewResponseDto(photographer, reviewList);
return new PhotographerReviewResponseDto(reviewList);
}

@Transactional(readOnly = true)
Expand Down

0 comments on commit 287c4de

Please sign in to comment.