From de02bb9c0d92e28545a8f2a62ed67d50f3cc5586 Mon Sep 17 00:00:00 2001 From: ahyoon99 Date: Wed, 14 Aug 2024 10:10:45 +0900 Subject: [PATCH] =?UTF-8?q?findById=20=EC=9E=91=EC=84=B1=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #37 --- .../controller/ApiController.java | 5 ++ .../db/H2DbRepositoryAbstract.java | 67 +++++++++++++++++++ .../wishlist/service/WishListService.java | 9 +++ 3 files changed, 81 insertions(+) create mode 100644 src/main/java/com/example/naverrestaurant/db/H2DbRepositoryAbstract.java diff --git a/src/main/java/com/example/naverrestaurant/controller/ApiController.java b/src/main/java/com/example/naverrestaurant/controller/ApiController.java index 66bde51..45a81d2 100644 --- a/src/main/java/com/example/naverrestaurant/controller/ApiController.java +++ b/src/main/java/com/example/naverrestaurant/controller/ApiController.java @@ -16,6 +16,11 @@ public class ApiController { private final WishListService wishListService; + @GetMapping("/findById") + public WishListDto findById(@RequestParam int index){ + return wishListService.findById(index); + } + @GetMapping("/search") public WishListDto search(@RequestParam String query){ return wishListService.search(query); diff --git a/src/main/java/com/example/naverrestaurant/db/H2DbRepositoryAbstract.java b/src/main/java/com/example/naverrestaurant/db/H2DbRepositoryAbstract.java new file mode 100644 index 0000000..8c8b1ab --- /dev/null +++ b/src/main/java/com/example/naverrestaurant/db/H2DbRepositoryAbstract.java @@ -0,0 +1,67 @@ +package com.example.naverrestaurant.db; + +import com.example.naverrestaurant.wishlist.entity.WishListEntity; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; + +import javax.sql.DataSource; +import java.util.List; +import java.util.Optional; + + + +abstract public class H2DbRepositoryAbstract implements MemoryDbRepositoryIfs{ + private JdbcTemplate jdbcTemplate; + + @Autowired + public void setDataSource(DataSource dataSource){ + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + @Override + public Optional findById(int index) { + String findByIdQuery = "select * from restaurant where index=? limit 1"; + int findByIdParam = index; + + WishListEntity wishListEntity = jdbcTemplate.queryForObject(findByIdQuery, + wishListEntityRowMapper(), + index); + + return (Optional) Optional.of(wishListEntity); + } + + @Override + public T save(T entity) { + return null; + } + + @Override + public void deleteById(int index) { + + } + + @Override + public List listAll() { + return null; + } + + private RowMapper wishListEntityRowMapper(){ + return ((rs, rowNum) -> { + WishListEntity wishListEntity = new WishListEntity(); + wishListEntity.setIndex(rs.getInt("index")); + wishListEntity.setTitle(rs.getString("title")); + wishListEntity.setCategory(rs.getString("category")); + wishListEntity.setAddress(rs.getString("address")); + wishListEntity.setRoadAddress(rs.getString("roadAddress")); + wishListEntity.setHomePageLink(rs.getString("homePageLink")); + wishListEntity.setImageLink(rs.getString("imageLink")); + wishListEntity.setVisit(rs.getBoolean("isVisit")); + wishListEntity.setVisitCount(rs.getInt("visitCount")); + wishListEntity.setLastVisitDate(rs.getTimestamp("lastVisitDate").toLocalDateTime()); + + return wishListEntity; + }); + } + +} 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 14ad9ef..0908cce 100644 --- a/src/main/java/com/example/naverrestaurant/wishlist/service/WishListService.java +++ b/src/main/java/com/example/naverrestaurant/wishlist/service/WishListService.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -96,4 +97,12 @@ public List findAll() { public void delete(int index) { wishListRepository.deleteById(index); } + + public WishListDto findById(int index) { + Optional wishListEntity = wishListRepository.findById(index); + if(!wishListEntity.isPresent()){ // wishListEntity에 값이 없으면 Exception 처리하기 + throw new IllegalArgumentException(); + } + return entityToDto(wishListEntity.get()); // wishListEntity에 값이 있으면 wishListDto로 변환하여 리턴하기 + } }