1

I have:

  • A User entity.
  • A Poll entity.

Relationship: User creates polls.

Use-case:
When an arbitrarily user is clicked his/her profile is loaded and shown.
The profile includes a list of polls created by the clicked user.

Which of the following api calls is the proper usage of such a use-case?

  1. website.com/api/users/{username}/polls
  2. website.com/api/polls?username=xxx

Between The Lines:
I currently have a UserController & a PollController.

PollController has:

  • getPolls()
  • createPoll()
  • getPollById()

UserController has functions related to the user, handles api calls starting with /api/users/...

I am trying to figure out which controller should handle the request to get polls by a user.

2

If you keep it under the user controller it will be more cohesive. The user controller should serve all the operations needed for the user and one of them is getting user polls.

It will make sense to have a polls controller when you have an independent use case for polls like getting polls statistics over all users.

1

Which of the following api calls is the proper usage of such a use-case?

REST doesn't care what spelling you use for your resource identifiers. So you can choose any spelling you want.

/api/users/{username}/polls
/api/polls?username=xxx

Both of these are fine.

You get somewhat different trade offs:

/api/users/{username}/polls -- here, you can use relative references in your poll resource to easily identify other resources related to the same user (ex: general purpose components will recognize that ../profile means /api/users/{username}/profile

/api/polls?username=xxx -- here, you can easily create HTML forms that allow the client to specify the username.

Neither of these is a game changer, is it? The differences between the two are really small.

I am trying to figure out which controller should handle the request to get polls by a user.

You could use a dedicated controller for this, which might be the simplest thing. But if you must choose between the UserController and the PollController, choose the one that already has most of the dependencies you need to implement this new resource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.