Diary
[1D1C] 0318 기록
euuuuuz:
2024. 3. 19. 09:21
RESTful API 설계에 대한 고민
카테고리 별로 리스트를 가져오는 API 에서 category 정보를 어떻게 받을 것인가?
- url 파라미터로 받을 것인가? /products/{category}
- query 로 받을 것인가? /product?category=driver
ChatGPT 답변
If the category is a fundamental part of the resource, using a URL parameter may be more appropriate.
If the category is just one of many filtering options, using a query parameter may be more flexible.
-> 카테고리 분류는 서비스 내용상 핵심적이고 고정적인 부분임
-> 디테일 페이지의 id params 로 인해 /category/ 프리픽스를 붙여서 분리하였다
-> /:category 와 /:id 를 함께 쓰면 express 는 무조건 앞서있는 라우트로 이동 시킨다
@Get('/')
getProducts(@Query() query) {
return this.productsService.getProducts({
size: query.size,
page: query.page,
});
}
@Get('/category/:category')
getProductsByCategory(@Query() query, @Param() params) {
return this.productsService.getProductsByCategory({
category: params.category,
size: query.size,
page: query.page,
});
}
@Get('/:id')
getProductById(@Param() params) {
// TODO : 리뷰 테이블 조회하여 가져오기
return this.productsService.getProductById(params.id);
}