나와바리 - 리뷰 서비스

Sungmin·2023년 4월 20일
0
post-thumbnail

리뷰생성


리뷰생성 메서드는 는 회원, 식당, 사진, 제목, 내용, 평점으로 구성되어있다.

  • 리뷰는 회원이 설정한 구역의 주소와 식당의 주소중 '구'가 일치해야지 작성할 수 있게 만들었다. 만약 회원의 구역이 "서울특별시 강남구 논현동"이고 식당의 주소가 "서울특별시 강남구 역삼동"일 경우 은 다르지만 가 같으므로 리뷰를 작성할 수 있다.
  • 데이터를 받으면 리뷰를 생성해주는 메서드에 값을 넣음
  • 리뷰 수를 1개 증가시킴
  • 식당의 평균점수를 계산해 주는 메서드 실행
  • 식당과 리뷰정보를 DB에 저장하는 save메서드 실행

리뷰 수정

  • 리뷰아이디 값을 받아 DB에서 찾아옴
  • 들어온 받은 값을 set
  • 식당의 평점도 업데이트

리뷰 삭제

  • 리뷰와 식당의 아이디값을 받아옴
  • DB에서 같은 아이디를 찾아옴
  • 식당의 리뷰를 제거하는 메서드 실행
  • 식당의 리뷰수를 1개 감소함
  • 식당의 평점 업데이트

리뷰 생성 테스트

    @Test
    public void 리뷰생성() throws Exception {
        //given
        Member member = Member.builder()
                .profile_nickname("Kim")
                .build();

        Member member2 = Member.builder()
                .profile_nickname("Nam")
                .build();
        em.persist(member);
        em.persist(member2);

        Restaurant restaurant = Restaurant.builder()
                .name("Vips")
                .build();

        em.persist(restaurant);

        List<Photo> photos = new ArrayList<>();
        Photo photo1 = Photo.builder()
                .photo_url("www.photo1.com")
                .build();
        em.persist(photo1);
        Photo photo2 = Photo.builder()
                .photo_url("www.photo2.com")
                .build();
        em.persist(photo2);

        photos.add(photo1);
        photos.add(photo2);

        Double rate = 4.5;
        String title = "GOOD";
        String content = "Good dish";

        Double rate2 = 3.5;
        String title2 = "SoSo";
        String content2 = "SoSo dish";



        //when

        Long reviewId1 = reviewService.createReview(member.getId(), restaurant.getId(), photos, title, content, rate);
        Long reviewId2 = reviewService.createReview(member2.getId(), restaurant.getId(), photos, title2, content2, rate2);

        Review review = reviewRepository.findOne(reviewId1);

        Restaurant restaurant1 = restaurantRepository.findOne(restaurant.getId());
        //then
        assertThat(review.getWriter()).isEqualTo(member);
        assertThat(review.getRestaurant()).isEqualTo(restaurant);
        assertThat(review.getPhotos()).containsExactlyInAnyOrder(photo1, photo2);
        assertThat(review.getTitle()).isEqualTo(title);
        assertThat(review.getContent()).isEqualTo(content);
        assertThat(review.getRate()).isEqualTo(rate);
        assertThat(restaurant1.getReviewCount()).isEqualTo(2);
    }
  • 회원, 식당생성
  • 리뷰 생성메서드 실행하고 값을 넣어줌
  • 리뷰정보와 식당 정보를 DB에서 꺼내옴
  • 각각의 꺼내온 정보와 내가 넣은 값이 일치하는지, 평균계산과 리뷰수의 증가가 잘 되고있는지 assertThat으로 비교

리뷰 수정 테스트

    @Test
    public void 리뷰수정() throws Exception {
        //given
        Member member = Member.builder()
                .profile_nickname("Kim")
                .build();
        em.persist(member);

        Restaurant restaurant = Restaurant.builder()
                .name("Vips")
                .build();
        em.persist(restaurant);

        List<Photo> photos = new ArrayList<>();
        Photo photo1 = Photo.builder()
                .photo_url("www.photo1.com")
                .build();
        em.persist(photo1);
        Photo photo2 = Photo.builder()
                .photo_url("www.photo2.com")
                .build();
        em.persist(photo2);

        photos.add(photo1);
        photos.add(photo2);

        Double rate = 4.5;
        String title = "GOOD";
        String content = "Good dish";

        Long reviewId = reviewService.createReview(member.getId(), restaurant.getId(), photos, title, content, rate);
        //when
        Double newRate = 3.5;
        String newTitle = "BAD";
        String newContent = "Bad dish";

        reviewService.updateReview(reviewId, photos,newTitle, newContent, newRate);

        Review updateReview = reviewRepository.findOne(reviewId);

        //then
        assertThat(updateReview.getPhotos()).isEqualTo(photos);
        assertThat(updateReview.getTitle()).isEqualTo(newTitle);
        assertThat(updateReview.getContent()).isEqualTo(newContent);
        assertThat(updateReview.getRate()).isEqualTo(newRate);
    }
  • 회원과 식당을 만들고 리뷰생성 메서드로 리뷰를 만들어줌
  • 또 다른 회원을 만들고 리뷰생성 메서드를 만들어줌
  • 수정된 값이 잘 들어가는지 확인

리뷰 삭제 테스트

   @Test
    public void 리뷰삭제() throws Exception {
        //given
        Member member = Member.builder()
                .profile_nickname("Kim")
                .build();
        em.persist(member);

        Restaurant restaurant = Restaurant.builder()
                .name("Vips")
                .build();
        em.persist(restaurant);

        List<Photo> photos = new ArrayList<>();
        Photo photo1 = Photo.builder()
                .photo_url("www.photo1.com")
                .build();
        em.persist(photo1);
        Photo photo2 = Photo.builder()
                .photo_url("www.photo2.com")
                .build();
        em.persist(photo2);

        photos.add(photo1);
        photos.add(photo2);

        Double rate = 4.5;
        String title = "GOOD";
        String content = "Good dish";

        Long reviewId = reviewService.createReview(member.getId(), restaurant.getId(), photos, title, content, rate);

        //when

        reviewService.deleteReview(reviewId, restaurant.getId());

        //then

        assertThat(restaurant.getReviewCount()).isEqualTo(0);
        assertThat(restaurant.getAvgRating()).isNull();

    }
  • 회원과 식당을 만들어줌
  • 리뷰생성메서드로 리뷰를 생성
  • 리뷰제거 메서드 실행
  • 리뷰가 잘 제거 되었는지 리뷰수랑 비교하여 확인
  • 리뷰수가 0 이므로 리뷰평균이 null인지 확인
profile
Let's Coding

0개의 댓글