Skip to content

Commit

Permalink
feat: 내 근처 재치 찾기 데이터 조회 로직 수정 #19
Browse files Browse the repository at this point in the history
  • Loading branch information
plum-king committed Jul 14, 2023
1 parent 5a2859e commit 7d5f3b2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zatch.zatchserver.DefaultRes;
import com.zatch.zatchserver.ResponseMessage;
import com.zatch.zatchserver.StatusCode;
import com.zatch.zatchserver.domain.ViewNearZatch;
import com.zatch.zatchserver.service.MainService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +14,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/main")
Expand All @@ -35,10 +38,11 @@ public ResponseEntity getMyTown(@PathVariable("userId") Long userId){
@ApiOperation(value = "메인페이지 내 주변 재치 조회", notes = "내 주변 재치 조회 API")
public ResponseEntity getNearZatch(@PathVariable("userId") Long userId) {
try {
mainService.getNearZatch(userId);
return new ResponseEntity(DefaultRes.res(StatusCode.OK, ResponseMessage.GET_NEAR_ZATCH_SUCCESS, userId), HttpStatus.OK);
List<ViewNearZatch> nearZatches = mainService.getNearZatch(userId);
return new ResponseEntity(DefaultRes.res(StatusCode.OK, ResponseMessage.GET_NEAR_ZATCH_SUCCESS, nearZatches), HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity(DefaultRes.res(StatusCode.INTERNAL_SERVER_ERROR, ResponseMessage.GET_NEAR_ZATCH_FAIL, "Error Get Zatch"), HttpStatus.INTERNAL_SERVER_ERROR);
System.out.println(e);
return new ResponseEntity(DefaultRes.res(StatusCode.INTERNAL_SERVER_ERROR, ResponseMessage.GET_NEAR_ZATCH_FAIL, "Error Get Near Zatch"), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/zatch/zatchserver/domain/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ public class Address {
private String addr_name;
private String addr_x;
private String addr_y;

public Address(){};

public Address(String addr_name, String addr_x, String addr_y) {
this.addr_name = addr_name;
this.addr_x = addr_x;
this.addr_y = addr_y;
}
}
31 changes: 12 additions & 19 deletions src/main/java/com/zatch/zatchserver/domain/ViewNearZatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,30 @@
public class ViewNearZatch {
private Long categoryId;
private Boolean isFree;
private String itemName;
private String mdName;
private String content;
private String zatchImgUrl;
private Integer quantity;
private Date purchaseDate;
private Date expirationDate;
private Date updated_at;
private Date dateBuy;
private Date dateExpire;
private Date updatedAt;
private Integer isOpened;
private Integer anyZatch;
private Integer likeCount;
private String town1;
private String town2;
private String town3;
private String addrName;

public ViewNearZatch(Long categoryId, Boolean isFree, String itemName, String content, String zatchImgUrl
, Integer quantity, Date purchaseDate, Date expirationDate, Date updated_at, Integer isOpened, Integer anyZatch,
Integer likeCount, String town1, String town2, String town3){
public ViewNearZatch(Long categoryId, Boolean isFree, String mdName, String content, Integer quantity, Date dateBuy, Date dateExpire, Date updatedAt, Integer isOpened, Integer anyZatch,
Integer likeCount, String addrName) {
this.categoryId = categoryId;
this.isFree = isFree;
this.itemName = itemName;
this.mdName = mdName;
this.content = content;
this.zatchImgUrl = zatchImgUrl;
this.quantity = quantity;
this.purchaseDate = purchaseDate;
this.expirationDate = expirationDate;
this.updated_at = updated_at;
this.dateBuy = dateBuy;
this.dateExpire = dateExpire;
this.updatedAt = updatedAt;
this.isOpened = isOpened;
this.anyZatch = anyZatch;
this.likeCount = likeCount;
this.town1 = town1;
this.town2 = town2;
this.town3 = town3;
this.addrName = addrName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

@Getter
public class ViewPopularZatch {
private Long zatchId;
private Long categoryId;
private Boolean isFree;
private String mdName;
Expand All @@ -18,9 +19,10 @@ public class ViewPopularZatch {
private Integer anyZatch;
private Integer likeCount;

public ViewPopularZatch(Long categoryId, Boolean isFree, String mdName, String content
public ViewPopularZatch(Long zatchId, Long categoryId, Boolean isFree, String mdName, String content
, Integer quantity, Date dateBuy, Date dateExpire, Date updated_at, Integer isOpened, Integer anyZatch,
Integer likeCount) {
this.zatchId = zatchId;
this.categoryId = categoryId;
this.isFree = isFree;
this.mdName = mdName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zatch.zatchserver.repository;

import com.zatch.zatchserver.domain.Address;
import com.zatch.zatchserver.domain.ViewNearZatch;
import com.zatch.zatchserver.domain.ViewPopularZatch;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -29,31 +30,47 @@ public String getMainTown(Long userId){

@Override
public List<ViewNearZatch> getNearZatch(Long userId) {
String sql = "SELECT main_addr_x, main_addr_y FROM user WHERE user_id = ?;";
Object[] params = {userId};
Double main_addr_x = Double.valueOf(String.valueOf(jdbcTemplate.queryForList(sql, params).get(0).get("main_addr_x")));
Double main_addr_y = Double.valueOf(String.valueOf(jdbcTemplate.queryForList(sql, params).get(0).get("main_addr_y")));

List<Address> addresses = jdbcTemplate.query(
"SELECT main_addr_name, main_addr_x, main_addr_y FROM user WHERE user_id in (SELECT user_id FROM zatch);",
new RowMapper<Address>() {
@Override
public Address mapRow(ResultSet rs, int rowNum) throws SQLException {
Address address = new Address(
rs.getString("main_addr_name"),
String.valueOf(rs.getDouble("main_addr_x")),
String.valueOf(rs.getDouble("main_addr_y"))
);
return address;
}
});

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 zatch.user_id, zatch_id, category_id, is_free, md_name, content, zatch.updated_at, quantity, date_buy, date_expire, is_opened, any_zatch, like_count, main_addr_name FROM zatch JOIN user on zatch.user_id = user.user_id;",
new RowMapper<ViewNearZatch>() {
@Override
public ViewNearZatch mapRow(ResultSet rs, int rowNum) throws SQLException {
ViewNearZatch nearZatch = new ViewNearZatch(
rs.getLong("category_id"),
rs.getBoolean("is_free"),
rs.getString("item_name"),
rs.getString("md_name"),
rs.getString("content"),
rs.getString("zatch_img_url"),
rs.getInt("quantity"),
rs.getDate("purchase_date"),
rs.getDate("expiration_date"),
rs.getDate("date_buy"),
rs.getDate("date_expire"),
rs.getDate("updated_at"),
rs.getInt("is_opened"),
rs.getInt("allow_any_zatch"),
rs.getInt("any_zatch"),
rs.getInt("like_count"),
rs.getString("town1"),
rs.getString("town2"),
rs.getString("town3")
rs.getString("main_addr_name")
);
return nearZatch;
}
}, userId);
});
return results.isEmpty() ? null : results;
}

Expand All @@ -65,6 +82,7 @@ public List<ViewPopularZatch> getPopularZatch(Long userId){
@Override
public ViewPopularZatch mapRow(ResultSet rs, int rowNum) throws SQLException {
ViewPopularZatch popularZatch = new ViewPopularZatch(
rs.getLong("zatch_id"),
rs.getLong("category_id"),
rs.getBoolean("is_free"),
rs.getString("md_name"),
Expand Down

0 comments on commit 7d5f3b2

Please sign in to comment.