From 881f8ca164ddd0d96ac6a4aa9d62d12b514a0465 Mon Sep 17 00:00:00 2001 From: plum-king Date: Sun, 21 May 2023 12:31:03 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=B8=EA=B8=B0=EC=9E=88=EB=8A=94=20?= =?UTF-8?q?=EC=9E=AC=EC=B9=98=20=EC=A1=B0=ED=9A=8C=20api=20=EB=B0=8F=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=20=EA=B0=92=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ZatchRepositoryImpl.java | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) 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"); } } }