1

I have a database of surveys responses and I'm trying to build a RESTful API to expose this data. API should be able to return the following information:

  1. Available surveys
  2. Questions
  3. Answers
  4. Report (sum of responses for every type of answer per survey question)

I have a problem with identifying how should the resources be structured in this case.

For surveys, questions and responses it's fairly simple:

GET /surveys
GET /questions // filter by surveyId possible
GET /responses // filter by questionId and SurveyId possible

What would be the best approach to fit the reports in this structure? Do I set up a separate route, e.g.:

GET /reports/responses-by-question?surveyId=1

or do I add it as next level in the responses path? E.g.

GET /responses/by-question?surveyId=1

Or do I get this completely wrong and there's some better approach?

2
  • How about surveys/1/reports to get all reports for a survey? Commented Aug 7, 2018 at 13:01
  • I was thinking about it, but the report calculates the responses, not surveys, hence it doesn't seem logical to me... But maybe I'm overcomplicating this. Commented Aug 7, 2018 at 13:07

1 Answer 1

1

Keeping the survey as the first class citizen, I would structure it like so

  1. GET /surveys: Get all surveys
  2. GET /surveys/<surveyId>/questions: Get questions for a survey.
  3. GET /surveys/<surveyId>/questions/<questionId>/answers: Get answers for a survey question.
  4. GET /surveys/<surveyId>/reports: Get report for a survey.

Note: If you think reports are going to be filtered by multiple paramaters, you could consider making it a search end point.

GET /surveys/<surveyId>/reports?questionId=<questionId>

Sign up to request clarification or add additional context in comments.

3 Comments

I like it, looks very elegant. Do you know any good guides on how to determine first class citizens in such situations?
That's more to do with how you design your domain entities for the service you're building. For example, if you're designing an order management system, it's likely an Order is the first class citizen.
Also, here's a video you may find useful: youtube.com/watch?v=U6CeaA-Phqo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.