Astaroth Astaroth - 2 months ago 7
reST (reStructuredText) Question

Which Endpoint should represent a trhreeway many to many Relationship with non related elements as well

It's difficult to sintetize the question into one line, but I hope I can explain myself better here:

I've got the following model:

Users


  • id



Messages


  • id



Tags


  • id



UsersMessagesCategories


  • id

  • userId

  • messageId

  • tagId



Tags
are NOT created by the
user
, available
tags
are created via admin panel.

I want to make an endpoint that returns the avaliable
tags
for a
message
but with the ones that are already associated, by that
user
, to that
message
flagged.

I can't seem to find a Rest standard way to make this.

PS: Just to make it clear, I want:

{
tags:[
{id: 1, associated: false},
{id: 2, associated: true},
{id: 3, associated: false}
]
}


Or maybe the way is:

{
tags:[
associated:[
{id: 2},
{id: 1}
],
unassociated: [
{id: 3}
]
]
}


I also don't know if it should be returned by (I guess this is the one):

/users/1/messages/1/tags


or by:

/tags


or even by:

messages/1/tags


Edit: Changed the endpoints so the URI is built with plural names.

Answer

Why don't you try with something like /tags?user=X&message=Y? As I understood tags are not direct children of messages or users, so no need to be direct children in the URL pattern.
REST doesn't define a real pattern to access resources, so you can use it more query-like with GET parameters like I wrote or you can have a more tree-like structure as you proposed.
The best thing you can do is to make every REST resource of your project accessible via a similar URL pattern, to make it more user friendly.

The first solution you proposed to format in JSON the results is the better one in my opinion

{
    tags:
    [
        {id: 1, associated: false},
        {id: 2, associated: true},
        {id: 3, associated: false}
    ]
}

but also this depends a lot on how you modeled the others REST services of your project. If you want you can return also the user which associate the tag with the message to improve readability.

Comments