Skip to content

Commit

Permalink
findById 작성하기
Browse files Browse the repository at this point in the history
Related to: #37
  • Loading branch information
ahyoon99 committed Aug 14, 2024
1 parent a7be8a8 commit de02bb9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T extends MemoryDbEntity> implements MemoryDbRepositoryIfs<T>{
private JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource){
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
public Optional<T> findById(int index) {
String findByIdQuery = "select * from restaurant where index=? limit 1";
int findByIdParam = index;

WishListEntity wishListEntity = jdbcTemplate.queryForObject(findByIdQuery,
wishListEntityRowMapper(),
index);

return (Optional<T>) Optional.of(wishListEntity);
}

@Override
public T save(T entity) {
return null;
}

@Override
public void deleteById(int index) {

}

@Override
public List<T> listAll() {
return null;
}

private RowMapper<WishListEntity> 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;
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -96,4 +97,12 @@ public List<WishListDto> findAll() {
public void delete(int index) {
wishListRepository.deleteById(index);
}

public WishListDto findById(int index) {
Optional<WishListEntity> wishListEntity = wishListRepository.findById(index);
if(!wishListEntity.isPresent()){ // wishListEntity에 값이 없으면 Exception 처리하기
throw new IllegalArgumentException();
}
return entityToDto(wishListEntity.get()); // wishListEntity에 값이 있으면 wishListDto로 변환하여 리턴하기
}
}

0 comments on commit de02bb9

Please sign in to comment.