I have created a collection called "movies" and another collection called "reviews" which holds the reviews for the movies in the collection "movies"(both are under sandbox application) and I am retying to connect them together and not sure how to do it! I read and tried every example in the documentation here with no luck yet!
Can someone please explain to me how can I connect two entities on BaaS? The goal is to retrieve the review of the movie if the user included "reviews=true" in the request!
Thank you
Hey Hani, can you show the steps you've taken to connect the entities? One thing you should also know - there isn't a way to retrieve both the entity and the connection at the same time; you'll need to retrieve them separately via {URL}/collection/entity/relationship.
Hi Brandon and thanks for your reply! here are the steps:
1- I created "movie" collection and I posted 10 movies manually to it and got 10 UUIDs
2- I created another collection named "reviews" with the entity "name" corresponding to the entity "name" in "movies", then I created couple dummy reviews for each movie, so I still have only 10 entities under "reviews" with the movies names but some movies has more than more review. (a review contain a name, comment, and a rating number out of 5)
3- From the documentation here I tried this via cURL:
curl -X POST https://api.usergrid.com/ /sandbox/movies/username/review/reviews
And I switched between movies and reviews because I wasn't sure what is "connecting entity" and "connected entity" in the example in the docs!
- is it right that I added the reviews manually or should I do the connectoin between the movies and
its reviews somehow else?
Thanks!
Answer by williamssean · Apr 25, 2016 at 02:58 AM
Hello @Hani Alghamdi
The way you should connect two entities is by UUID or entity name. I pasted how to connect two entities by UUID below. If you want the movies collection to connect to the reviews collection, then you have to create a relationship with the following request. In this instance, I used rated as the relationship name, but you could replace that with whatever you want.
curl -X POST https://api.usergrid.com/<org>/sandbox/movies/<movieUUID>/rated/<reviewUUID>
Here is my request and response. You can see my movie entity is now connected to the rating that I created.
curl -XPOST https://api.usergrid.com/myorg/sandbox/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated/accea87a-0a8d-11e6-87b9-3b0939e74c09
In the response you can see that the two entities are now connected.
{ "action":"post", "application":"31943980-e80e-832e", "params":{ "access_token":[ ""] }, "path":"/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated", "uri":"https://api.usergrid.com/swilliams/sandbox/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated", "entities":[ { "uuid":"accea87a-0a8d-11e6-87b9-3b0939e74c09", "type":"review", "name":"review1", "created":1461551428727, "modified":1461551428727, "metadata":{ "path":"/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated/accea87a-0a8d-11e6-87b9-3b0939e74c09" }, "rating":"great" } ], "timestamp":1461552246234, "duration":64, "organization":"myorg", "applicationName":"sandbox" }
Now I can access the rating with the following request. If you have multiple ratings attached to this movie, then all of them would be returned by this request.
curl -X GET https://api.usergrid.com/myorg/sandbox/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated
Response:
{
"action" : "get",
"application" : "319439b536a789da",
"params" : {
"access_token" : [ "" ]
},
"path" : "/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated",
"uri" : "https://api.usergrid.com/myorg/sandbox/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated",
"entities" : [ {
"uuid" : "accea87a-0a8d-11e6-87b9-3b0939e74c09",
"type" : "review",
"name" : "review1",
"created" : 1461551428727,
"modified" : 1461551428727,
"metadata" : {
"connecting" : {
"rated" : "/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated/accea87a-0a8d-11e6-87b9-3b0939e74c09/connecting/rated"
},
"path" : "/movies/96a0409a-0a8d-11e6-a3f0-6b5db3f771f8/rated/accea87a-0a8d-11e6-87b9-3b0939e74c09"
},
"rating" : "great"
} ],
"timestamp" : 1461552816852,
"duration" : 28,
"organization" : "myorg",
"applicationName" : "sandbox"
}
Answer by Kurt Googler Kanaskie · Jan 24, 2017 at 11:38 PM
If you wanted to have the relationship be intrinsic, that is, you want the collections to be related as you would expect in a traditional collections and resources API design, this works.
First create the review and then make the connection:
Then you can:
If you want to delete a review, first remove the relationship and then delete the entity: