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

[refactor] 여행지 리뷰(PlaceReview) 기능 추가 및 리팩토링 #96

Merged
merged 6 commits into from
Jan 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,18 @@
import com.haejwo.tripcometrue.domain.review.placereview.dto.response.PlaceReviewResponseDto;
import com.haejwo.tripcometrue.domain.review.placereview.dto.response.RegisterPlaceReviewResponseDto;
import com.haejwo.tripcometrue.domain.review.placereview.dto.response.delete.DeletePlaceReviewResponseDto;
import com.haejwo.tripcometrue.domain.review.placereview.dto.response.delete.DeleteSomeFailurePlaceReviewResponseDto;
import com.haejwo.tripcometrue.domain.review.placereview.service.PlaceReviewService;
import com.haejwo.tripcometrue.global.springsecurity.PrincipalDetails;
import com.haejwo.tripcometrue.global.util.ResponseDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.MULTI_STATUS;

@Slf4j
@RestController
Expand Down Expand Up @@ -58,58 +52,50 @@ public ResponseEntity<ResponseDTO<PlaceReviewResponseDto>> getOnePlaceReview(
return ResponseEntity.ok(ResponseDTO.okWithData(responseDto));
}

@GetMapping("/{placeId}/reviews")
public ResponseEntity<ResponseDTO<Page<PlaceReviewResponseDto>>> getPlaceReviewList(
@AuthenticationPrincipal PrincipalDetails principalDetails,
@PathVariable Long placeId,
@RequestParam(defaultValue = "false") boolean onlyImage,
Pageable pageable
) {

Page<PlaceReviewResponseDto> responseDtos = placeReviewService.getPlaceReviews(principalDetails, placeId, onlyImage, pageable);
return ResponseEntity.ok(ResponseDTO.okWithData(responseDtos));
}

@PutMapping("/reviews/{placeReviewId}")
public ResponseEntity<ResponseDTO<PlaceReviewResponseDto>> modifyPlaceReview(
@AuthenticationPrincipal PrincipalDetails principalDetails,
@PathVariable Long placeReviewId,
@RequestBody @Validated PlaceReviewRequestDto requestDto) {
@RequestBody @Validated PlaceReviewRequestDto requestDto
) {

PlaceReviewResponseDto responseDto = placeReviewService
.modifyPlaceReview(principalDetails, placeReviewId, requestDto);
return ResponseEntity.ok(ResponseDTO.okWithData(responseDto));
}

