简体   繁体   中英

RESTful Question/Answer design?

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM