diff --git a/src/main/java/com/example/WebOrder/controller/ReviewController.java b/src/main/java/com/example/WebOrder/controller/ReviewController.java index 0515f3b24e..06da5248e3 100644 --- a/src/main/java/com/example/WebOrder/controller/ReviewController.java +++ b/src/main/java/com/example/WebOrder/controller/ReviewController.java @@ -59,7 +59,12 @@ public String getReviewMenuPage(HttpServletRequest request, Model model, @PathVa @GetMapping("/review/write/{userId}/{itemId}") public String getReviewWriteForm(@PathVariable("userId") Long userId, @PathVariable("itemId") Long itemId,@RequestParam(name="page", defaultValue = "1") Integer page, Model model){ model.addAttribute("item", itemService.getItemInfoById(itemId)); - model.addAttribute("totalPage", reviewService.getNumberOfPages(itemId)); + Integer totalPages = reviewService.getNumberOfPages(itemId); + if (totalPages == 0) + model.addAttribute("totalPage", 1); + else + model.addAttribute("totalPage", totalPages); + model.addAttribute("reviews", reviewService.getReviewsOfItem(itemId, PageRequest.of(page - 1, 10, Sort.Direction.DESC, "id"))); return "review/reviewWrite"; } diff --git a/src/main/java/com/example/WebOrder/dto/ItemDto.java b/src/main/java/com/example/WebOrder/dto/ItemDto.java index 329182d146..edb1a37359 100644 --- a/src/main/java/com/example/WebOrder/dto/ItemDto.java +++ b/src/main/java/com/example/WebOrder/dto/ItemDto.java @@ -20,7 +20,7 @@ public class ItemDto { private String itemImageUrl; private int price; private Long adminId; - private Integer avgRate; + private Double avgRate; private Long orderedCount; private Long categoryId; private ItemStatus status; @@ -33,17 +33,7 @@ public static ItemDto fromEntity(Item item){ dto.setDescription(item.getDescription()); dto.setItemImageUrl(item.getItemImageUrl()); dto.setAdminId(item.getAdminId()); - - if (item.getReviews().size() != 0){ - int avgOfRate = 0; - for (Review incReview : item.getReviews()){ - avgOfRate += incReview.getRate(); - } - avgOfRate /= item.getReviews().size(); - dto.setAvgRate(avgOfRate); - } - else - dto.setAvgRate(0); + dto.setAvgRate(item.getAvgRate()); dto.setOrderedCount(item.getOrderedCount()); diff --git a/src/main/java/com/example/WebOrder/dto/MenuStatisticsDto.java b/src/main/java/com/example/WebOrder/dto/MenuStatisticsDto.java index 1c7eac573e..d7e7af56ed 100644 --- a/src/main/java/com/example/WebOrder/dto/MenuStatisticsDto.java +++ b/src/main/java/com/example/WebOrder/dto/MenuStatisticsDto.java @@ -12,7 +12,7 @@ public class MenuStatisticsDto { //가장 별점이 좋은 메뉴 private String bestRateMenu; - private Integer bestRateNum; + private Double bestRateNum; //가장 많이 주문된 메뉴 diff --git a/src/main/java/com/example/WebOrder/entity/Item.java b/src/main/java/com/example/WebOrder/entity/Item.java index 2a84065847..7135f1860f 100644 --- a/src/main/java/com/example/WebOrder/entity/Item.java +++ b/src/main/java/com/example/WebOrder/entity/Item.java @@ -41,7 +41,7 @@ public class Item { @Max(value = 5) @Min(value = 0) - private Integer avgRate; + private Double avgRate; private Long orderedCount; diff --git a/src/main/java/com/example/WebOrder/repository/ReviewRepository.java b/src/main/java/com/example/WebOrder/repository/ReviewRepository.java index 3200402a11..5c3101207e 100644 --- a/src/main/java/com/example/WebOrder/repository/ReviewRepository.java +++ b/src/main/java/com/example/WebOrder/repository/ReviewRepository.java @@ -4,6 +4,8 @@ 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; @@ -11,5 +13,7 @@ @Repository public interface ReviewRepository extends JpaRepository { Page findByItemId(Long itemId, Pageable pageable); + @Query("SELECT ROUND(AVG(r.rate), 1) FROM Review r WHERE r.item.id = :itemId") + Double findAverageRateByItemId(@Param("itemId") Long itemId); } diff --git a/src/main/java/com/example/WebOrder/service/ItemService.java b/src/main/java/com/example/WebOrder/service/ItemService.java index 3af82666fb..5acd27d3bc 100644 --- a/src/main/java/com/example/WebOrder/service/ItemService.java +++ b/src/main/java/com/example/WebOrder/service/ItemService.java @@ -58,7 +58,7 @@ public Long createItem(Long adminId, ItemDto dto, String fileName) { item.setDescription(dto.getDescription()); item.setAdminId(adminId); item.setReviews(new ArrayList<>()); - item.setAvgRate(0); + item.setAvgRate(0.0); item.setOrderedCount(0L); log.info(String.valueOf(dto.getCategoryId())); Category category = categoryRepository.findById(dto.getCategoryId()).get(); @@ -104,7 +104,7 @@ public MenuStatisticsDto getBestItemStat(Long userId) { MenuStatisticsDto dto = new MenuStatisticsDto(); if (avgRateEntity.isEmpty()) { dto.setBestRateMenu("메뉴 없음"); - dto.setBestRateNum(0); + dto.setBestRateNum(0.0); } else { dto.setBestRateMenu(avgRateEntity.get().getName()); diff --git a/src/main/java/com/example/WebOrder/service/OrderService.java b/src/main/java/com/example/WebOrder/service/OrderService.java index 74cf56748a..ba13281f39 100644 --- a/src/main/java/com/example/WebOrder/service/OrderService.java +++ b/src/main/java/com/example/WebOrder/service/OrderService.java @@ -53,6 +53,7 @@ public Long order(Long seatId, String json) throws JsonProcessingException { if (dto.getCount() > 0) { Item item = itemRepository.findById(dto.getItemId()).get(); orderItems.add(OrderItem.createOrderItem(item, dto.getCount())); + item.setOrderedCount(item.getOrderedCount() + dto.getCount()); } } // 주문 생성 diff --git a/src/main/java/com/example/WebOrder/service/ReviewService.java b/src/main/java/com/example/WebOrder/service/ReviewService.java index 8d0ea6aaaf..f36e83c2e5 100644 --- a/src/main/java/com/example/WebOrder/service/ReviewService.java +++ b/src/main/java/com/example/WebOrder/service/ReviewService.java @@ -43,20 +43,14 @@ public Long createReview(ReviewDto dto){ review.setRate(dto.getRate()); review.setComment(dto.getComment()); - reviewedItem.getReviews().add(review); + review = reviewRepository.save(review); - int avgOfRate = 0; - for (Review incReview : reviewedItem.getReviews()){ - avgOfRate += incReview.getRate(); - } - avgOfRate /= reviewedItem.getReviews().size(); - reviewedItem.setAvgRate(avgOfRate); + reviewedItem.setAvgRate(reviewRepository.findAverageRateByItemId(dto.getItemId())); - Long returnValue = reviewRepository.save(review).getId(); itemRepository.save(reviewedItem); - return returnValue; + return review.getId(); } public List getReviewsOfItem(Long itemId, Pageable pageable){ @@ -125,4 +119,9 @@ public List getItemDtoListFromCookieString(Long userId, String cookieSt public Integer getNumberOfPages(Long itemId) { return reviewRepository.findByItemId(itemId, Pageable.ofSize(10)).getTotalPages(); } + + // 소수점 아래 1자리만 보이게 한다. + public Double getAverageRateOfItem(Long itemId){ + return reviewRepository.findAverageRateByItemId(itemId); + } }