Skip to content

Commit

Permalink
Merge pull request #57 from Zatch-Team/Feature/ViewNearZatch
Browse files Browse the repository at this point in the history
feat: 메인 페이지 재치 조회 #19
  • Loading branch information
plum-king authored Jun 24, 2023
2 parents 8514500 + ebecd3d commit 11b9974
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/zatch/zatchserver/ResponseMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "재치 검색 결과 조회 성공";
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/zatch/zatchserver/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}


}
36 changes: 36 additions & 0 deletions src/main/java/com/zatch/zatchserver/domain/ViewPopularZatch.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ViewNearZatch> getNearZatch(Long userId);
List<ViewPopularZatch> getPopularZatch(Long userId);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,7 +22,7 @@ public MainRepositoryImpl(DataSource dataSource) {
@Override
public List<ViewNearZatch> getNearZatch(Long userId) {
List<ViewNearZatch> 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<ViewNearZatch>() {
@Override
public ViewNearZatch mapRow(ResultSet rs, int rowNum) throws SQLException {
Expand All @@ -47,4 +48,30 @@ public ViewNearZatch mapRow(ResultSet rs, int rowNum) throws SQLException {
}, userId);
return results.isEmpty() ? null : results;
}

@Override
public List<ViewPopularZatch> getPopularZatch(Long userId){
List<ViewPopularZatch> 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<ViewPopularZatch>() {
@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;
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/zatch/zatchserver/service/MainService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,4 +15,6 @@ public class MainService {

public List<ViewNearZatch> getNearZatch(Long userId){return mainRepository.getNearZatch(userId);}

public List<ViewPopularZatch> getPopularZatch(Long userId){return mainRepository.getPopularZatch(userId);}

}

0 comments on commit 11b9974

Please sign in to comment.