REST风格的问题/答案的设计?
-
23-09-2019 - |
题
这是一个玩具项目我工作的时刻。
我的应用包含选择题答案的问题。
在问题网址为以下列格式,与GET
&POST
映射到问题控制器执行不同的操作。
GET: url.com/questions/:category/:difficulty => 'ask'
POST: url.com/questions/:category/:difficulty => 'answer'
我不知道它是否值得重新设计成一个RESTful风格这一点。我知道我需要介绍的答案作为一种资源,但我努力思考的URL看起来自然的回答这个问题的。
将重新设计是值得的?你会如何去结构的网址吗?
解决方案
我认为这将是更好,如果你映射:
POST url.com/:category/:difficulty/questions => ask question
GET url.com/questions => list all questions
GET url.com/:category/:difficulty/questions => list questions for category and difficulty
GET url.com/questions/:id => show question
POST url.com/questions/:id => add answer
PUT url.com/questions/:id => edit question
PUT url.com/questions/:question_id/:id => edit answer with id :id
GET url.com/questions/:question_id/:id => show question with highlighted answer (like it is here on SO)
如果你会做:
POST: url.com/questions/:category/:difficulty => 'answer'
然后你可以有只有一个指定的类别和难度的问题。
其他提示
您不一定需要把答案作为一个单独的资源:你可以把答案作为一个问题提出的一个元素。事实上,我认为这将是更好的 - 答案是不是真的值得任何东西,除非它绑到一个问题
。不隶属于 StackOverflow