API 경로 구조를 구성하는 방법에 대한 질문이 있습니다. RESTful API를 작성하는 것에 대한 많은 책을 읽었으며 이에 대한 답을 찾을 수 없습니다. 모든 책은 간단한 CRUD 작업과 중첩 된 리소스를 한 수준에서 알려줍니다. 예 : /users
및 /users/1/posts
. 그거 아무 문제 없어.RESTful API : 중첩 된 리소스를 구성하는 방법
그러나 좀 더 어려운 실생활의 예를 살펴 보자 :
예
이GET /cars // List of cars
GET /cars/{car_id} // Get single car
POST /cars // Add new car
PUT /cars/{car_id} // Update existing car
DELETE /cars/{car_id} // Delete car by specified ID
cars
의 데이터베이스 테이블의 구조가 지금까지
Table "cars"
- id
- uuid
- make
- model
- year
- created_at
- updated_at
- deleted_at
아무런 문제가 될 것이지만, 그럼 중첩 된 리소스를 추가해야합니다. 지정된 자동차로 완료된 모든 수리. 데이터베이스 테이블에 대한
GET /cars/{car_id}/repairs // List of repairs that were done with car
GET /cars/{car_id}/repairs/{repair_id} // Get single repair
POST /cars/{car_id}/repairs // Add new repair for specified car
PUT /cars/{car_id}/repairs/{repair_id} // Update existing repair for specified car
DELETE /cars/{car_id}/repairs/{repair_id} // Delete repair by specified ID
구조는 지금까지 아무 문제
Table "car_repairs"
- id
- uuid
- car_id (foreign key to cars)
- title
- description
- repair_started_at
- repair_ended_at
- created_at
- updated_at
- deleted_at
없을 것이다. 모든 책 에서처럼. /users
경로 및 중첩 경로 /users/1/posts
. 하지만 여기서 또 다른 중첩 수준을 추가해야 할 때 문제가 시작됩니다.
나는
GET /cars/{car_id}/repairs/{repair_id}/defects // List of all defects that were found for specified repair for speicified car
GET /cars/{car_id}/repairs/{repair_id}/defects/{defect_id} // Get single defect
POST /cars/{car_id}/repairs/{repair_id}/defects // Add new defect
PUT /cars/{car_id}/repairs/{repair_id}/defects/{defect_id} // Update existing defect
DELETE /cars/{car_id}/repairs/{repair_id}/defects/{defect_id} // Delete existing defect
테이블 구조가 될 것 자동차 수리 때 발견 된 모든 결함에 대한 CRUD 경로를 필요
Table "car_repair_defects"
- id
- uud
- car_id
- repair_id
- name
- description
- created_at
- updated_at
- deleted_at
질문입니다 :
여기에 수행 할 작업 , 정상적인 연습이나하지 레벨 .../defects
입니까? 내가 예에서, 중첩의 또 다른, 4 레벨을 추가해야하는 경우
가 상황을 고려, 사용 된 모든 부품은 가장 좋은 방법은 편안하고 API의에서 중첩 자원
하나는 말할 수있을 때 어떤 결함
발견 이 작업은 중첩없이 수행 할 수 있습니다. 예 /cars
/repairs
/defects
/parts
그렇다면 중첩 된 리소스가있는 RESTful 예제는 어떻습니까? 그렇다면 중첩 자원 0, 1, 2, 3의 최대 레벨은 무엇입니까?
또한 중첩없이 완료되는 경우 예를 들어 모든 가능한 자동차 결함을 나열하는 사전 경로 /defects
을 만들어야합니다. 이름 충돌이있을 것입니다.
또한 중첩이없는 경우 항목을 필터링하면 중첩으로 필터링 할 수 있습니까?
defects?car_id=1&repair_id=2&defect_id=3
이렇게하면? 이것은보기 흉한.
책이나 기사를 가리 키거나 이전에 나열한 중첩 및 질문의 최대 수준에 대한 답변을주십시오. 감사합니다.
내 질문에 투표가 거부되면 이유를 설명해주십시오. – user991