I need some advice on deciding which endpoint to use in order to create an article and have that article be assigned to a publication.
Say I have two API endpoints:
- /v1/publications
- /v1/articles
If I want to create a new article and have it automatically assigned to a publication, should I:
- Make the call to
POST /v1/publications/article
and have that endpoint create the new article as well as its assignment to the specified publication or - Make the call to
POST /v1/articles
to create the new article and have that endpoint assign the new article to the publication
Ex:
Publications.createArticle(publicationId);
// creates a new article
// publication id is required and auto assigns the new article to publication id
vs.
Articles.create(publicationId);
// Creates a new article
// publication id is optional. If it exists, assign it to the publication
// If it does not exist, just create the article
Top comments (3)
Critical details, hinted at by Kasey - lifecycles.
Can articles exist by themselves? Is a publication a collection of articles that has it's own lifecycle? Can a publication be "empty" of articles?
Is the relationship / process of "publishing" articles missing?
Answering these will hopefully give you a better "real world" model of what you are trying to achieve, and thus a better API design - fun innit?
I completely agree with Phil and Kasey. Jumping right into it so it would IMHO be modelized like this:
Below is the according REST endpoint you could produce (GET, POST).
Hope it helps?
Forgive me -- the linked post is not a direct answer, but I think gets at the heart of the issue.
Things that happen in relationships are the things that matter.
Following the advice there, I would say that the relationship between the article and the publication is an entity itself. If your REST API represents essentially database calls, then it would be two operations: one on article and one on the article-to-publication link entity.
Edit: I suppose with this answer I was also assuming something different than the title. That it is potentially a many-to-many relationship between article and publication. If it is a 1-to-many relationship from publication to article, then it seems fair that article could be a sub-entity of publication.