Dmitriy
Dmitriy

Reputation: 683

REST get resource by two ids

I have two entities: articles and users. Users may write comment about articles, however the specified user may write only one comment about the specified article. So, comment can be identified by article_id and user_id.

The classic REST GET request for comment is: /comment/:id. But in my case I don't have comment_id because it is useless. I think about /comment/:article_id/:user_id or /comment/:article_id!:user_id GET requests.

What is the best practice for such cases?

Upvotes: 0

Views: 57

Answers (2)

morsor
morsor

Reputation: 1323

I wouldn't call the missing commentId 'useless', as this situation is exactly when you need it. If at all possible, you should create a surrogate primary key (with no business meaning and created automatically by your DB) in your comments table - instead of the compound key of userID + articleID.

This would allow for more flexibility when/if requirements change. Perhaps users will be allowed to post more comments or the comments need to be threaded.

If the database is unchangeable legacy, I agree with the URLs given by @Gerino

Upvotes: 1

Gerino
Gerino

Reputation: 1983

As you said, standard form would be: /comment/{comment_id}

In your case I'd probably go for: /user/{userId}/comment/{articleId} or /user/{userId}/article/{articleId}/comment

Note, that this is solely based on what I've seen around, not on any formal recommendations.

Upvotes: 2

Related Questions