@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class ReviewDTO {
private Long reviewNum;
//Movie mno
private Long mno;
//Member id
private Long mid;
//Member nickName
private String nickName;
//Member email
private String email;
private int grade;
private String text;
private LocalDateTime regDate,modDate;
}
ReviewDTO는 화면에 필요한 모든 내용을 가지고 있어야 하기 때문에 회원의 아이디/닉네임/이메일도 같이 처리할 수 있도록 설계합니다.
ReviewService는 기존의 서비스가 하는 작업과 동일합니다.
entityToDTO()와 dtoToEntity() 같은 메서드를 정의합니다.
-특정한 영화의 모든 리뷰를 가져오는 기능
-새로운 영화 리뷰를 등록하는 기능
-특정 영화 리뷰를 수정하는 기능
-특정 영화 리뷰를 삭제하는 기능
Review 엔티티 클래스에 리뷰 평점과 리뷰 내용을 수정할 수 있는 기능을 추가합니다.
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@ToString(exclude = {"movie","member"})
@Entity
public class Review extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long reviewNum;
@ManyToOne(fetch=FetchType.LAZY)
private Movie movie;
@ManyToOne(fetch=FetchType.LAZY)
private Member member;
private int grade;
private String text;
<----------------추가------------------->
public void changeGrade(int grade){
this.grade =grade;
}
public void changeText(String text){
this.text=text;
}
</----------------추가-------------------/>
}
public interface ReviewService {
//영화의 모든 리뷰를 가져옵니다
List<ReviewDTO> getListOfMovie(Long mno);
//영화에 리뷰를 추가합니다.
Long register(ReviewDTO movieReviewDTO);
//특정한 영화의 리뷰를 수정합니다.
void modify(ReviewDTO movieReviewDTO);
//특정한 영화의 리뷰를 삭제합니다.
void remove(Long reviewNum);
default Review dtoToEntity(ReviewDTO reviewDTO){
Review review = Review.builder()
.reviewNum(reviewDTO.getReviewNum())
.movie(Movie.builder().mno(reviewDTO.getMno()).build())
.member(Member.builder().mid(reviewDTO.getMid()).build())
.grade(reviewDTO.getGrade())
.text(reviewDTO.getText())
.build();
return review;
}
default ReviewDTO entityToDto(Review review){
ReviewDTO reviewDTO = ReviewDTO.builder()
.reviewNum(review.getReviewNum())
.mno(review.getMovie().getMno())
.mid(review.getMember().getMid())
.nickName(review.getMember().getNickName())
.email(review.getMember().getEmail())
.grade(review.getGrade())
.text(review.getText())
.regDate(review.getRegDate())
.modDate(review.getModDate())
.build();
return reviewDTO;
}
}
@Service
@RequiredArgsConstructor
@Log4j2
public class ReviewServiceImpl implements ReviewService{
private final ReviewRepository reviewRepository;
@Override
public List<ReviewDTO> getListOfMovie(Long mno) {
Movie movie = Movie.builder().mno(mno).build();
List<Review> result = reviewRepository.findByMovie(movie);
return result.stream().map(movieReview ->entityToDto(movieReview)).collect(Collectors.toList());
}
@Override
public Long register(ReviewDTO movieReviewDTO) {
Review review = dtoToEntity(movieReviewDTO);
Review result = reviewRepository.save(review);
return result.getReviewNum();
}
@Override
public void modify(ReviewDTO movieReviewDTO) {
Optional<Review> result = reviewRepository.findById(movieReviewDTO.getReviewNum());
if(result.isPresent()){
Review review = result.get();
review.changeGrade(movieReviewDTO.getGrade());
review.changeText(movieReviewDTO.getText());
}
}
@Override
public void remove(Long reviewNum) {
reviewRepository.deleteById(reviewNum);
}
@RestController
@RequestMapping("/reviews")
@Log4j2
@RequiredArgsConstructor
public class ReviewController {
private final ReviewService reviewService;
@GetMapping("/{mno}/all")// 결과데이터 : ReviewDTO 리스트, 해당영화의 모든 리뷰 반환
public ResponseEntity<List<ReviewDTO>>getList(@PathVariable("mno")Long mno){
log.info("-----------------------lis-----------------------");
log.info("mno: "+mno);
List<ReviewDTO> reviewDTOList = reviewService.getListOfMovie(mno);
return new ResponseEntity<>(reviewDTOList, HttpStatus.OK);
}
@PostMapping("/{mno}")// 결과데이터 : 생성된 리뷰 번호 , 새로운 리뷰등록
public ResponseEntity<Long> addReview (@RequestBody ReviewDTO reviewDTO){
log.info("<-----------------------add Review----------------------->");
log.info("reviewDTO : "+reviewDTO);
Long reviewNum = reviewService.register(reviewDTO);
return new ResponseEntity<>(reviewNum,HttpStatus.OK);
}
@PutMapping("/{mno}/{reviewNum}")// 결과데이터 : 리뷰의 수정 성공 여부, 리뷰수정
public ResponseEntity<Long>modifyReview (@PathVariable("mno")Long reviewNum,
@RequestBody ReviewDTO reviewDTO){
log.info("<-----------------------modify removeReview----------------------->");
log.info("reviewNum : "+reviewNum);
reviewService.remove(reviewNum);
return new ResponseEntity<>(reviewNum,HttpStatus.OK);
}
}
<div class="reviewModal modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Movie Review</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label >Reviewer ID</label>
<input type="text" class="form-control" name="mid" >
</div>
<div class="form-group">
<label >Grade <span class="grade"></span></label>
<div class='starrr'></div>
</div>
<div class="form-group">
<label >Review Text</label>
<input type="text" class="form-control" name="text" placeholder="Good Movie!" >
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary reviewSaveBtn">Save changes</button>
<button type="button" class="btn btn-warning modifyBtn">Modify </button>
<button type="button" class="btn btn-danger removeBtn">Remove </button>
</div>
</div>
</div>
</div>
<div class="imageModal modal " tabindex="-2" role="dialog">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Picture</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>