0

For a project we have a class called Item.

This Item has an ID, Title, Description and MediaFile[]. Now, we have an edit function in which we can change the title, description and add/remove MediaFile. Adding a MediaFile is done by posting a file to the backend, which returns a Filename and Id.

When we want to PUT the changed Item, our backend wants to have a different model, only containing the ID, Title, Description and an array containing MediaFile id's Guid[]. This demands an extra "conversion" in our frontend from MediaFile[] to Guid[]. Doesn't it make more sense to send the complete, updated Item as we got it with a GET in the first place?

In short: For collections in a class, should you send the complete array of classes or an array containing only id's of the classes?

MysticEarth
  • 2,646
  • 4
  • 33
  • 53

1 Answers1

1

I think there is no single point of view, as usual it's a trade of... Everything depends on your needs and particular scenario. In general, as you've mentioned, the same result could be achieved by two techniques:

  • PUTing the whole Item-resource representation, containing collection of referenced MediaFiles to API
  • Producing multiple backend calls, each creating link between Item resource and referenced Media file.

The former approach acts as a single operation, guarantiing more consistency, but has all disadvantages of massive backend call: in case of failure it's hard to determine was the whole reuest failed, or partially succeeded. Besides, if this batch processing takes reasonable time - user stays blocked till the end of request (I mean you are not able to build a UI, with progress reporting). It's all-or-nothing scenario

Letter one (multiple requests) adds separate resource for relation and produces extra backend calls, but is more user-friendly and allows UI to be more responsible. You can notify the user of the batch operation state and progress and, besides, handle failures one by one (r.g. apply some retry policy).

In other words:

Dependent resource approach:

PUT /items Body: {... MediaFile: [...]}

Multiple calls:

PUT /items Body: {...}
PUT /items/{itemId}/mediafiles/{mediaFileId} Body:{ ... MediaFileReosurse1 }
PUT /items/{itemId}/mediafiles/{mediaFileId} Body:{ ... MediaFileReosurse2 }
...
PUT /items/{itemId}/mediafiles/{mediaFileId} Body:{ ... MediaFileReosurseN }
n.prokhorov
  • 930
  • 7
  • 13