diff --git a/src/main/java/com/zatch/zatchserver/repository/ZatchRepositoryImpl.java b/src/main/java/com/zatch/zatchserver/repository/ZatchRepositoryImpl.java index 7131a97..6e166f1 100644 --- a/src/main/java/com/zatch/zatchserver/repository/ZatchRepositoryImpl.java +++ b/src/main/java/com/zatch/zatchserver/repository/ZatchRepositoryImpl.java @@ -60,12 +60,12 @@ public Long register(Zatch zatch) { public List getZatchName(Long userId) { try { List results = jdbcTemplate.query( - "SELECT item_name from zatch where user_id = ?", + "SELECT md_name from zatch where user_id = ?", new RowMapper() { @Override public ViewMyZatch mapRow(ResultSet rs, int rowNum) throws SQLException { ViewMyZatch myZatch = new ViewMyZatch( - rs.getString("item_name")); + rs.getString("md_name")); return myZatch; } }, userId); @@ -79,7 +79,8 @@ public Integer increaseLike(Long userId, Long zatchId) { String increaseLikeQuery = "insert into zatch_like(user_id, zatch_id) values(?,?)"; - jdbcTemplate.update(increaseLikeQuery, new Object[] {userId, zatchId}); + jdbcTemplate.update(increaseLikeQuery, new Object[]{userId, zatchId}); + jdbcTemplate.update("update zatch set like_count = like_count + 1 where zatch_id = ?", zatchId); String getZatchLikeCountQuery = "select count(zatch_like_id) from zatch_like where zatch_id = ?"; return jdbcTemplate.queryForObject(getZatchLikeCountQuery, Integer.class, zatchId); @@ -88,27 +89,18 @@ public Integer increaseLike(Long userId, Long zatchId) { public Integer decreaseLike(Long userId, Long zatchId) { String decreaseLikeQuery = "delete from zatch_like where user_id = ? and zatch_id = ?"; - jdbcTemplate.update(decreaseLikeQuery, new Object[] {userId, zatchId}); + jdbcTemplate.update(decreaseLikeQuery, new Object[]{userId, zatchId}); + jdbcTemplate.update("update zatch set like_count = like_count - 1 where zatch_id = ?", zatchId); String getZatchLikeCountQuery = "select count(zatch_like_id) from zatch_like where zatch_id = ?"; return jdbcTemplate.queryForObject(getZatchLikeCountQuery, Integer.class, zatchId); } - //지금 인기있는 재치 item 3개 보여주기 - public List> showPopularZatch(){ - - String showPopularZatchQuery= - "SELECT item_name, count(*) as like_count, zatch.created_at FROM zatch_like left join zatch on zatch_like.zatch_id=zatch.zatch_id GROUP BY item_name ORDER BY like_count desc, zatch.created_at desc"; - -// List itemName = null; -// for(int i=0;i<3;i++){ -// //System.out.println("@@@@@@@@@ showPopularZatch"); -// String name=String.valueOf(jdbcTemplate.queryForList(showPopularZatchQuery).get(i).get("item_name")); -// //System.out.println(name); -// //itemName.add(String.valueOf(jdbcTemplate.queryForList(showPopularZatchQuery).get(i).get("item_name"))); -// itemName.add(name); -// } - //return itemName; + //인기있는 재치 item 3개 보여주기 + public List> showPopularZatch() { + + String showPopularZatchQuery = + "SELECT md_name, count(*) as like_count, zatch.created_at FROM zatch_like left join zatch on zatch_like.zatch_id=zatch.zatch_id GROUP BY md_name ORDER BY like_count desc limit 3"; return jdbcTemplate.queryForList(showPopularZatchQuery); } @@ -116,14 +108,14 @@ public List> showPopularZatch(){ public List viewAll(String itemName1, String itemName2) { try { List results = jdbcTemplate.query( - "SELECT * from zatch where (item_name LIKE ? OR item_name LIKE ?)", + "SELECT * from zatch where (md_name LIKE ? OR md_name LIKE ?)", new RowMapper() { @Override public ExchangeSearch mapRow(ResultSet rs, int rowNum) throws SQLException { ExchangeSearch exchangeSearch = new ExchangeSearch( rs.getLong("user_id"), rs.getInt("is_free"), - rs.getString("item_name"), + rs.getString("md_name"), rs.getInt("allow_any_zatch"), rs.getInt("like_count") ); @@ -134,7 +126,7 @@ public ExchangeSearch mapRow(ResultSet rs, int rowNum) throws SQLException { } catch ( Exception e) { - throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Item Name Not Found"); + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "MD Name Not Found"); } } }