Skip to content

Commit

Permalink
Merge pull request #49 from ahyoon99/dev/feat/setStarRating/#35
Browse files Browse the repository at this point in the history
FEAT: " Add 식당에 별점 매기기"
  • Loading branch information
ahyoon99 authored Sep 2, 2024
2 parents 96b23ab + 840573c commit bce12e8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ 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) " +
"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);

Expand Down Expand Up @@ -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<WishListEntity> wishListEntityRowMapper(){
return ((rs, rowNum) -> {
WishListEntity wishListEntity = new WishListEntity();
Expand All @@ -91,6 +96,7 @@ private RowMapper<WishListEntity> wishListEntityRowMapper(){
if(rs.getTimestamp("lastVisitDate") != null){ // lastVisitDate가 null이 아닐 때만 set 해주기
wishListEntity.setLastVisitDate(rs.getTimestamp("lastVisitDate").toLocalDateTime());
}
wishListEntity.setStarRating((rs.getInt("starRating")));
return wishListEntity;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public class WishListDto {
private boolean isVisit; // 방문 여부
private int visitCount; // 방문 횟수
private LocalDateTime lastVisitDate; // 마지막 방문 날짜
private int starRating; // 별점
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public class WishListEntity extends MemoryDbEntity {
private boolean isVisit; // 방문 여부
private int visitCount; // 방문 횟수
private LocalDateTime lastVisitDate; // 마지막 방문 날짜
private int starRating; // 별점
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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());
}
}

}
4 changes: 2 additions & 2 deletions src/main/resources/h2/data.sql
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 2 additions & 1 deletion src/main/resources/h2/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ CREATE TABLE RESTAURANT
imageLink VARCHAR(255), -- 음식, 가게 이미지 주소
isVisit BOOLEAN NOT NULL, -- 방문 여부
visitCount INTEGER NOT NULL, -- 방문 횟수
lastVisitDate TIMESTAMP -- 마지막 방문 날짜
lastVisitDate TIMESTAMP, -- 마지막 방문 날짜
starRating INTEGER -- 별점
);

0 comments on commit bce12e8

Please sign in to comment.