RESTful 问题/答案设计?

发布于 2024-08-27 10:25:29 字数 394 浏览 3 评论 0原文

这是我目前正在进行的一个玩具项目。

我的应用程序包含带有多项选择答案的问题。

问题url的格式如下,带有GET & POST 映射到问题控制器上的不同操作。

GET:   url.com/questions/:category/:difficulty      => 'ask'
POST:  url.com/questions/:category/:difficulty      => 'answer'

我想知道是否值得将其重新设计为 RESTful 风格。我知道我需要引入答案作为资源,但我正在努力想出一个看起来很自然的网址来回答这个问题。

重新设计值得吗?您将如何构建 url?

This is a toy project I'm working on at the moment.

My app contains questions with multiple choice answers.

The question url is in the following format, with GET & POST mapping to different actions on the questions controller.

GET:   url.com/questions/:category/:difficulty      => 'ask'
POST:  url.com/questions/:category/:difficulty      => 'answer'

I'm wondering if it's worth redesigning this into a RESTful style. I know I'd need to introduce answers as a resource, but I'm struggling to think of a url that would look natural for answering that question.

Would a redesign be worthwhile? How would you go about structuring the urls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2024-09-03 10:25:29

我认为如果你映射:

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'

那么你只能有一个具有指定类别和难度的问题。

I think it would be better if you map:

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)

If you would do:

POST:  url.com/questions/:category/:difficulty      => 'answer'

Then you could have only one question with specified category and difficulty.

猥琐帝 2024-09-03 10:25:29

您不一定需要将答案视为单独的资源:您可以将答案视为问题的元素。事实上,我认为这样会更好——除非与问题相关,否则答案实际上没有任何价值。

You don't necessarily need to treat answers as a separate resource: you could treat an answer as an element of a question. In fact, I think this would be better - an answer isn't really worth anything unless it's tied to a question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文