Skip to content

Commit

Permalink
Merge pull request #112 from dionisos198/refactor/#111-update-review-dto
Browse files Browse the repository at this point in the history
Refactor/#111 update review dto
  • Loading branch information
dionisos198 authored Jan 19, 2024
2 parents 685a04e + 416cf6d commit 2c5c40f
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@RestController
@RequiredArgsConstructor
@Api(tags = {"프로그램 좋아요 및 관심없어요 지정"})
@Api(tags = {"프로그램 좋아요 및 관심없어요 지정 컨트롤러"})

public class ProgramChoiceController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,52 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import tavebalak.OTTify.common.BaseResponse;
import tavebalak.OTTify.error.ErrorCode;
import tavebalak.OTTify.error.exception.UnauthorizedException;
import tavebalak.OTTify.oauth.jwt.SecurityUtil;
import tavebalak.OTTify.program.dto.programDetails.Response.ProgramResponseDto;
import tavebalak.OTTify.program.dto.response.UserSpecificRatingResponseDto;
import tavebalak.OTTify.program.service.ProgramDetailsShowService;
import tavebalak.OTTify.user.entity.User;
import tavebalak.OTTify.user.repository.UserRepository;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/program/{programId}")
@Api(tags = {"프로그램에 대한 상세정보 컨트롤러"})

