forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from kookmin-sw/리뷰_작성하기
리뷰 작성하기
- Loading branch information
Showing
10 changed files
with
418 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/example/WebOrder/controller/ReviewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.example.WebOrder.controller; | ||
|
||
import com.example.WebOrder.dto.ReviewDto; | ||
import com.example.WebOrder.service.ItemService; | ||
import com.example.WebOrder.service.ReviewService; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@Controller | ||
@Slf4j | ||
public class ReviewController { | ||
|
||
private final ReviewService reviewService; | ||
private final ItemService itemService; | ||
public ReviewController(ReviewService reviewService, ItemService itemService) { | ||
this.reviewService = reviewService; | ||
this.itemService = itemService; | ||
} | ||
|
||
|
||
// fetch를 통해 item의 review를 가져오는 컨트롤러 | ||
// requestParam으로 page가 있어야 한다. | ||
@ResponseBody | ||
@GetMapping("/review/get/{itemId}") | ||
public List<ReviewDto> getReviewOfItem( @PathVariable("itemId") Long itemId, @RequestParam(name="page", defaultValue = "1") Integer page){ | ||
Pageable pageable = PageRequest.of(page - 1, 10, Sort.Direction.DESC, "id"); | ||
|
||
List<ReviewDto> reviewsOfItem = reviewService.getReviewsOfItem(itemId, pageable); | ||
|
||
return reviewsOfItem; | ||
} | ||
|
||
// 쿠키를 분석해서 주문한 메뉴의 리스트를 보여주는 페이지 | ||
@GetMapping("/review/menu/{userId}") | ||
public String getReviewMenuPage(HttpServletRequest request, Model model, @PathVariable("userId") Long userId){ | ||
String orderItemsIdString = new String(); | ||
|
||
for (Cookie cookie : request.getCookies()){ | ||
if (cookie.getName().equals("orderItemIds")){ | ||
orderItemsIdString = cookie.getValue(); | ||
} | ||
} | ||
log.info("orderItemIds : " + orderItemsIdString); | ||
|
||
model.addAttribute("itemList", reviewService.getItemDtoListFromCookieString(userId, orderItemsIdString)); | ||
return "review/reviewMenu"; | ||
} | ||
|
||
// 리뷰 적는 form을 주는 페이지 | ||
@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)); | ||
model.addAttribute("reviews", reviewService.getReviewsOfItem(itemId, PageRequest.of(page - 1, 10, Sort.Direction.DESC, "id"))); | ||
return "review/reviewWrite"; | ||
} | ||
|
||
|
||
|
||
// 리뷰 적는 form을 제출 | ||
@PostMapping("/review/write/{userId}/{itemId}") | ||
public String writeReview(@PathVariable("userId") Long userId, @PathVariable("itemId") Long itemId, ReviewDto reviewDto){ | ||
reviewService.createReview(reviewDto); | ||
return "redirect:/review/write/" + userId + "/" + itemId; | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
src/main/java/com/example/WebOrder/repository/ReviewRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package com.example.WebOrder.repository; | ||
|
||
import com.example.WebOrder.entity.Item; | ||
import com.example.WebOrder.entity.Review; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface ReviewRepository extends JpaRepository<Review, Long> { | ||
List<Review> findAllByItemId(Long itemId); | ||
Page<Review> findByItemId(Long itemId, Pageable pageable); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.