백앤드 수정
서버에 like_amount 추가하기
public class Review extends Feed {
public int user_id;
public int restaurant_id;
public Restaurant restaurant;
public double like_amount;
@Override public boolean hasMedia() {
return pictures != null && pictures.size() > 0;
}
}
서버에 좋아요 카운팅하는 쿼리 추가
@Mapper
public interface LikeMapper {
@Insert("INSERT INTO `like`(user_id, review_id) VALUES (#{user_id}, #{review_id})")
@Options(useGeneratedKeys = true, keyProperty = "like_id")
void addLike(@RequestBody Like like);
@Update("UPDATE review SET like_amount = like_amount+1 where review_id = #{review_id}")
void addLikeamount(@Param("review_id") int review_id);
@Delete("DELETE FROM `like` WHERE like_id = #{like_id}")
void deleteLike(@RequestBody Like like);
@Update("UPDATE review SET like_amount = like_amount-1 where review_id = #{review_id}")
void subtractLikeamount(@Param("review_id") int review_id);
@Select("SELECT * from `like` where like_id = #{like_id}")
Like getLike(@Param("like_id") int like_id);
}
컨트롤러에서 좋아요시 카운팅 처리 추가
@CrossOrigin("*")
@RequestMapping("/addLike")
@ResponseBody
public Like addLike(@RequestBody Like like) {
likeMapper.addLike(like);
Alarm alarm = new Alarm();
Review review = userMapper.getReviewsByReviewId(like.review_id);
alarm.other_user_id = like.user_id;
alarm.user_id = review.user_id;
alarm.alarm_type = AlarmType.LIKE;
alarm.review_id = like.review_id;
//리뷰 카운트 추가하기
likeMapper.addLikeamount(like.review_id);
User user = userMapper.getUserByUserId(like.user_id);
String contents = review.contents.length() > 10 ? review.contents.substring(0, 10) + ".." : review.contents;
alarm.contents = String.format("%s님이 \"%s\" 게시물에 좋아요를 눌렀습니다.", user.user_name, contents);
userMapper.addAlarm(alarm);
return like;
}
@CrossOrigin("*")
@RequestMapping("/deleteLike")
@ResponseBody
public Like deleteLike(@RequestBody Like like) {
Like like1 = likeMapper.getLike(like.like_id);
likeMapper.deleteLike(like);
//리뷰 카운트 빼기
likeMapper.subtractLikeamount(like1.review_id);
return like;
}