Skip to content

Commit

Permalink
save() 수정하기
Browse files Browse the repository at this point in the history
- 저장할 entity의 index가 null이라면, entity를 insert 하기
- 저장할 entity의 index가 null이 아니라면, index에 해당하는 entity의 정보를 수정(update)하기

Related to: #37
  • Loading branch information
ahyoon99 committed Sep 3, 2024
1 parent ba41fda commit 72eb4e4
Showing 1 changed file with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,32 @@ public Optional<T> findById(int index) {

@Override
public T save(T entity) {
String saveQuery = "insert into " +
"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.getStarRating()};

jdbcTemplate.update(saveQuery, saveParams);

String lastInsertIndexQuery = "select max(index) from restaurant";
int listInsertIndex = jdbcTemplate.queryForObject(lastInsertIndexQuery, int.class);

String lastInsertEntityQuery = "select * from restaurant where index=?";
WishListEntity result = jdbcTemplate.queryForObject(lastInsertEntityQuery, wishListEntityRowMapper(), listInsertIndex);
return (T) result;
if(entity.getIndex() == null){
String saveQuery = "insert into " +
"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.getStarRating()};

jdbcTemplate.update(saveQuery, saveParams);

String lastInsertIndexQuery = "select max(index) from restaurant";
int listInsertIndex = jdbcTemplate.queryForObject(lastInsertIndexQuery, int.class);

String lastInsertEntityQuery = "select * from restaurant where index=?";
WishListEntity result = jdbcTemplate.queryForObject(lastInsertEntityQuery, wishListEntityRowMapper(), listInsertIndex);
return (T) result;
}
else{
updateById(entity.getIndex(), entity);
String updateEntityQuery = "select * from restaurant where index=?";
WishListEntity result = jdbcTemplate.queryForObject(updateEntityQuery, wishListEntityRowMapper(), entity.getIndex());
return (T) result;
}
}

@Override
Expand Down

0 comments on commit 72eb4e4

Please sign in to comment.