Sorry upfront if this is duplicate, but I was unable to find topic with answers to my questions.
I'm building an MEAN stack app, and I'm planning to create REST API on backend. Now I've stumbled on the following problem.
I have one resource /discussion, which contains list of users and posts itself. The resource roughly looks like this:
discussion = {
"name": "discussionName",
"users": [<arrayOfUsersThatArePartOfThisDiscussion>],
"posts": [<arrayOfPostsForThisDiscussion>]
}
What I want to achieve is list all the discussions that user contributed to, so that I can present those to him. Now, what I don't understand is what is the most REST-full way to do this.
My thinking is:
Maintain a list of discussions for the user. In that case, the user resource would roughly look like:
user = { "id": "myId", "name": "MyName", "discussionIds": [<listOfDiscussionsImPartOf>] }
If this is my representation, what happens when user tries to post into discussion that he previously didn't post?
My understanding is than in that case the POST /disucssions/:id/posts
should be executed to create another post.
But then, who is updating user/:userId/discussionIds? Is that client or server duty?
If it's server duty, how do I force cache invalidation for that resource (user/:userId/discussions)? so that I get fresh data on following GET user/:userId/discussions
- Don't maintain a list of discussions in user resource.
In this case, how do I filter discussions that user is part of? I was thinking of using query string with GET operation, but then again, how do I write query that will filter resource based on existence of provided param in it's array? For clarity purposes, I'm looking for something like:
GET api.something.com/discussions?where userId in discussion.usersIds
Hope you can answer my questions. Thanks in advance