From ebecd3d6d02bca6db5cdbf9fb402f0e33ae73524 Mon Sep 17 00:00:00 2001 From: plum-king Date: Sat, 24 Jun 2023 21:32:03 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=A9=94=EC=9D=B8=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EC=9E=AC=EC=B9=98=20=EC=A1=B0=ED=9A=8C=20#19?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zatch/zatchserver/ResponseMessage.java | 2 ++ .../controller/MainController.java | 15 ++++++++ .../zatchserver/domain/ViewPopularZatch.java | 36 +++++++++++++++++++ .../repository/MainRepository.java | 2 ++ .../repository/MainRepositoryImpl.java | 29 ++++++++++++++- .../zatchserver/service/MainService.java | 3 ++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/zatch/zatchserver/domain/ViewPopularZatch.java diff --git a/src/main/java/com/zatch/zatchserver/ResponseMessage.java b/src/main/java/com/zatch/zatchserver/ResponseMessage.java index df19c76..8727f0e 100644 --- a/src/main/java/com/zatch/zatchserver/ResponseMessage.java +++ b/src/main/java/com/zatch/zatchserver/ResponseMessage.java @@ -21,6 +21,8 @@ public class ResponseMessage { public static final String GET_NEAR_ZATCH_SUCCESS = "내 주변 재치 조회 성공"; public static final String GET_NEAR_ZATCH_FAIL = "내 주변 재치 조회 실패"; + public static final String GET_POPULAR_ZATCH_SUCCESS = "인기있는 재치 조회 성공"; + public static final String GET_POPULAR_ZATCH_FAIL = "인기있는 재치 조회 실패"; public static final String ZATCH_SEARCH_SUCCESS = "재치 검색 조회 성공"; public static final String ZATCH_SEARCH_FAIL = "재치 검색 조회 실패"; public static final String GET_SEARCH_RESULT_SUCCESS = "재치 검색 결과 조회 성공"; diff --git a/src/main/java/com/zatch/zatchserver/controller/MainController.java b/src/main/java/com/zatch/zatchserver/controller/MainController.java index 337c16d..ca82ba6 100644 --- a/src/main/java/com/zatch/zatchserver/controller/MainController.java +++ b/src/main/java/com/zatch/zatchserver/controller/MainController.java @@ -37,4 +37,19 @@ public ResponseEntity getNearZatch(@PathVariable("userId") Long userId) { return new ResponseEntity(DefaultRes.res(StatusCode.INTERNAL_SERVER_ERROR, ResponseMessage.GET_NEAR_ZATCH_FAIL, "Error Get Zatch"), HttpStatus.INTERNAL_SERVER_ERROR); } } + + //인기있는 재치 띄우기 + @GetMapping("{userId}/viewPopularZatch") + @ApiOperation(value = "메인페이지 내 인기있는 재치 조회", notes = "인기 있는 재치 조회 API") + public ResponseEntity getPopularZatch(@PathVariable("userId") Long userId) { + try { + mainService.getPopularZatch(userId); + return new ResponseEntity(DefaultRes.res(StatusCode.OK, ResponseMessage.GET_POPULAR_ZATCH_SUCCESS, userId), HttpStatus.OK); + + } catch (Exception e) { + return new ResponseEntity(DefaultRes.res(StatusCode.INTERNAL_SERVER_ERROR, ResponseMessage.GET_POPULAR_ZATCH_FAIL, "Error Get Zatch"), HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + } diff --git a/src/main/java/com/zatch/zatchserver/domain/ViewPopularZatch.java b/src/main/java/com/zatch/zatchserver/domain/ViewPopularZatch.java new file mode 100644 index 0000000..8ec5d12 --- /dev/null +++ b/src/main/java/com/zatch/zatchserver/domain/ViewPopularZatch.java @@ -0,0 +1,36 @@ +package com.zatch.zatchserver.domain; + +import lombok.Getter; + +import java.util.Date; + +@Getter +public class ViewPopularZatch { + private Long categoryId; + private Boolean isFree; + private String mdName; + private String content; + private Integer quantity; + private Date dateBuy; + private Date dateExpire; + private Date updated_at; + private Integer isOpened; + private Integer anyZatch; + private Integer likeCount; + + public ViewPopularZatch(Long categoryId, Boolean isFree, String mdName, String content + , Integer quantity, Date dateBuy, Date dateExpire, Date updated_at, Integer isOpened, Integer anyZatch, + Integer likeCount) { + this.categoryId = categoryId; + this.isFree = isFree; + this.mdName = mdName; + this.content = content; + this.quantity = quantity; + this.dateBuy = dateBuy; + this.dateExpire = dateExpire; + this.updated_at = updated_at; + this.isOpened = isOpened; + this.anyZatch = anyZatch; + this.likeCount = likeCount; + } +} diff --git a/src/main/java/com/zatch/zatchserver/repository/MainRepository.java b/src/main/java/com/zatch/zatchserver/repository/MainRepository.java index 893501a..e9cf02d 100644 --- a/src/main/java/com/zatch/zatchserver/repository/MainRepository.java +++ b/src/main/java/com/zatch/zatchserver/repository/MainRepository.java @@ -1,9 +1,11 @@ package com.zatch.zatchserver.repository; import com.zatch.zatchserver.domain.ViewNearZatch; +import com.zatch.zatchserver.domain.ViewPopularZatch; import java.util.List; public interface MainRepository { List getNearZatch(Long userId); + List getPopularZatch(Long userId); } diff --git a/src/main/java/com/zatch/zatchserver/repository/MainRepositoryImpl.java b/src/main/java/com/zatch/zatchserver/repository/MainRepositoryImpl.java index 6fd2379..caa7f45 100644 --- a/src/main/java/com/zatch/zatchserver/repository/MainRepositoryImpl.java +++ b/src/main/java/com/zatch/zatchserver/repository/MainRepositoryImpl.java @@ -1,6 +1,7 @@ package com.zatch.zatchserver.repository; import com.zatch.zatchserver.domain.ViewNearZatch; +import com.zatch.zatchserver.domain.ViewPopularZatch; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; @@ -21,7 +22,7 @@ public MainRepositoryImpl(DataSource dataSource) { @Override public List getNearZatch(Long userId) { List results = jdbcTemplate.query( - "SELECT user.user_id, zatch.zatch_id, zatch.category_id, zatch.is_free, zatch.item_name, zatch.content, zatch.updated_at, quantity, purchase_date, expiration_date, is_opened, allow_any_zatch, zatch.like_count, user.town1, user.town2, user.town3, zatch_img.zatch_img_url FROM zatch LEFT OUTER JOIN zatch_img ON zatch.zatch_id = zatch_img.zatch_id JOIN user ON zatch.user_id = user.user_id WHERE ((town1 IN (SELECT town1 or town2 or town3 FROM user)) or (town2 IN (SELECT town1 or town2 or town3 FROM user)) or (town3 IN (SELECT town1 or town2 or town3 FROM user))) and user.user_id != ?", + "SELECT user.user_id, zatch.zatch_id, zatch.category_id, zatch.is_free, zatch.item_name, zatch.content, zatch.updated_at, quantity, purchase_date, expiration_date, is_opened, allow_any_zatch, zatch.like_count, user.town1, user.town2, user.town3, zatch_img.zatch_img_url FROM zatch LEFT OUTER JOIN zatch_img ON zatch.zatch_id = zatch_img.zatch_id JOIN user ON zatch.user_id = user.user_id WHERE ((town1 IN (SELECT town1 or town2 or town3 FROM user)) or (town2 IN (SELECT town1 or town2 or town3 FROM user)) or (town3 IN (SELECT town1 or town2 or town3 FROM user))) and user.user_id != ?;", new RowMapper() { @Override public ViewNearZatch mapRow(ResultSet rs, int rowNum) throws SQLException { @@ -47,4 +48,30 @@ public ViewNearZatch mapRow(ResultSet rs, int rowNum) throws SQLException { }, userId); return results.isEmpty() ? null : results; } + + @Override + public List getPopularZatch(Long userId){ + List results = jdbcTemplate.query( + "SELECT user_id, zatch_id, category_id, is_free, md_name, content, updated_at, quantity, date_buy, date_expire, is_opened, any_zatch, like_count FROM zatch ORDER BY like_count DESC LIMIT 3;", + new RowMapper() { + @Override + public ViewPopularZatch mapRow(ResultSet rs, int rowNum) throws SQLException { + ViewPopularZatch popularZatch = new ViewPopularZatch( + rs.getLong("category_id"), + rs.getBoolean("is_free"), + rs.getString("md_name"), + rs.getString("content"), + rs.getInt("quantity"), + rs.getDate("date_buy"), + rs.getDate("date_expire"), + rs.getDate("updated_at"), + rs.getInt("is_opened"), + rs.getInt("any_zatch"), + rs.getInt("like_count") + ); + return popularZatch; + } + }); + return results.isEmpty() ? null : results; + } } diff --git a/src/main/java/com/zatch/zatchserver/service/MainService.java b/src/main/java/com/zatch/zatchserver/service/MainService.java index 9443ba9..fbd4899 100644 --- a/src/main/java/com/zatch/zatchserver/service/MainService.java +++ b/src/main/java/com/zatch/zatchserver/service/MainService.java @@ -1,6 +1,7 @@ package com.zatch.zatchserver.service; import com.zatch.zatchserver.domain.ViewNearZatch; +import com.zatch.zatchserver.domain.ViewPopularZatch; import com.zatch.zatchserver.repository.MainRepositoryImpl; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -14,4 +15,6 @@ public class MainService { public List getNearZatch(Long userId){return mainRepository.getNearZatch(userId);} + public List getPopularZatch(Long userId){return mainRepository.getPopularZatch(userId);} + }