//todo : 로그인한 사람이 맞는지 확인
//todo : 복수형 s 붙이기가
@DeleteMapping("/reviews")
public ResponseEntity<ResponseDTO<DeletePlaceReviewResponseDto>> removePlaceReview(
@RequestBody DeletePlaceReviewRequestDto requestDto) {
public ResponseEntity<ResponseDTO<DeletePlaceReviewResponseDto>> removePlaceReviews(
@RequestBody DeletePlaceReviewRequestDto requestDto
) {

DeletePlaceReviewResponseDto responseDto = placeReviewService.deletePlaceReview(requestDto);
DeletePlaceReviewResponseDto responseDto =
placeReviewService.deletePlaceReviews(requestDto);
return ResponseEntity.ok(ResponseDTO.okWithData(responseDto));
}

if (responseDto instanceof DeleteSomeFailurePlaceReviewResponseDto) {
return ResponseEntity
.status(MULTI_STATUS)
.body(ResponseDTO.errorWithData(MULTI_STATUS, responseDto));
}
@GetMapping("/{placeId}/reviews")
public ResponseEntity<ResponseDTO<PlaceReviewListResponseDto>> getPlaceReviewList(
@AuthenticationPrincipal PrincipalDetails principalDetails,
@PathVariable Long placeId,
@RequestParam(defaultValue = "false") boolean onlyImage,
Pageable pageable
) {

return ResponseEntity.ok(ResponseDTO.okWithData(responseDto));
PlaceReviewListResponseDto responseDtos =
placeReviewService.getPlaceReviewList(principalDetails, placeId, onlyImage, pageable);
return ResponseEntity.ok(ResponseDTO.okWithData(responseDtos));
}

@GetMapping("/reviews/my")
public ResponseEntity<ResponseDTO<List<PlaceReviewListResponseDto>>> getMyPlaceReviews(
@AuthenticationPrincipal PrincipalDetails principalDetails,
Pageable pageable
public ResponseEntity<ResponseDTO<PlaceReviewListResponseDto>> getMyPlaceReviews(
@AuthenticationPrincipal PrincipalDetails principalDetails,
Pageable pageable
) {
List<PlaceReviewListResponseDto> responseDtos
= placeReviewService.getMyPlaceReviewsList(principalDetails, pageable);
ResponseDTO<List<PlaceReviewListResponseDto>> responseBody
= ResponseDTO.okWithData(responseDtos);

return ResponseEntity
.status(HttpStatus.OK)
.body(responseBody);
PlaceReviewListResponseDto responseDtos
= placeReviewService.getMyPlaceReviewList(principalDetails, pageable);
return ResponseEntity.ok((ResponseDTO.okWithData(responseDtos)));
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package com.haejwo.tripcometrue.domain.review.placereview.dto.response;

import com.haejwo.tripcometrue.domain.review.placereview.entity.PlaceReview;
import org.springframework.data.domain.Page;

import java.time.LocalDateTime;
import java.util.List;

public record PlaceReviewListResponseDto(

Long id,
String content,
String imageUrl,
Integer likeCount,
Long memberId,
Long placeId,
LocalDateTime createdAt
Long totalCount,
int nowPageNumber,
boolean isFirst,
boolean isLast,
List<PlaceReviewResponseDto> placeReviews

) {
public static PlaceReviewListResponseDto fromEntity(PlaceReview placeReview) {
return new PlaceReviewListResponseDto(
placeReview.getId(),
placeReview.getContent(),
placeReview.getImageUrl(),
placeReview.getLikeCount(),
placeReview.getMember().getId(),
placeReview.getPlace().getId(),
placeReview.getCreatedAt()
);
}
public static PlaceReviewListResponseDto fromResponseDtos(
Page<PlaceReview> reviews,
List<PlaceReviewResponseDto> placeReviews
) {
return new PlaceReviewListResponseDto(
reviews.getTotalElements(),
reviews.getNumber(),
reviews.isFirst(),
reviews.isLast(),
placeReviews
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.ArrayList;
import java.util.List;

import static com.haejwo.tripcometrue.domain.review.global.PointType.ONLY_ONE_POINT;
import static com.haejwo.tripcometrue.domain.review.global.PointType.TWO_POINTS;
import static jakarta.persistence.FetchType.LAZY;

@Getter
Expand Down Expand Up @@ -43,6 +45,7 @@ public class PlaceReview extends BaseTimeEntity {
private String imageUrl;
private Integer likeCount;
private Integer commentCount;
private boolean hasAnyRegisteredPhotoUrl;

@Builder
public PlaceReview(Member member, Place place, String content, String imageUrl) {
Expand All @@ -52,8 +55,23 @@ public PlaceReview(Member member, Place place, String content, String imageUrl)
this.imageUrl = imageUrl;
}

public void update(PlaceReviewRequestDto requestDto) {
public void save(PlaceReviewRequestDto requestDto, Member member) {
if (requestDto.imageUrl() != null) {
member.earnPoint(TWO_POINTS.getPoint());
hasAnyRegisteredPhotoUrl = true;
return;
}

member.earnPoint(ONLY_ONE_POINT.getPoint());
}

public void update(PlaceReviewRequestDto requestDto, Member member) {
this.content = requestDto.content();

if (!hasAnyRegisteredPhotoUrl && requestDto.imageUrl() != null) {
member.earnPoint(ONLY_ONE_POINT.getPoint());
hasAnyRegisteredPhotoUrl = true;
}
this.imageUrl = requestDto.imageUrl();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import com.haejwo.tripcometrue.domain.member.entity.Member;
import com.haejwo.tripcometrue.domain.place.entity.Place;
import com.haejwo.tripcometrue.domain.review.placereview.entity.PlaceReview;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PlaceReviewRepository extends JpaRepository<PlaceReview, Long>, PlaceReviewRepositoryCustom {

boolean existsByMemberAndPlace(Member member, Place place);

List<PlaceReview> findByMemberId(Long memberId, Pageable pageable);
@Query("select pr from PlaceReview pr join fetch pr.member m where pr.member = :member order by pr.createdAt desc")
Page<PlaceReview> findByMember(@Param("member") Member member, Pageable pageable);
}
Loading
Loading