public class ProgramDetailsController {

private final ProgramDetailsShowService programDetailsShowService;
private final UserRepository userRepository;


@ApiOperation(value = "프로그램에 대한 상세 정보 보기", notes = "프로그램에 대한 상세 정보를 받아옵니다")
@ApiImplicitParam(name = "programId", dataType = "long", value = "프로그램 ID", required = true, paramType = "path")
@GetMapping("/api/v1/program/{programId}/details")
@GetMapping("/details")
@ResponseStatus(HttpStatus.OK)
public BaseResponse<ProgramResponseDto> getProgramDetails(
@PathVariable("programId") Long programId) {
return BaseResponse.success(programDetailsShowService.showDetails(programId));
}

@ApiOperation(value = "자신의 취향에 맞는 사람들의 리뷰의 평균 별점 및 현재 자신의 첫번째 장르 보여주기", notes = "자신의 취향에 맞는 사람들의 리뷰의 평균 별점, 첫번째 장르를 보여줍니다")
@ApiImplicitParam(name = "programId", dataType = "long", value = "현재 프로그램의 ID", required = true, paramType = "path")
@GetMapping("/user/specific/rating")
@ResponseStatus(HttpStatus.OK)
public BaseResponse<UserSpecificRatingResponseDto> showUserSpecificReviewRating(
@PathVariable("programId") Long programId) {
User findUser = getUser();
return BaseResponse.success(
programDetailsShowService.showUserSpecificRating(findUser, programId));
}

private User getUser() {
return userRepository.findByEmail(SecurityUtil.getCurrentEmail().get())
.orElseThrow(() -> new UnauthorizedException(ErrorCode.UNAUTHORIZED));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tavebalak.OTTify.program.dto.response;

import lombok.Getter;

@Getter
public class UserSpecificRatingResponseDto {

private String usersFirstGenreName;
private String usersFirstGenreProgramRating;

public UserSpecificRatingResponseDto(String usersFirstGenreName,
double usersFirstGenreProgramRating) {
this.usersFirstGenreName = usersFirstGenreName;
this.usersFirstGenreProgramRating = String.format("%.1f", usersFirstGenreProgramRating);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package tavebalak.OTTify.program.service;

import tavebalak.OTTify.program.dto.programDetails.Response.ProgramResponseDto;
import tavebalak.OTTify.program.dto.response.UserSpecificRatingResponseDto;
import tavebalak.OTTify.user.entity.User;

public interface ProgramDetailsShowService {
ProgramResponseDto showDetails(Long programId);

ProgramResponseDto showDetails(Long programId);

UserSpecificRatingResponseDto showUserSpecificRating(User user, Long programId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
import tavebalak.OTTify.error.ErrorCode;
import tavebalak.OTTify.error.exception.NotFoundException;
import tavebalak.OTTify.genre.entity.Genre;
import tavebalak.OTTify.genre.repository.GenreRepository;
import tavebalak.OTTify.genre.repository.UserGenreRepository;
import tavebalak.OTTify.program.dto.programDetails.Response.ProgramDetailResponse;
import tavebalak.OTTify.program.dto.programDetails.Response.ProgramProviderListResponseDto;
import tavebalak.OTTify.program.dto.programDetails.Response.ProgramProviderResponseDto;
Expand All @@ -25,11 +27,14 @@
import tavebalak.OTTify.program.dto.programDetails.openApiRequest.programDetailRequest.OATvDetailsDto;
import tavebalak.OTTify.program.dto.programDetails.openApiRequest.providerDetails.OACountryDetailsDto;
import tavebalak.OTTify.program.dto.programDetails.openApiRequest.providerDetails.OAProgramProviderDto;
import tavebalak.OTTify.program.dto.response.UserSpecificRatingResponseDto;
import tavebalak.OTTify.program.entity.Ott;
import tavebalak.OTTify.program.entity.Program;
import tavebalak.OTTify.program.entity.ProgramType;
import tavebalak.OTTify.program.repository.OttRepository;
import tavebalak.OTTify.program.repository.ProgramRepository;
import tavebalak.OTTify.review.repository.ReviewRepository;
import tavebalak.OTTify.user.entity.User;

@Service
@Transactional(readOnly = true)
Expand All @@ -40,6 +45,8 @@ public class ProgramDetailsShowServiceImpl implements ProgramDetailsShowService
private final GenreRepository genreRepository;
private final ProgramRepository programRepository;
private final OttRepository ottRepository;
private final UserGenreRepository userGenreRepository;
private final ReviewRepository reviewRepository;


@Override
Expand Down Expand Up @@ -305,4 +312,29 @@ private ProgramProviderListResponseDto changeOTTtoKoreanAndMakeProviderResponseD
.build();

}

//user 의 first genre 에 맞춘 평점과 User 의 first Genre name을 함꼐 반환합니다.

@Override
public UserSpecificRatingResponseDto showUserSpecificRating(User user, Long programId) {
Program program = programRepository.findById(programId)
.orElseThrow(() -> new NotFoundException(ErrorCode.PROGRAM_NOT_FOUND));

Genre usersFirstGenre = userGenreRepository.find1stGenreByUserIdFetchJoin(user.getId())
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_FIRST_GENRE_NOT_FOUND))
.getGenre();

int userSpecificGenreCount = reviewRepository.countByGenreName(usersFirstGenre.getName(),
program);

Double sumRating = reviewRepository.sumReviewRatingByGenreName(usersFirstGenre.getName(),
program);

double userSpecificReviewRatingSum = (sumRating != null) ? sumRating : 0.0;

double avg =
userSpecificGenreCount == 0 ? 0 : userSpecificReviewRatingSum / userSpecificGenreCount;

return new UserSpecificRatingResponseDto(usersFirstGenre.getName(), avg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
import tavebalak.OTTify.error.ErrorCode;
import tavebalak.OTTify.error.exception.UnauthorizedException;
import tavebalak.OTTify.oauth.jwt.SecurityUtil;
import tavebalak.OTTify.review.dto.reviewresponse.FourReviewResponseWithCounts;
import tavebalak.OTTify.review.dto.reviewresponse.ReviewProgramResponseDto;
import tavebalak.OTTify.review.dto.reviewresponse.ReviewResponseDtoList;
import tavebalak.OTTify.review.service.ReviewShowProgramDetailService;
import tavebalak.OTTify.user.entity.User;
import tavebalak.OTTify.user.repository.UserRepository;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/{programId}")
@Api(tags = {"프로그램 상세 페이지에서 리뷰 보여주기"})
@RequestMapping("/api/v1/reviews/{programId}")
@Api(tags = {"프로그램 상세 페이지에서 리뷰 보여주는 컨트롤러"})

public class ReviewShowProgramDetailController {

Expand All @@ -49,21 +49,21 @@ public BaseResponse<ReviewProgramResponseDto> showMyReview(

@ApiOperation(value = "프로그램 페이지의 초기 리뷰 보여주기", notes = "프로그램 페이지의 처음 4개의 리뷰를 보여줍니다")
@ApiImplicitParam(name = "programId", dataType = "long", value = "현재 프로그램의 ID", required = true, paramType = "path")
@GetMapping("/normal/reviews/count/4")
@GetMapping("/normal/count/4")
@ResponseStatus(HttpStatus.OK)
public BaseResponse<ReviewResponseDtoList> show4ReviewList(
public BaseResponse<FourReviewResponseWithCounts> show4ReviewList(
@PathVariable("programId") Long programId) {
return BaseResponse.success(reviewShowProgramDetailService.show4Review(programId));
}

@ApiOperation(value = "전체 리뷰 리스트를 보여주기", notes = "전체 리뷰 리스트를 보여줍니다")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", dataType = "int", value = "페이지 번호(0부터 시작)", defaultValue = "0", required = false, paramType = "query", example = "1"),
@ApiImplicitParam(name = "direction", dataType = "String", value = "내림차순과 오름차순", required = false, defaultValue = "DESC", paramType = "query", example = "Desc"),
@ApiImplicitParam(name = "sort", dataType = "String", value = "정렬기준(likeCounts,createdAt)", required = false, defaultValue = "likeCounts", paramType = "query", example = "createdAt"),
@ApiImplicitParam(name = "size", dataType = "int", value = "페이지당 아이템 갯수", defaultValue = "10", required = false, paramType = "query", example = "5")
@ApiImplicitParam(name = "page", dataType = "int", value = "페이지 번호(0부터 시작)", paramType = "query"),
@ApiImplicitParam(name = "direction", dataType = "String", value = "내림차순과 오름차순", paramType = "query"),
@ApiImplicitParam(name = "sort", dataType = "String", value = "정렬기준(likeCounts,createdAt)", paramType = "query"),
@ApiImplicitParam(name = "size", dataType = "int", value = "페이지당 아이템 갯수", paramType = "query")
})
@GetMapping("/normal/reviews")
@GetMapping("/normals")
@ResponseStatus(HttpStatus.OK)
public BaseResponse<Slice<ReviewProgramResponseDto>> showReviewListALL(
@PathVariable("programId") Long programId,
Expand All @@ -75,26 +75,26 @@ public BaseResponse<Slice<ReviewProgramResponseDto>> showReviewListALL(
reviewShowProgramDetailService.showReviewList(programId, pageable));
}

@ApiOperation(value = "프로그램 페이지의 자신의 취향에 맞는 장르 보여주기", notes = "자신의 취향에 맞는 좋아요 순 처음 4개의 리뷰를 보여줍니다")
@ApiOperation(value = "프로그램 페이지의 자신의 취향에 맞는 리뷰 4개 보여주기", notes = "자신의 취향에 맞는 좋아요 순 처음 4개의 리뷰를 보여줍니다")
@ApiImplicitParam(name = "programId", dataType = "long", value = "현재 프로그램의 ID", required = true, paramType = "path", example = "1")
@GetMapping("/user/specific/reviews/count/4")
@GetMapping("/user/specific/count/4")
@ResponseStatus(HttpStatus.OK)
public BaseResponse<ReviewResponseDtoList> show4UserSpecificReviewList(
public BaseResponse<FourReviewResponseWithCounts> show4UserSpecificReviewList(
@PathVariable("programId") Long programId) {
User findUser = getUser();
return BaseResponse.success(
reviewShowProgramDetailService.show4UserSpecificReviewList(findUser, programId));
}


@ApiOperation(value = "사용자의 취향에 맞는 전체 장르 리스트를 보여주기", notes = "전체 장르 리스트를 보여줍니다")
@ApiOperation(value = "사용자의 취향에 맞는 전체 리뷰 리스트를 보여주기", notes = "자신의 취향에 맞는 리뷰 리스트를 보여줍니다")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", dataType = "int", value = "페이지 번호(0부터 시작)", defaultValue = "0", required = false, paramType = "query", example = "1"),
@ApiImplicitParam(name = "direction", dataType = "String", value = "내림차순과 오름차순", required = false, defaultValue = "DESC", paramType = "query", example = "Desc"),
@ApiImplicitParam(name = "sort", dataType = "String", value = "정렬기준(likeCounts,createdAt)", required = false, defaultValue = "likeCounts", paramType = "query", example = "createdAt"),
@ApiImplicitParam(name = "size", dataType = "int", value = "페이지당 아이템 갯수", defaultValue = "10", required = false, paramType = "query", example = "5")
@ApiImplicitParam(name = "page", dataType = "int", value = "페이지 번호(0부터 시작)", paramType = "query"),
@ApiImplicitParam(name = "direction", dataType = "String", value = "내림차순과 오름차순", paramType = "query"),
@ApiImplicitParam(name = "sort", dataType = "String", value = "정렬기준(likeCounts,createdAt)", paramType = "query"),
@ApiImplicitParam(name = "size", dataType = "int", value = "페이지당 아이템 갯수", paramType = "query")
})
@GetMapping("/user/specific/reviews")
@GetMapping("/user/specifics")
@ResponseStatus(HttpStatus.OK)
public BaseResponse<Slice<ReviewProgramResponseDto>> showUserSpecificReviewListALL(
@PathVariable("programId") Long programId,
Expand All @@ -109,17 +109,6 @@ public BaseResponse<Slice<ReviewProgramResponseDto>> showUserSpecificReviewListA
}


@ApiOperation(value = "자신의 취향에 맞는 사람들의 리뷰의 평균 별점 보여주기", notes = "자신의 취향에 맞는 사람들의 리뷰의 평균 별점 보여줍니다")
@ApiImplicitParam(name = "programId", dataType = "long", value = "현재 프로그램의 ID", required = true, paramType = "path")
@GetMapping("/user/specific/review/rating")
@ResponseStatus(HttpStatus.OK)
public BaseResponse<String> showUserSpecificReviewRating(
@PathVariable("programId") Long programId) {
User findUser = getUser();
return BaseResponse.success(
reviewShowProgramDetailService.showUserSpecificRating(findUser, programId));
}

private User getUser() {
return userRepository.findByEmail(SecurityUtil.getCurrentEmail().get())
.orElseThrow(() -> new UnauthorizedException(ErrorCode.UNAUTHORIZED));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tavebalak.OTTify.review.dto.reviewresponse;

import lombok.Getter;

@Getter
public class FourReviewResponseWithCounts {

private ReviewResponseDtoList reviewResponseDtoList;
private int leftReviewCounts;

public FourReviewResponseWithCounts(ReviewResponseDtoList reviewResponseDtoList,
int leftReviewCounts) {
this.reviewResponseDtoList = reviewResponseDtoList;
this.leftReviewCounts = leftReviewCounts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tavebalak.OTTify.user.entity.User;

public interface ReviewRepository extends JpaRepository<Review, Long> {

Slice<Review> findByUserIdOrderByCreatedAt(Long userId, Pageable pageable);

boolean existsByProgramAndUser(Program program, User user);
Expand All @@ -36,7 +37,7 @@ Slice<Review> findUserSpecificByProgramAndGenre(@Param("program") Program progra
@Param("genre") String genre, Pageable pageable);

@Query("select count(r) from Review r where r.genre=:genre and r.program=:program")
long countByMyGenreName(@Param("genre") String genre, @Param("program") Program program);
int countByGenreName(@Param("genre") String genre, @Param("program") Program program);

@Query("select sum(r.rating) from Review r where r.genre=:genre and r.program=:program")
Double sumReviewRatingByGenreName(@Param("genre") String genreName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import tavebalak.OTTify.review.dto.reviewresponse.FourReviewResponseWithCounts;
import tavebalak.OTTify.review.dto.reviewresponse.ReviewProgramResponseDto;
import tavebalak.OTTify.review.dto.reviewresponse.ReviewResponseDtoList;
import tavebalak.OTTify.user.entity.User;

public interface ReviewShowProgramDetailService {

ReviewProgramResponseDto showMyReview(User user, Long programId);

ReviewResponseDtoList show4Review(Long programId);
FourReviewResponseWithCounts show4Review(Long programId);

Slice<ReviewProgramResponseDto> showReviewList(Long programId, Pageable pageable);

ReviewResponseDtoList show4UserSpecificReviewList(User user, Long programId);
FourReviewResponseWithCounts show4UserSpecificReviewList(User user, Long programId);

Slice<ReviewProgramResponseDto> showUserSpecificReviewList(User user, Long programId,
Pageable pageable);

String showUserSpecificRating(User user, Long programId);


}
Loading

0 comments on commit 2c5c40f

Please sign in to comment.