@Getter
@AllArgsConstructor
@JsonPropertyOrder({"isSuccess", "code", "message","hasNext","result"})
public class StoreResponse<T,Boolean> {//BaseResponse 객체를 사용할때 성공, 실패 경우
@JsonProperty("isSuccess")
private final boolean isSuccess;
private final String message;
private final int code;
private boolean hasNext;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T result;
// 요청에 성공한 경우
public StoreResponse(T result, boolean hasNext) {
this.isSuccess =SUCCESS.isSuccess();
this.message = (String)SUCCESS.getMessage();
this.code =SUCCESS.getCode();
this.hasNext=hasNext;
this.result = result;
}
// 요청에 실패한 경우
public StoreResponse(BaseResponseStatus status) {
this.isSuccess = status.isSuccess();
this.message = (String) status.getMessage();
this.code = status.getCode();
}
}
BaseResponse를 변형해서 만든 클래스.
hasNext를 넘겨주는데 한번만 넘겨주기 위해서 사용함
/*
페이징 사용
방법2- 페이지 번호 주기
*/
//Query String
@ResponseBody // return되는 자바 객체를 JSON으로 바꿔서 HTTP body에 담는 어노테이션.
// JSON은 HTTP 통신 시, 데이터를 주고받을 때 많이 쓰이는 데이터 포맷.
@GetMapping("/stores")
// GET 방식의 요청을 매핑하기 위한 어노테이션
public StoreResponse<List<GetStoreRes>,Boolean> getStores(@RequestParam(defaultValue="1") int pageNum) {
try {
List<GetStoreRes> getStoreRes = storeProvider.getStores(pageNum);
boolean hasNext = storeProvider.hasNext(pageNum);
return new StoreResponse<>(getStoreRes,hasNext);
} catch (BaseException exception) {
return new StoreResponse<>((exception.getStatus()));
}
}
//recordsPerPage는 상수값 3
int offset = pageNum*recordsPerPage;
Object[] params = new Object[]{recordsPerPage,offset};
String getStoresQuery = "select * from Store orders limit ? offset ?"
/*
페이징 처리
방법3- 최근 본 idx를 기준으로 paging
한 페이지에 보여주는 레코드 : 3개
*/
//Query String
@ResponseBody // return되는 자바 객체를 JSON으로 바꿔서 HTTP body에 담는 어노테이션.
// JSON은 HTTP 통신 시, 데이터를 주고받을 때 많이 쓰이는 데이터 포맷.
@GetMapping("/store") // (GET) 127.0.0.1:9000/app/users
// GET 방식의 요청을 매핑하기 위한 어노테이션
public StoreResponse<List<GetStoreCategoryRes>,Boolean> getStoreCategories(@RequestParam String categoryName, @RequestParam(defaultValue="0") int lastIdx) {
// @RequestParam은, 1개의 HTTP Request 파라미터를 받을 수 있는 어노테이션(?뒤의 값). default로 RequestParam은 반드시 값이 존재해야 하도록 설정되어 있지만, (전송 안되면 400 Error 유발)
// 지금 예시와 같이 required 설정으로 필수 값에서 제외 시킬 수 있음
// defaultValue를 통해, 기본값(파라미터가 없는 경우, 해당 파라미터의 기본값 설정)을 지정할 수 있음
try {
List<GetStoreCategoryRes> getStoreCategoryRes = storeProvider.getStoresByCategoryName(categoryName,lastIdx);
boolean hasNext = storeProvider.hasNext(categoryName,lastIdx);
//null값이면 메시지를 반환한다.
if(getStoreCategoryRes.isEmpty()){
String message =categoryName+"가게가 없습니다.";
return new StoreResponse(message,false);
}
return new StoreResponse<>(getStoreCategoryRes,hasNext);
} catch (BaseException exception) {
return new StoreResponse<>((exception.getStatus()));
}
}
select *
from Category c
inner join StoreCategory sc on sc.categoryIdx=c.categoryIdx
inner join Store s on sc.storeIdx=s.storeIdx
where c.categoryIdx =?
orders limit ? offset ?