diff --git a/src/main/java/com/example/demo/config/MapperConfig.java b/src/main/java/com/example/demo/config/MapperConfig.java new file mode 100644 index 00000000..5a4cc0ba --- /dev/null +++ b/src/main/java/com/example/demo/config/MapperConfig.java @@ -0,0 +1,39 @@ +package com.example.demo.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.example.demo.mapper.BusinessHourMapper; +import com.example.demo.mapper.CafeMapper; +import com.example.demo.mapper.ReviewMapper; +import com.example.demo.mapper.SnsDetailMapper; +import com.example.demo.mapper.StudyOnceMapper; + +@Configuration +public class MapperConfig { + + @Bean + public BusinessHourMapper businessHourMapper() { + return new BusinessHourMapper(); + } + + @Bean + public CafeMapper cafeMapper() { + return new CafeMapper(); + } + + @Bean + public ReviewMapper reviewMapper() { + return new ReviewMapper(); + } + + @Bean + public SnsDetailMapper snsDetailMapper() { + return new SnsDetailMapper(); + } + + @Bean + public StudyOnceMapper studyOnceMapper() { + return new StudyOnceMapper(); + } +} diff --git a/src/main/java/com/example/demo/controller/StudyOnceController.java b/src/main/java/com/example/demo/controller/StudyOnceController.java index 82f1f33c..6255d17e 100644 --- a/src/main/java/com/example/demo/controller/StudyOnceController.java +++ b/src/main/java/com/example/demo/controller/StudyOnceController.java @@ -33,9 +33,9 @@ public class StudyOnceController { private final StudyOnceService studyOnceService; private final CafegoryTokenManager cafegoryTokenManager; - @GetMapping("/{studyId:[0-9]+}") - public ResponseEntity search(@PathVariable Long studyId) { - StudyOnceSearchResponse response = studyOnceService.searchByStudyId(studyId); + @GetMapping("/{studyOnceId:[0-9]+}") + public ResponseEntity search(@PathVariable Long studyOnceId) { + StudyOnceSearchResponse response = studyOnceService.searchByStudyId(studyOnceId); return ResponseEntity.ok(response); } @@ -54,30 +54,30 @@ public ResponseEntity create(@RequestBody StudyOnceCrea return ResponseEntity.ok(response); } - @PostMapping("/{studyId:[0-9]+}") - public ResponseEntity tryJoin(@PathVariable Long studyId, + @PostMapping("/{studyOnceId:[0-9]+}") + public ResponseEntity tryJoin(@PathVariable Long studyOnceId, @RequestHeader("Authorization") String authorization) { long memberId = cafegoryTokenManager.getIdentityId(authorization); LocalDateTime requestTime = LocalDateTime.now(); - studyOnceService.tryJoin(memberId, studyId); + studyOnceService.tryJoin(memberId, studyOnceId); return ResponseEntity.ok(new StudyOnceJoinResult(requestTime, true)); } - @DeleteMapping("/{studyId:[0-9]+}") - public ResponseEntity tryQuit(@PathVariable Long studyId, + @DeleteMapping("/{studyOnceId:[0-9]+}") + public ResponseEntity tryQuit(@PathVariable Long studyOnceId, @RequestHeader("Authorization") String authorization) { long memberId = cafegoryTokenManager.getIdentityId(authorization); LocalDateTime requestTime = LocalDateTime.now(); - studyOnceService.tryQuit(memberId, studyId); + studyOnceService.tryQuit(memberId, studyOnceId); return ResponseEntity.ok(new StudyOnceJoinResult(requestTime, true)); } - @PatchMapping("/{studyId:[0-9]+}/attendance") - public ResponseEntity takeAttendance(@PathVariable Long studyId, + @PatchMapping("/{studyOnceId:[0-9]+}/attendance") + public ResponseEntity takeAttendance(@PathVariable Long studyOnceId, @RequestHeader("Authorization") String authorization, @RequestBody UpdateAttendanceRequest request) { long leaderId = cafegoryTokenManager.getIdentityId(authorization); - UpdateAttendanceResponse response = studyOnceService.updateAttendances(leaderId, studyId, + UpdateAttendanceResponse response = studyOnceService.updateAttendances(leaderId, studyOnceId, request, LocalDateTime.now()); return ResponseEntity.ok(response); } diff --git a/src/main/java/com/example/demo/dto/StudyMemberStateRequest.java b/src/main/java/com/example/demo/dto/StudyMemberStateRequest.java index f70f3d07..e312c1ef 100644 --- a/src/main/java/com/example/demo/dto/StudyMemberStateRequest.java +++ b/src/main/java/com/example/demo/dto/StudyMemberStateRequest.java @@ -7,15 +7,15 @@ @Getter public class StudyMemberStateRequest { - private Long userId; + private Long memberId; private boolean attendance; private LocalDateTime lastUpdateTime; public StudyMemberStateRequest() { } - public StudyMemberStateRequest(Long userId, boolean attendance, LocalDateTime lastUpdateTime) { - this.userId = userId; + public StudyMemberStateRequest(Long memberId, boolean attendance, LocalDateTime lastUpdateTime) { + this.memberId = memberId; this.attendance = attendance; this.lastUpdateTime = lastUpdateTime; } diff --git a/src/main/java/com/example/demo/dto/StudyMemberStateResponse.java b/src/main/java/com/example/demo/dto/StudyMemberStateResponse.java index bb837c2f..b6d6f5d5 100644 --- a/src/main/java/com/example/demo/dto/StudyMemberStateResponse.java +++ b/src/main/java/com/example/demo/dto/StudyMemberStateResponse.java @@ -9,7 +9,7 @@ @RequiredArgsConstructor public class StudyMemberStateResponse { - private final Long userId; + private final Long memberId; private final boolean attendance; private final LocalDateTime lastUpdateTime; diff --git a/src/main/java/com/example/demo/mapper/BusinessHourMapper.java b/src/main/java/com/example/demo/mapper/BusinessHourMapper.java new file mode 100644 index 00000000..ec0e5cf7 --- /dev/null +++ b/src/main/java/com/example/demo/mapper/BusinessHourMapper.java @@ -0,0 +1,18 @@ +package com.example.demo.mapper; + +import java.util.List; +import java.util.stream.Collectors; + +import com.example.demo.domain.BusinessHour; +import com.example.demo.dto.BusinessHourResponse; + +public class BusinessHourMapper { + + public List toBusinessHourResponses(List businessHours) { + return businessHours.stream() + .map(hour -> new BusinessHourResponse(hour.getDay(), hour.getStartTime().toString(), + hour.getEndTime().toString())) + .collect(Collectors.toList()); + } + +} diff --git a/src/main/java/com/example/demo/mapper/CafeMapper.java b/src/main/java/com/example/demo/mapper/CafeMapper.java new file mode 100644 index 00000000..af23de5f --- /dev/null +++ b/src/main/java/com/example/demo/mapper/CafeMapper.java @@ -0,0 +1,130 @@ +package com.example.demo.mapper; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import com.example.demo.domain.BusinessHour; +import com.example.demo.domain.CafeImpl; +import com.example.demo.domain.CafeSearchCondition; +import com.example.demo.domain.OpenChecker; +import com.example.demo.dto.BusinessHourResponse; +import com.example.demo.dto.CafeBasicInfoResponse; +import com.example.demo.dto.CafeResponse; +import com.example.demo.dto.CafeSearchRequest; +import com.example.demo.dto.CafeSearchResponse; +import com.example.demo.dto.CanMakeStudyOnceResponse; +import com.example.demo.dto.ReviewResponse; +import com.example.demo.dto.SnsResponse; +import com.example.demo.dto.StudyOnceForCafeResponse; + +public class CafeMapper { + + public List toCafeSearchResponses(List cafes, + OpenChecker openChecker) { + return cafes.stream() + .map(cafe -> + new CafeSearchResponse( + cafe.getId(), + cafe.getName(), + cafe.showFullAddress(), + cafe.getBusinessHours().stream() + .map(hour -> new BusinessHourResponse(hour.getDay(), hour.getStartTime().toString(), + hour.getEndTime().toString())) + .collect(Collectors.toList()), + cafe.isOpen(openChecker), + cafe.getSnsDetails().stream() + .map(s -> new SnsResponse(s.getName(), s.getUrl())) + .collect(Collectors.toList()), + cafe.getPhone(), + cafe.getMinBeveragePrice(), + cafe.getMaxAllowableStay().getValue(), + cafe.getAvgReviewRate() + ) + ) + .collect(Collectors.toList()); + } + + public CafeBasicInfoResponse toCafeBasicInfoResponse(CafeImpl cafe, + List businessHourResponses, + List snsResponses, + OpenChecker openChecker) { + return new CafeBasicInfoResponse( + cafe.getId(), + cafe.getName(), + cafe.showFullAddress(), + businessHourResponses, + cafe.isOpen(openChecker), + snsResponses, + cafe.getPhone(), + cafe.getMinBeveragePrice(), + cafe.getMaxAllowableStay().getValue(), + cafe.getAvgReviewRate() + ); + } + + public CafeSearchCondition toCafeSearchCondition(CafeSearchRequest request) { + return new CafeSearchCondition.Builder(request.isCanStudy(), + request.getArea()) + .maxTime(request.getMaxTime()) + .minMenuPrice(request.getMinBeveragePrice()) + .startTime(request.getStartTime()) + .endTime(request.getEndTime()) + .build(); + } + + public CafeResponse toCafeResponse( + CafeImpl findCafe, + List businessHourResponses, + List snsResponses, + List reviewResponses, + List studyOnceForCafeResponses, + OpenChecker openChecker) { + return CafeResponse.builder() + .basicInfo( + toCafeBasicInfoResponse( + findCafe, + businessHourResponses, + snsResponses, + openChecker) + ) + .review( + reviewResponses + ) + .meetings( + studyOnceForCafeResponses + ) + .canMakeMeeting( + List.of(new CanMakeStudyOnceResponse(), new CanMakeStudyOnceResponse()) + ) + .build(); + } + + public CafeResponse toCafeResponseWithEmptyInfo( + CafeImpl findCafe, + List businessHourResponses, + List snsResponses, + List reviewResponses, + OpenChecker openChecker + ) { + return CafeResponse.builder() + .basicInfo( + toCafeBasicInfoResponse( + findCafe, + businessHourResponses, + snsResponses, + openChecker) + ) + .review( + reviewResponses + ) + .meetings( + Collections.emptyList() + ) + .canMakeMeeting( + Collections.emptyList() + ) + .build(); + } + +} diff --git a/src/main/java/com/example/demo/mapper/ReviewMapper.java b/src/main/java/com/example/demo/mapper/ReviewMapper.java new file mode 100644 index 00000000..eef679d1 --- /dev/null +++ b/src/main/java/com/example/demo/mapper/ReviewMapper.java @@ -0,0 +1,77 @@ +package com.example.demo.mapper; + +import java.util.List; +import java.util.stream.Collectors; + +import com.example.demo.domain.MemberImpl; +import com.example.demo.domain.ReviewImpl; +import com.example.demo.dto.ReviewResponse; +import com.example.demo.dto.ReviewSearchResponse; +import com.example.demo.dto.WriterResponse; + +public class ReviewMapper { + + public List toReviewSearchResponses(List reviews) { + return reviews.stream() + .map(review -> + new ReviewSearchResponse( + review.getId(), + produceWriterResponse( + review.getMember().getId(), review.getMember().getName(), + review.getMember().getThumbnailImage().getThumbnailImage() + ), + review.getRate(), + review.getContent() + )) + .collect(Collectors.toList()); + } + + private WriterResponse produceWriterResponse(Long memberId, String name, String thumbnailImg) { + return new WriterResponse(memberId, name, thumbnailImg); + } + + public ReviewResponse toReviewResponse(ReviewImpl findReview) { + return ReviewResponse.builder() + .reviewId(findReview.getId()) + .writer( + produceWriterResponse( + findReview.getMember().getId(), + findReview.getMember().getName(), + findReview.getMember().getThumbnailImage().getThumbnailImage() + )) + .rate(findReview.getRate()) + .content(findReview.getContent()) + .build(); + } + + public List toReviewResponses(List reviews) { + return reviews.stream() + .map(review -> + produceReviewResponse( + review.getId(), + productWriterResponse(review.getMember()), + review.getRate(), + review.getContent() + ) + ) + .collect(Collectors.toList()); + } + + private ReviewResponse produceReviewResponse(Long reviewId, WriterResponse writerResponse, double rate, + String content) { + return ReviewResponse.builder() + .reviewId(reviewId) + .writer(writerResponse) + .rate(rate) + .content(content) + .build(); + } + + private WriterResponse productWriterResponse(MemberImpl member) { + return new WriterResponse(member.getId(), + member.getName(), + member.getThumbnailImage().getThumbnailImage() + ); + } + +} diff --git a/src/main/java/com/example/demo/mapper/SnsDetailMapper.java b/src/main/java/com/example/demo/mapper/SnsDetailMapper.java new file mode 100644 index 00000000..1fe6a7a2 --- /dev/null +++ b/src/main/java/com/example/demo/mapper/SnsDetailMapper.java @@ -0,0 +1,16 @@ +package com.example.demo.mapper; + +import java.util.List; +import java.util.stream.Collectors; + +import com.example.demo.domain.SnsDetail; +import com.example.demo.dto.SnsResponse; + +public class SnsDetailMapper { + + public List toSnsResponses(List snsDetails) { + return snsDetails.stream() + .map(snsDetail -> new SnsResponse(snsDetail.getName(), snsDetail.getUrl())) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/com/example/demo/mapper/StudyOnceMapper.java b/src/main/java/com/example/demo/mapper/StudyOnceMapper.java new file mode 100644 index 00000000..122c475b --- /dev/null +++ b/src/main/java/com/example/demo/mapper/StudyOnceMapper.java @@ -0,0 +1,62 @@ +package com.example.demo.mapper; + +import java.util.List; +import java.util.stream.Collectors; + +import com.example.demo.domain.CafeImpl; +import com.example.demo.domain.MemberImpl; +import com.example.demo.domain.StudyOnceImpl; +import com.example.demo.dto.StudyOnceCreateRequest; +import com.example.demo.dto.StudyOnceForCafeResponse; +import com.example.demo.dto.StudyOnceSearchResponse; + +public class StudyOnceMapper { + + public List toStudyOnceForCafeResponse(CafeImpl findCafe) { + return findCafe.getStudyOnceGroup().stream() + .map(studyOnce -> + StudyOnceForCafeResponse.builder() + .cafeId(findCafe.getId()) + .studyOnceId(studyOnce.getId()) + .name(studyOnce.getName()) + .startDateTime(studyOnce.getStartDateTime()) + .endDateTime(studyOnce.getEndDateTime()) + .maxMemberCount(studyOnce.getMaxMemberCount()) + .nowMemberCount(studyOnce.getNowMemberCount()) + .isEnd(studyOnce.isAbleToTalk()) + .build() + ) + .collect(Collectors.toList()); + } + + public StudyOnceImpl toNewEntity(StudyOnceCreateRequest studyOnceCreateRequest, CafeImpl cafe, + MemberImpl leader) { + return StudyOnceImpl.builder() + .name(studyOnceCreateRequest.getName()) + .startDateTime(studyOnceCreateRequest.getStartDateTime()) + .endDateTime(studyOnceCreateRequest.getEndDateTime()) + .maxMemberCount(studyOnceCreateRequest.getMaxMemberCount()) + .nowMemberCount(0) + .isEnd(false) + .ableToTalk(studyOnceCreateRequest.isCanTalk()) + .cafe(cafe) + .leader(leader) + .build(); + } + + public StudyOnceSearchResponse toStudyOnceSearchResponse(StudyOnceImpl saved, boolean canJoin) { + return StudyOnceSearchResponse.builder() + .cafeId(saved.getCafe().getId()) + .studyOnceId(saved.getId()) + .name(saved.getName()) + .startDateTime(saved.getStartDateTime()) + .endDateTime(saved.getEndDateTime()) + .maxMemberCount(saved.getMaxMemberCount()) + .nowMemberCount(saved.getNowMemberCount()) + .canTalk(saved.isAbleToTalk()) + .canJoin(canJoin) + .isEnd(saved.isEnd()) + .build(); + } + +} diff --git a/src/main/java/com/example/demo/service/CafeQueryServiceImpl.java b/src/main/java/com/example/demo/service/CafeQueryServiceImpl.java index 4202b0b6..f8f84704 100644 --- a/src/main/java/com/example/demo/service/CafeQueryServiceImpl.java +++ b/src/main/java/com/example/demo/service/CafeQueryServiceImpl.java @@ -2,9 +2,7 @@ import static com.example.demo.exception.ExceptionType.*; -import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -16,18 +14,16 @@ import com.example.demo.domain.CafeImpl; import com.example.demo.domain.CafeSearchCondition; import com.example.demo.domain.OpenChecker; -import com.example.demo.dto.BusinessHourResponse; -import com.example.demo.dto.CafeBasicInfoResponse; import com.example.demo.dto.CafeResponse; import com.example.demo.dto.CafeSearchRequest; import com.example.demo.dto.CafeSearchResponse; -import com.example.demo.dto.CanMakeStudyOnceResponse; import com.example.demo.dto.PagedResponse; -import com.example.demo.dto.ReviewResponse; -import com.example.demo.dto.SnsResponse; -import com.example.demo.dto.StudyOnceForCafeResponse; -import com.example.demo.dto.WriterResponse; import com.example.demo.exception.CafegoryException; +import com.example.demo.mapper.BusinessHourMapper; +import com.example.demo.mapper.CafeMapper; +import com.example.demo.mapper.ReviewMapper; +import com.example.demo.mapper.SnsDetailMapper; +import com.example.demo.mapper.StudyOnceMapper; import com.example.demo.repository.MemberRepository; import com.example.demo.repository.cafe.CafeQueryRepository; import com.example.demo.repository.cafe.CafeRepository; @@ -44,21 +40,34 @@ public class CafeQueryServiceImpl implements CafeQueryService { private final CafeRepository cafeRepository; private final MemberRepository memberRepository; private OpenChecker openChecker = new BusinessHourOpenChecker(); + private final BusinessHourMapper businessHourMapper; + private final SnsDetailMapper snsDetailMapper; + private final CafeMapper cafeMapper; + private final ReviewMapper reviewMapper; + private final StudyOnceMapper studyOnceMapper; @Override public PagedResponse searchWithPagingByDynamicFilter(CafeSearchRequest request) { Pageable pageable = PageRequestCustom.of(request.getPage(), request.getSizePerPage()); - CafeSearchCondition cafeSearchCondition = createCafeSearchCondition(request); + CafeSearchCondition cafeSearchCondition = cafeMapper.toCafeSearchCondition(request); Page pagedCafes = cafeQueryRepository.findWithDynamicFilter(cafeSearchCondition, pageable); - return createPagedResponse(pagedCafes, mapToResponseList(pagedCafes)); + return createPagedResponse(pagedCafes, + cafeMapper.toCafeSearchResponses(pagedCafes.getContent(), openChecker)); } @Override public CafeResponse searchCafeById(Long cafeId) { CafeImpl findCafe = findCafeById(cafeId); - return produceCafeResponse(findCafe); + return cafeMapper.toCafeResponse( + findCafe, + businessHourMapper.toBusinessHourResponses(findCafe.getBusinessHours()), + snsDetailMapper.toSnsResponses(findCafe.getSnsDetails()), + reviewMapper.toReviewResponses(findCafe.getReviews()), + studyOnceMapper.toStudyOnceForCafeResponse(findCafe), + openChecker + ); } private CafeImpl findCafeById(Long cafeId) { @@ -72,47 +81,26 @@ public CafeResponse searchCafeForMemberByCafeId(Long cafeId, Long memberId) { if (!memberRepository.existsById(memberId)) { throw new CafegoryException(MEMBER_NOT_FOUND); } - return produceCafeResponse(findCafe); + return cafeMapper.toCafeResponse( + findCafe, + businessHourMapper.toBusinessHourResponses(findCafe.getBusinessHours()), + snsDetailMapper.toSnsResponses(findCafe.getSnsDetails()), + reviewMapper.toReviewResponses(findCafe.getReviews()), + studyOnceMapper.toStudyOnceForCafeResponse(findCafe), + openChecker + ); } @Override public CafeResponse searchCafeForNotMemberByCafeId(Long cafeId) { CafeImpl findCafe = findCafeById(cafeId); - return produceCafeResponseWithEmptyInfo(findCafe); - } - - private CafeResponse produceCafeResponseWithEmptyInfo(CafeImpl findCafe) { - return CafeResponse.builder() - .basicInfo( - produceCafeBasicInfoResponse(findCafe) - ) - .review( - mapToReviewResponseList(findCafe) - ) - .meetings( - Collections.emptyList() - ) - .canMakeMeeting( - Collections.emptyList() - ) - .build(); - } - - private CafeResponse produceCafeResponse(CafeImpl findCafe) { - return CafeResponse.builder() - .basicInfo( - produceCafeBasicInfoResponse(findCafe) - ) - .review( - mapToReviewResponseList(findCafe) - ) - .meetings( - mapToStudyOnceForCafeResponse(findCafe) - ) - .canMakeMeeting( - List.of(new CanMakeStudyOnceResponse(), new CanMakeStudyOnceResponse()) - ) - .build(); + return cafeMapper.toCafeResponseWithEmptyInfo( + findCafe, + businessHourMapper.toBusinessHourResponses(findCafe.getBusinessHours()), + snsDetailMapper.toSnsResponses(findCafe.getSnsDetails()), + reviewMapper.toReviewResponses(findCafe.getReviews()), + openChecker + ); } private PagedResponse createPagedResponse(Page pagedCafes, @@ -125,91 +113,4 @@ private PagedResponse createPagedResponse(Page pag ); } - private List mapToResponseList(Page pagedCafes) { - return pagedCafes.getContent().stream() - .map(cafe -> - new CafeSearchResponse( - cafe.getId(), - cafe.getName(), - cafe.showFullAddress(), - cafe.getBusinessHours().stream() - .map(hour -> new BusinessHourResponse(hour.getDay(), hour.getStartTime().toString(), - hour.getEndTime().toString())) - .collect(Collectors.toList()), - cafe.isOpen(openChecker), - cafe.getSnsDetails().stream() - .map(s -> new SnsResponse(s.getName(), s.getUrl())) - .collect(Collectors.toList()), - cafe.getPhone(), - cafe.getMinBeveragePrice(), - cafe.getMaxAllowableStay().getValue(), - cafe.getAvgReviewRate() - ) - ) - .collect(Collectors.toList()); - } - - private CafeSearchCondition createCafeSearchCondition(CafeSearchRequest request) { - return new CafeSearchCondition.Builder(request.isCanStudy(), - request.getArea()) - .maxTime(request.getMaxTime()) - .minMenuPrice(request.getMinBeveragePrice()) - .startTime(request.getStartTime()) - .endTime(request.getEndTime()) - .build(); - } - - private CafeBasicInfoResponse produceCafeBasicInfoResponse(CafeImpl findCafe) { - return new CafeBasicInfoResponse( - findCafe.getId(), - findCafe.getName(), - findCafe.showFullAddress(), - findCafe.getBusinessHours().stream() - .map(hour -> new BusinessHourResponse(hour.getDay(), hour.getStartTime().toString(), - hour.getEndTime().toString())) - .collect(Collectors.toList()), - findCafe.isOpen(openChecker), - findCafe.getSnsDetails().stream() - .map(s -> new SnsResponse(s.getName(), s.getUrl())) - .collect(Collectors.toList()), - findCafe.getPhone(), - findCafe.getMinBeveragePrice(), - findCafe.getMaxAllowableStay().getValue(), - findCafe.getAvgReviewRate() - ); - } - - private List mapToReviewResponseList(CafeImpl findCafe) { - return findCafe.getReviews().stream() - .map(review -> - ReviewResponse.builder() - .reviewId(review.getId()) - .writer( - new WriterResponse(review.getMember().getId(), - review.getMember().getName(), - review.getMember().getThumbnailImage().getThumbnailImage() - )) - .rate(review.getRate()) - .content(review.getContent()) - .build() - ) - .collect(Collectors.toList()); - } - - private List mapToStudyOnceForCafeResponse(CafeImpl findCafe) { - return findCafe.getStudyOnceGroup().stream() - .map(studyOnce -> - StudyOnceForCafeResponse.builder() - .cafeId(findCafe.getId()) - .studyOnceId(studyOnce.getId()) - .name(studyOnce.getName()) - .startDateTime(studyOnce.getStartDateTime()) - .endDateTime(studyOnce.getEndDateTime()) - .maxMemberCount(studyOnce.getMaxMemberCount()) - .nowMemberCount(studyOnce.getNowMemberCount()) - .isEnd(studyOnce.isAbleToTalk()) - .build() - ) - .collect(Collectors.toList()); - } } diff --git a/src/main/java/com/example/demo/service/ReviewQueryServiceImpl.java b/src/main/java/com/example/demo/service/ReviewQueryServiceImpl.java index 7305215c..8a5fa400 100644 --- a/src/main/java/com/example/demo/service/ReviewQueryServiceImpl.java +++ b/src/main/java/com/example/demo/service/ReviewQueryServiceImpl.java @@ -3,7 +3,6 @@ import static com.example.demo.exception.ExceptionType.*; import java.util.List; -import java.util.stream.Collectors; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -15,8 +14,8 @@ import com.example.demo.dto.ReviewResponse; import com.example.demo.dto.ReviewSearchRequest; import com.example.demo.dto.ReviewSearchResponse; -import com.example.demo.dto.WriterResponse; import com.example.demo.exception.CafegoryException; +import com.example.demo.mapper.ReviewMapper; import com.example.demo.repository.ReviewRepository; import com.example.demo.repository.cafe.CafeRepository; import com.example.demo.util.PageRequestCustom; @@ -30,6 +29,7 @@ public class ReviewQueryServiceImpl implements ReviewQueryService { private final ReviewRepository reviewRepository; private final CafeRepository cafeRepository; + private final ReviewMapper reviewMapper; @Override public PagedResponse searchWithPagingByCafeId(ReviewSearchRequest request) { @@ -37,12 +37,13 @@ public PagedResponse searchWithPagingByCafeId(ReviewSearch Pageable pageable = PageRequestCustom.of(request.getPage(), request.getSizePerPage()); Page pagedReviews = reviewRepository.findAllWithPagingByCafeId(request.getCafeId(), pageable); - return createPagedResponse(pagedReviews, mapToResponseList(pagedReviews)); + return createPagedResponse(pagedReviews, + reviewMapper.toReviewSearchResponses(pagedReviews.getContent())); } @Override public ReviewResponse searchOne(Long reviewId) { - return mapToReviewResponse(findReviewById(reviewId)); + return reviewMapper.toReviewResponse(findReviewById(reviewId)); } private ReviewImpl findReviewById(Long reviewId) { @@ -50,19 +51,6 @@ private ReviewImpl findReviewById(Long reviewId) { .orElseThrow(() -> new CafegoryException(REVIEW_NOT_FOUND)); } - private ReviewResponse mapToReviewResponse(ReviewImpl findReview) { - return ReviewResponse.builder() - .reviewId(findReview.getId()) - .writer( - new WriterResponse(findReview.getMember().getId(), - findReview.getMember().getName(), - findReview.getMember().getThumbnailImage().getThumbnailImage() - )) - .rate(findReview.getRate()) - .content(findReview.getContent()) - .build(); - } - private void validateExistCafe(Long cafeId) { if (!cafeRepository.existsById(cafeId)) { throw new CafegoryException(CAFE_NOT_FOUND); @@ -79,17 +67,4 @@ private PagedResponse createPagedResponse(Page ); } - private List mapToResponseList(Page pagedReviews) { - return pagedReviews.getContent().stream() - .map(review -> - new ReviewSearchResponse( - review.getId(), - new WriterResponse(review.getMember().getId(), review.getMember().getName(), - review.getMember().getThumbnailImage().getThumbnailImage()), - review.getRate(), - review.getContent() - ) - ) - .collect(Collectors.toList()); - } } diff --git a/src/main/java/com/example/demo/service/StudyOnceServiceImpl.java b/src/main/java/com/example/demo/service/StudyOnceServiceImpl.java index 0e6f46d3..7ffee458 100644 --- a/src/main/java/com/example/demo/service/StudyOnceServiceImpl.java +++ b/src/main/java/com/example/demo/service/StudyOnceServiceImpl.java @@ -25,6 +25,7 @@ import com.example.demo.dto.UpdateAttendanceRequest; import com.example.demo.dto.UpdateAttendanceResponse; import com.example.demo.exception.CafegoryException; +import com.example.demo.mapper.StudyOnceMapper; import com.example.demo.repository.MemberRepository; import com.example.demo.repository.StudyMemberRepository; import com.example.demo.repository.StudyOnceRepository; @@ -41,6 +42,7 @@ public class StudyOnceServiceImpl implements StudyOnceService { private final StudyOnceRepository studyOnceRepository; private final MemberRepository memberRepository; private final StudyMemberRepository studyMemberRepository; + private final StudyOnceMapper studyOnceMapper; @Override public void tryJoin(long memberIdThatExpectedToJoin, long studyId) { @@ -79,7 +81,8 @@ public PagedResponse searchStudy(StudyOnceSearchRequest List searchResults = studyOnceRepository.findAllByStudyOnceSearchRequest( studyOnceSearchRequest) .stream() - .map(studyOnce -> makeStudyOnceSearchResponse(studyOnce, studyOnce.canJoin(LocalDateTime.now()))) + .map(studyOnce -> studyOnceMapper.toStudyOnceSearchResponse(studyOnce, + studyOnce.canJoin(LocalDateTime.now()))) .collect(Collectors.toList()); return new PagedResponse<>(studyOnceSearchRequest.getPage(), maxPage, searchResults.size(), searchResults); } @@ -96,7 +99,7 @@ public StudyOnceSearchResponse searchByStudyId(long studyId) { StudyOnceImpl searched = studyOnceRepository.findById(studyId) .orElseThrow(() -> new CafegoryException(STUDY_ONCE_NOT_FOUND)); boolean canJoin = searched.canJoin(LocalDateTime.now()); - return makeStudyOnceSearchResponse(searched, canJoin); + return studyOnceMapper.toStudyOnceSearchResponse(searched, canJoin); } @Override @@ -118,7 +121,7 @@ private List mapToStateResponses(List stu private List generateStudyMemberIdsFromRequest(long studyOnceId, UpdateAttendanceRequest request) { return request.getStates().stream() - .map(memberReq -> new StudyMemberId(memberReq.getUserId(), studyOnceId)) + .map(memberReq -> new StudyMemberId(memberReq.getMemberId(), studyOnceId)) .collect(Collectors.toList()); } @@ -126,7 +129,7 @@ private void processAttendanceUpdates(long leaderId, long studyOnceId, UpdateAtt LocalDateTime now) { for (StudyMemberStateRequest memberStateRequest : request.getStates()) { Attendance attendance = memberStateRequest.isAttendance() ? Attendance.YES : Attendance.NO; - updateAttendance(leaderId, studyOnceId, memberStateRequest.getUserId(), attendance, now); + updateAttendance(leaderId, studyOnceId, memberStateRequest.getMemberId(), attendance, now); } } @@ -175,10 +178,10 @@ public StudyOnceSearchResponse createStudy(long leaderId, StudyOnceCreateRequest //ToDo 카페 영업시간 이내인지 확인 하는 작업 추가 필요 LocalDateTime startDateTime = studyOnceCreateRequest.getStartDateTime(); MemberImpl leader = getMember(leaderId, startDateTime); - StudyOnceImpl studyOnce = makeNewStudyOnce(studyOnceCreateRequest, cafe, leader); + StudyOnceImpl studyOnce = studyOnceMapper.toNewEntity(studyOnceCreateRequest, cafe, leader); StudyOnceImpl saved = studyOnceRepository.save(studyOnce); boolean canJoin = true; - return makeStudyOnceSearchResponse(saved, canJoin); + return studyOnceMapper.toStudyOnceSearchResponse(saved, canJoin); } private MemberImpl getMember(long leaderId, LocalDateTime startDateTime) { @@ -189,34 +192,4 @@ private MemberImpl getMember(long leaderId, LocalDateTime startDateTime) { return leader; } - private static StudyOnceImpl makeNewStudyOnce(StudyOnceCreateRequest studyOnceCreateRequest, CafeImpl cafe, - MemberImpl leader) { - return StudyOnceImpl.builder() - .name(studyOnceCreateRequest.getName()) - .startDateTime(studyOnceCreateRequest.getStartDateTime()) - .endDateTime(studyOnceCreateRequest.getEndDateTime()) - .maxMemberCount(studyOnceCreateRequest.getMaxMemberCount()) - .nowMemberCount(0) - .isEnd(false) - .ableToTalk(studyOnceCreateRequest.isCanTalk()) - .cafe(cafe) - .leader(leader) - .build(); - - } - - private static StudyOnceSearchResponse makeStudyOnceSearchResponse(StudyOnceImpl saved, boolean canJoin) { - return StudyOnceSearchResponse.builder() - .cafeId(saved.getCafe().getId()) - .studyOnceId(saved.getId()) - .name(saved.getName()) - .startDateTime(saved.getStartDateTime()) - .endDateTime(saved.getEndDateTime()) - .maxMemberCount(saved.getMaxMemberCount()) - .nowMemberCount(saved.getNowMemberCount()) - .canTalk(saved.isAbleToTalk()) - .canJoin(canJoin) - .isEnd(saved.isEnd()) - .build(); - } } diff --git a/src/test/java/com/example/demo/service/StudyOnceServiceImplTest.java b/src/test/java/com/example/demo/service/StudyOnceServiceImplTest.java index 0634db13..c77e5915 100644 --- a/src/test/java/com/example/demo/service/StudyOnceServiceImplTest.java +++ b/src/test/java/com/example/demo/service/StudyOnceServiceImplTest.java @@ -584,7 +584,7 @@ void can_take_attendances() { em.clear(); //then List studyMemberIds = memberStateRequests.stream() - .map(memberReq -> new StudyMemberId(memberReq.getUserId(), studyOnce.getId())) + .map(memberReq -> new StudyMemberId(memberReq.getMemberId(), studyOnce.getId())) .collect(Collectors.toList()); List searchedStudyMembers = studyMemberRepository.findAllById(studyMemberIds);