REST and many for many

I base my question on How to handle many-to-many relationships in a RESTful API? but I want to continue with the accepted answer.

Assume that there is a many-to-many relationship between players and teams (as in the question mentioned above).

As I understand it, there are several options for modeling this using REST resources:

The payload contains links to related resources.

GET /players/john

gives

{
    "name": "John",
    "_links": [
        {
            "rel": "team",
            "href": "/teams/1"
        },
        {
            "rel": "team",
            "href": "/teams/4"
        }
    ]
}

and

GET /teams/1

gives

{
    "name": "Team 1",
    "_links": [
        {
            "rel": "player",
            "href": "/players/john"
        },
        ...
    ]
}

, . , , . " " API RESTful?:

, URL/player/5/teams/

/1 , "" , " 1".

GET /teams/1

{
    "name": "Team 1",
}

GET /players/john

{
    "name": "John",
}

,

GET /relationships

    [
    {
        "_links": [
            {
                "rel": "player",
                "href": "/players/john"
            },
            {
                "rel": "team",
                "href": "/teams/1"
            }
        ]
    },

    ...
]

, , , . /players/john, ? , , , . , , , API.

, , "" , . ( ), - "" " 1".

GET /teams/1/players

-

{
    "_links": [
        {
            "rel": "player",
            "href": "/players/john"
        },
        ...
    ]
}

GET /players/john/teams

-

{
    "_links": [
        {
            "rel": "team",
            "href": "/teams/1"
        },
        ...
    ]
}

- , URL- ( )

, ?

"" REST?

contstraint, " " API RESTful?:

, URL/player/5/teams/

!

+4
1

GET /teams/dream

{
    "_links": {
        "self": {
            "href": "/teams/dream"
        }
        "players": {
            "href": "/players?team=dream"
        }
    },
    "name": "Dream"
}

GET /player/john

{
    "_links": {
        "self": {
            "href": "/player/john"
        },
        "teams": {
            "href": "/teams?player=john"
        },
    },
    "name": "John",
}

GET /teams?player=john

{
    "_links": {
    },
    "items": [
        {
            "href": "/teams/a-team"
        }
    ],
}

john (, json-) ( ... .. , )

PATCH /teams?player=john

[{
    "op": "add",
    "path": "/-",
    "value": {
        "href": "/team/dream"
    }
}]

john

GET /teams?player=john

{
    "_links": {
    },
    "items": [
        {
            "href": "/teams/a-team"
        },
        {
            "href": "/teams/dream"
        }
    ]
}

:

PATCH /teams?player=john

[{
    "op": "remove",
    "path": "/0"
}]
+1

All Articles