Skip to content

Commit

Permalink
Merge pull request #52 from moonwonki/master
Browse files Browse the repository at this point in the history
버그 수정
  • Loading branch information
moonwonki authored May 11, 2024
2 parents e474417 + 9fd223a commit 87d008e
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/com/example/WebOrder/dto/ItemDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class MenuStatisticsDto {

//가장 별점이 좋은 메뉴
private String bestRateMenu;
private Integer bestRateNum;
private Double bestRateNum;


//가장 많이 주문된 메뉴
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/WebOrder/entity/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Item {

@Max(value = 5)
@Min(value = 0)
private Integer avgRate;
private Double avgRate;

private Long orderedCount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
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 ReviewRepository extends JpaRepository<Review, Long> {
Page<Review> 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);

}
4 changes: 2 additions & 2 deletions src/main/java/com/example/WebOrder/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
// 주문 생성
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/example/WebOrder/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReviewDto> getReviewsOfItem(Long itemId, Pageable pageable){
Expand Down Expand Up @@ -125,4 +119,9 @@ public List<ItemDto> 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);
}
}

0 comments on commit 87d008e

Please sign in to comment.