2017-07-12 60 views
1

我有两个实体“人”和“企业”和它们都有一个子资源(如位置)过滤参数

所以我有终点:

GET /persons/{id}/locations 
GET /businesses/{id}/locations 

现在我希望端点过滤主资源的位置。 如果我做的:

GET /persons/{id}/locations?country={...} 
GET /businesses/{id}/locations?country={...} 

我将寻找一个特定的人/企业的位置。

什么是过滤所有的人 的位置的最佳实践我有一些想法:

1. GET /persons/locations?country={...} 
2. GET /locations?entity=persons&country={...} 

,但不知道这些都是罚款。

回答

0

您可以使用expand概念来处理实体之间的关系。

GET /persons 

{ 
    "data": [ 
     {"person_id": 1}, 
     {"person_id": 2} 
], 
"links": [ 
    { 
     "rel": "locations", 
     "href": "/person/1/locations" 
    }, 
    { 
     "rel": "locations", 
     "href": "/person/2/locations" 
    } 
    ] 
} 

GET /persons?expand=locations&country={...} 

{ 
    "data": [ 
     {"person_id": 1, "locations": [...]}, 
     {"person_id": 2, "locations": [...]} 
    ] 
}