You have the following situation:
- You have an SQL database containing two tables
product
andproduct_images
- You would like the user to be able to add, remove, or move around existing images
- They can perform those actions from a form which downloads the current state of the product and displays each picture in order.
- When a user select a new picture and uploads it, the frontend 1) sends the picture to backend at /upload-picture, 2) backend saves it to S3 and responds with the location of the picture, 4) when user clicks submit the picture URLs are included in the edit product request
What is the usual or good approach to perform the changes?
I thought of the followings ways:
Solution 1: delete then recreate
Frontend sends an array containing both existing and new URLs, something like this:
{ "pictures": ['old1', 'old2', 'new1', 'old3', 'new2'] }
Backend deletes all
product_images
rows of the product and recreates from thatpictures
array.
Solution 2: compare old and new array, send a diff
Frontend sends an array containing a diff of the form (added, removed, same). For example:
{ "pictures": { "added": [{"url": "new1", "index": 2}, {"url": "new2", "index": 1}], "removed": [{"url": "old1", "oldIndex": 1}], "same": [{"url": "old2", "oldIndex": 2, "newIndex": 0}], } }
It seems like the first solution is easier to implement. What do you think?