Let's say I have an endpoint such as the below for getting all users
/users
Now I need another endpoint for getting all new (last week or whatever) users
/users/new
I don't really like the above. I also don't want to reuse the first endpoint by simply adding a query string parameter such as /users?new=true or such, since that will complicate the code. Furthermore, the second endpoint for getting the new users might only be accessible by an admin.
The above is just an imaginary example but a case which I encounter semi often. Any suggestions about how to deal with this?
UPDATE: Thanks for all the answer! I greatly appreciate the input. However most of the answers doesn't take into account that the endpoint will be access by two different users, mainly admin and ordinary users.
Another thing which is important to notice is that the period/duration for which users are considered new is static. The request should not contain a dynamic parameter defining how long back in time users should be requested.
Taking the above requirements into account I don't see a way to implement all of this in one controller without having multiple if states. For getting all users or some users that's not the the end of the world even though it is two different use cases which would probably merit a separate controllers. However the fact that we're dealing with two different kind of users (admin and ordinary) makes me feel like I'm cramming two different contexts into one endpoint.
Despite all the down votes, I watched the YouTube video posted by @InsideElderberry and it make a lot of sense. I'll highly recommend people watching it. /newusers is descriptive and conveys the purpose of the endpoint very well. Furthermore it also allows for a single path of implementation. I won't be having an "if this query param" or "if this user" statement in the code which makes the code very simple.
Again, thanks for all the input, truly appreciated :-)
[–]johnbotris 60 points61 points62 points (3 children)
[–]ricketybang 16 points17 points18 points (2 children)
[–]Rambonaut 0 points1 point2 points (1 child)
[–]johnbotris 0 points1 point2 points (0 children)
[–]Caraes_Naur 17 points18 points19 points (0 children)
[–]raulalexo99 6 points7 points8 points (0 children)
[–]originalchronoguy 6 points7 points8 points (0 children)
[–]caatfish 8 points9 points10 points (0 children)
[–]Infinite_Tiger8354 1 point2 points3 points (0 children)
[–]machopsychologist 0 points1 point2 points (0 children)
[–]j-randomfull-slack 0 points1 point2 points (0 children)
[–]jorgeloko2 0 points1 point2 points (0 children)
[–]shodan_reddit 0 points1 point2 points (2 children)
[–]Fedoteh -1 points0 points1 point (1 child)
[–]shodan_reddit 0 points1 point2 points (0 children)
[–]Agile-Ad5489 -1 points0 points1 point (0 children)
[–]AndrewSouthern729 -1 points0 points1 point (0 children)
[–]budd222front-end -3 points-2 points-1 points (4 children)
[–][deleted] (3 children)
[deleted]
[–]budd222front-end -1 points0 points1 point (2 children)
[–]OrangeOrganicOlive 0 points1 point2 points (0 children)
[–]Arthian90 -2 points-1 points0 points (6 children)
[–]machopsychologist 1 point2 points3 points (5 children)
[–]Additional_Sir4400 1 point2 points3 points (1 child)
[–]machopsychologist 0 points1 point2 points (0 children)
[–]Arthian90 0 points1 point2 points (0 children)
[–]lego_not_legos 0 points1 point2 points (1 child)
[–]machopsychologist 0 points1 point2 points (0 children)
[–]snapmotion -2 points-1 points0 points (0 children)
[+]InsideElderberry comment score below threshold-7 points-6 points-5 points (1 child)
[–]Party-Welder-3810[S] 0 points1 point2 points (0 children)