我的数据模式类似于
User
很多 Transaction
。 User
和Transaction
都是数据库中的不同表格
我有REST
个端点为
用户端点
GET /users/uuid # to get information about specific user
PUT /users/uuid # update information of a specific user
交易终点
GET /transactions/uuid # get a specific transaction
GET /transactions/user_uuid # get all transactions of a specific user
GET /transactions/user_uuid/yyyy # get all transactions of a specific user in year yyyy
GET /transactions/user_uuid/yyyy/mm # get all transactions of a specific user in year yyyy, month mm
GET /transactions/user_uuid/yyyy/mm/dd # get all transactions of a specific user in year yyyy, month mm and day dd
transactions
始终会附加User
资源,为它们设置不同的端点是否有意义?GET /users/uuid/transactions # all transactions for a user GET /users/uuid/transactions/yyyy # all transactions for a user for year yyyy GET /users/uuid/transactions/yyyy/mm # all transactions for a user for year yyyy, month mm GET /users/uuid/transactions/yyyy/mm/dd # all transactions for a user for year yyyy, month mm and day dd
此刻我很困惑哪一个更好。
谢谢
答案 0 :(得分:1)
如果您始终在用户的上下文中访问transactions
,那么对您来说第二种方法是有意义的,因为正如您所说,用户有很多transactions
,您的第一种方法是网址设计可能相当令人困惑,因为它不清楚日期部分引用transactions
而不是用户资源。另一方面,如果您想要为所有用户阅读所有transactions
,可以理解您有transactions
的两个端点
GET /transactions/ # reads all transactions for all users
GET /transactions/yyyy # reads all trasnsaction for all users with date
GET /users/uuid/transactions # reads all transaction for a particular user
GET /users/uuid/transactions/yyyy # reads all transactions for a particular user with date