From 840573c8728d86f460d3ff64963df3101e411109 Mon Sep 17 00:00:00 2001 From: ahyoon99 Date: Thu, 29 Aug 2024 11:19:33 +0900 Subject: [PATCH] =?UTF-8?q?FEAT:=20"=20Add=20=EC=8B=9D=EB=8B=B9=EC=97=90?= =?UTF-8?q?=20=EB=B3=84=EC=A0=90=20=EB=A7=A4=EA=B8=B0=EA=B8=B0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 위시리스트에 있는 식당 별점 매기기 Related to: #35 --- .../naverrestaurant/controller/ApiController.java | 5 +++++ .../naverrestaurant/db/H2DbRepositoryAbstract.java | 12 +++++++++--- .../naverrestaurant/wishlist/dto/WishListDto.java | 1 + .../wishlist/entity/WishListEntity.java | 1 + .../wishlist/service/WishListService.java | 14 ++++++++++++++ src/main/resources/h2/data.sql | 4 ++-- src/main/resources/h2/schema.sql | 3 ++- 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/naverrestaurant/controller/ApiController.java b/src/main/java/com/example/naverrestaurant/controller/ApiController.java index 0624fe7..61866e6 100644 --- a/src/main/java/com/example/naverrestaurant/controller/ApiController.java +++ b/src/main/java/com/example/naverrestaurant/controller/ApiController.java @@ -46,4 +46,9 @@ public void delete(@PathVariable int index){ public void addVisit(@PathVariable int index){ wishListService.addVisit(index); } + + @PatchMapping("/{index}") + public void setStarRating(@PathVariable int index, @RequestParam int starRating){ + wishListService.setStarRating(index, starRating); + } } diff --git a/src/main/java/com/example/naverrestaurant/db/H2DbRepositoryAbstract.java b/src/main/java/com/example/naverrestaurant/db/H2DbRepositoryAbstract.java index dda561f..cc37c34 100644 --- a/src/main/java/com/example/naverrestaurant/db/H2DbRepositoryAbstract.java +++ b/src/main/java/com/example/naverrestaurant/db/H2DbRepositoryAbstract.java @@ -34,14 +34,14 @@ public Optional findById(int index) { @Override public T save(T entity) { String saveQuery = "insert into " + - "restaurant(title, category, address, roadAddress, homepageLink, imageLink, isvisit, visitCount, lastVisitDate) " + - "values (?,?,?,?,?,?,?,?,?)"; + "restaurant(title, category, address, roadAddress, homepageLink, imageLink, isvisit, visitCount, lastVisitDate, starRating) " + + "values (?,?,?,?,?,?,?,?,?,?)"; WishListEntity wishListEntity = (WishListEntity) entity; Object []saveParams = new Object[] {wishListEntity.getTitle(), wishListEntity.getCategory(), wishListEntity.getAddress(), wishListEntity.getRoadAddress(), wishListEntity.getHomePageLink(), wishListEntity.getImageLink(), wishListEntity.isVisit(), - wishListEntity.getVisitCount(), wishListEntity.getLastVisitDate() }; + wishListEntity.getVisitCount(), wishListEntity.getLastVisitDate(), wishListEntity.getStarRating()}; jdbcTemplate.update(saveQuery, saveParams); @@ -76,6 +76,11 @@ public void updateById(int index, T entity) { jdbcTemplate.update(updateByIdQuery, wishListEntity.isVisit(), wishListEntity.getVisitCount(), index); } + public void updateStarRatingById(int index, int starRating){ + String updateStarRatingByIdQuery = "update restaurant set starRating = ? where index = ?"; + jdbcTemplate.update(updateStarRatingByIdQuery, starRating, index); + } + private RowMapper wishListEntityRowMapper(){ return ((rs, rowNum) -> { WishListEntity wishListEntity = new WishListEntity(); @@ -91,6 +96,7 @@ private RowMapper wishListEntityRowMapper(){ if(rs.getTimestamp("lastVisitDate") != null){ // lastVisitDate가 null이 아닐 때만 set 해주기 wishListEntity.setLastVisitDate(rs.getTimestamp("lastVisitDate").toLocalDateTime()); } + wishListEntity.setStarRating((rs.getInt("starRating"))); return wishListEntity; }); } diff --git a/src/main/java/com/example/naverrestaurant/wishlist/dto/WishListDto.java b/src/main/java/com/example/naverrestaurant/wishlist/dto/WishListDto.java index 9d95e19..3c47047 100644 --- a/src/main/java/com/example/naverrestaurant/wishlist/dto/WishListDto.java +++ b/src/main/java/com/example/naverrestaurant/wishlist/dto/WishListDto.java @@ -20,4 +20,5 @@ public class WishListDto { private boolean isVisit; // 방문 여부 private int visitCount; // 방문 횟수 private LocalDateTime lastVisitDate; // 마지막 방문 날짜 + private int starRating; // 별점 } diff --git a/src/main/java/com/example/naverrestaurant/wishlist/entity/WishListEntity.java b/src/main/java/com/example/naverrestaurant/wishlist/entity/WishListEntity.java index 751e27a..894076a 100644 --- a/src/main/java/com/example/naverrestaurant/wishlist/entity/WishListEntity.java +++ b/src/main/java/com/example/naverrestaurant/wishlist/entity/WishListEntity.java @@ -22,4 +22,5 @@ public class WishListEntity extends MemoryDbEntity { private boolean isVisit; // 방문 여부 private int visitCount; // 방문 횟수 private LocalDateTime lastVisitDate; // 마지막 방문 날짜 + private int starRating; // 별점 } diff --git a/src/main/java/com/example/naverrestaurant/wishlist/service/WishListService.java b/src/main/java/com/example/naverrestaurant/wishlist/service/WishListService.java index 089c448..f521439 100644 --- a/src/main/java/com/example/naverrestaurant/wishlist/service/WishListService.java +++ b/src/main/java/com/example/naverrestaurant/wishlist/service/WishListService.java @@ -69,6 +69,7 @@ private WishListEntity dtoToEntity(WishListDto wishListDto) { entity.setVisit(wishListDto.isVisit()); entity.setVisitCount(wishListDto.getVisitCount()); entity.setLastVisitDate(wishListDto.getLastVisitDate()); + entity.setStarRating(wishListDto.getStarRating()); return entity; } @@ -84,6 +85,7 @@ private WishListDto entityToDto(WishListEntity wishListEntity) { dto.setVisit(wishListEntity.isVisit()); dto.setVisitCount(wishListEntity.getVisitCount()); dto.setLastVisitDate(wishListEntity.getLastVisitDate()); + dto.setStarRating(wishListEntity.getStarRating()); return dto; } @@ -115,5 +117,17 @@ public void addVisit(int index) { wishListRepository.updateById(index, restaurnatEntity); } } + + public void setStarRating(int index, int starRating){ + var restaurant = wishListRepository.findById(index); + if(restaurant.isPresent()){ + var restaurantEntity = restaurant.get(); + restaurantEntity.setStarRating(starRating); + wishListRepository.updateStarRatingById(index, starRating); + + var result = wishListRepository.findById(index); + System.out.println(result.toString()); + } + } } diff --git a/src/main/resources/h2/data.sql b/src/main/resources/h2/data.sql index df885b1..be777be 100644 --- a/src/main/resources/h2/data.sql +++ b/src/main/resources/h2/data.sql @@ -1,2 +1,2 @@ -insert into RESTAURANT(title, category, address, roadAddress, homepageLink, imageLink, isVisit, visitCount, lastVisitDate) VALUES ('전주 마라탕2', '중식', '전주시 만성동1', '만성로1', 'www.maratang.com', 'www.pic1.jpeg',FALSE,0,'2023-08-10 11:28:36.026095'); -insert into RESTAURANT(title, category, address, roadAddress, homepageLink, imageLink, isVisit, visitCount, lastVisitDate) VALUES ('만성 찜닭2', '한식', '전주시 만성동2', '만성로2', 'www.zzimdak.com', 'www.pic2.jpeg',TRUE,0,'2024-08-12 11:28:36.026095'); +insert into RESTAURANT(title, category, address, roadAddress, homepageLink, imageLink, isVisit, visitCount, lastVisitDate, starRating) VALUES ('전주 마라탕2', '중식', '전주시 만성동1', '만성로1', 'www.maratang.com', 'www.pic1.jpeg',FALSE,0,'2023-08-10 11:28:36.026095',0); +insert into RESTAURANT(title, category, address, roadAddress, homepageLink, imageLink, isVisit, visitCount, lastVisitDate, starRating) VALUES ('만성 찜닭2', '한식', '전주시 만성동2', '만성로2', 'www.zzimdak.com', 'www.pic2.jpeg',TRUE,1,'2024-08-12 11:28:36.026095',3); diff --git a/src/main/resources/h2/schema.sql b/src/main/resources/h2/schema.sql index 7b8e6fc..7c185e9 100644 --- a/src/main/resources/h2/schema.sql +++ b/src/main/resources/h2/schema.sql @@ -12,5 +12,6 @@ CREATE TABLE RESTAURANT imageLink VARCHAR(255), -- 음식, 가게 이미지 주소 isVisit BOOLEAN NOT NULL, -- 방문 여부 visitCount INTEGER NOT NULL, -- 방문 횟수 - lastVisitDate TIMESTAMP -- 마지막 방문 날짜 + lastVisitDate TIMESTAMP, -- 마지막 방문 날짜 + starRating INTEGER -- 별점 ); \ No newline at end of file