Skip to content

Commit

Permalink
Merge pull request #54 from Zatch-Team/Feature/ViewNearZatch
Browse files Browse the repository at this point in the history
feat: 인기있는 재치 조회 api 및 응답 값 수정 #52
  • Loading branch information
plum-king authored May 21, 2023
2 parents f3e9b73 + 881f8ca commit 6fbcc72
Showing 1 changed file with 14 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public Long register(Zatch zatch) {
public List<ViewMyZatch> getZatchName(Long userId) {
try {
List<ViewMyZatch> results = jdbcTemplate.query(
"SELECT item_name from zatch where user_id = ?",
"SELECT md_name from zatch where user_id = ?",
new RowMapper<ViewMyZatch>() {
@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);
Expand All @@ -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);
Expand All @@ -88,42 +89,33 @@ 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<Map<String, Object>> 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<String> 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<Map<String, Object>> 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);
}

@Override
public List<ExchangeSearch> viewAll(String itemName1, String itemName2) {
try {
List<ExchangeSearch> 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<ExchangeSearch>() {
@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")
);
Expand All @@ -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");
}
}
}
Expand Down

0 comments on commit 6fbcc72

Please sign in to comment.