lagoshny / ngx-hal-client

Spring HAL client for Angular
21 stars 15 forks source link

How do we do the PUT, POST or PATCH of a many to many relationship #39

Closed lagoshny closed 3 years ago

lagoshny commented 3 years ago

Question from the issue.

How do we do the PUT, POST or PATCH of a many to many relationship replacing the objects in the JSON with URLs?

lagoshny commented 3 years ago

You can use Resource class addRelation(relationName: string, resource: Resource) method to perform a PUT request with a body as URL instead Resource object.

There are no methods to perform POST or PATCH requests with a body as a URL instead Resource object.

grrenier commented 3 years ago

Let's suppose we have 2 resources, Client and ClientGroup with a many to many relationship, what do we do if we want to: 1 - Add multiple clients to a group. 2 - There are 3 clients in the group and we need to remove 2 of them and add 1 or more at the same time in a single http request. 3- Remove the the clients from the group, maybe this case is includes in the second case.

lagoshny commented 3 years ago

1) It is a problem because addRelation accepts only a single Resource instead of an array of resources, you can add many clients only in a loop of clients when adding each client per addRelation request. 2) I think that is not possible to add some relations and delete in one request by HATEOAS specification. 3) It is a problem too as the first point because deleteRelation accepts only a single Resource and can delete one resource per request.

I will think about how I can resolve the first and third points to use an array of resources instead single resource param.

lagoshny commented 3 years ago

@grrenier , I published 1.2.0 version that has the next changes:

addRealtion now accepts entity and an array of entities, which means when passed an array of entities they will be added to the resource collection.

substituteRelation has the same changes that allow you to replace a current collection of entities with new ones.

Also added new clearCollectionRelation method that accepts relation name and deletes all current entities relations.

You can read more about changes in CHANGELOG.md.

Answering your question now:

1. Use addRealtion with passed an array of entities to add several relations at the time 2,3. You can use substituteRelation to replace all current entities with new ones where you can delete unneeded and add new ones use one request 3. Because of HTTP delete method has not a body we can not delete several entities at the time but I added clearCollectionRelation method that deletes all related entities in one request.

grrenier commented 3 years ago

@lagoshny that's awesome! I will try the new functions and let you know about the result. Thank you!

lagoshny commented 3 years ago

Thanks for the issue. If you find some problems, create a new issue I close this.

grrenier commented 3 years ago

Hi @lagoshny , all is working properly. In fact just with substituteRelation we can do everything, but is nice to have the other functions also. Thank you again for maintaining this package.