How do I associate a firebase sub-entry to a main entry? (explanation in comments) - ios

The JSON:
{
"coffee-shops" : {
"-KJ4I4D-Jrqrzc9wP42C" : {
"coffeeShopName" : "Starbucks",
"coffeeShopRating" : 3.5,
"-KJ4VVB51wx9NpEKtjxQ" : {
"coffeeShopName" : "Starbucks",
"coffeeShopRating" : 1,
"coffeeShopReview" : "R",
"coffeeShopReviewerName" : "Charles"
},
"-KJEJ6MpQwOHcay_9k6v" : {
"coffeeShopName" : "Starbucks",
"coffeeShopRating" : 4,
"coffeeShopReview" : "B",
"coffeeShopReviewerName" : "Charles"
}
}
},
"users" : {
"02a54e06-9635-4e22-9bb7-c0ddcd9c6f4f" : {
"email" : "charles#gmail.com",
"provider" : "password",
"username" : "thecoffeeguy"
},
"03fe2c17-3c66-442c-a63d-4a1e02fd660c" : {
"email" : "test#gmail.com",
"provider" : "password",
"username" : "Charles"
},
"16a7279f-5478-4f3f-b5f8-2f261d166d92" : {
"email" : "tester#gmail.com",
"provider" : "password",
"username" : "haha"
},
"23275f65-8e16-4ede-9236-21485b7493b9" : {
"email" : "boo#gmail.com",
"provider" : "password",
"username" : "boo"
},
"a5ed6962-76bc-476a-b432-6787e45badfc" : {
"email" : "mesbekmek#gmail.com",
"provider" : "password",
"username" : "mesbekmek"
}
}
}
Some context: I'm making a coffee app and I need to have the reviews be specific to the coffee shop a user is at. Right now, all reviews that have ever been made will show up on my tableview.
This isn't so much a "how to code" question, but me wondering how to approach this and how I might solve it.
This is what I think I should do:
get a specific coffee shop's uuid
iterate over the reviews because they are sub-entries in my coffee shop model
get data from iterating over the reviews, then see if the uuid of the cell(?) selected matches the review uuid
This doesn't really sound right to me, so any help would be great.

You can organize your db in a different way.
coffee-shops:
|--coffeeId1
|--coffeeId2
|--coffeeId3
reviews:
|--coffeeId1
|----reviewId1
|----reviewId2
|--coffeeId2
|----reviewIdX
|----reviewIdXx
When you insert a review you can use the same key of the coffee-shop.
In this way all the reviews of the same coffee shop is under the same ref.
You can achieve it using something like this:
ref(reviews).child(coffeeshop.getKey()).push();

Related

Does POST /reviews-v1 using Crucible REST API allow us to add reviewers?

Is there a way to add reviewers when creating a code review through the REST API? I tried adding "reviewers" with an array of usernames but that didn't work. Got an error
Unrecognized field "reviewers" (Class com.atlassian.crucible.spi.data.ReviewData), not marked as ignorable
The request body example on their api docs is below:
{
"reviewData" : {
"projectKey" : "CR-FOO",
"name" : "Example review.",
"description" : "Description or statement of objectives for this example review.",
"author" : {
"userName" : "auth",
"displayName" : "Jane Authy",
},
"moderator" : {
"userName" : "scott",
"displayName" : "Scott the Moderator",
},
"creator" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
},
"permaId" : {
"id" : "CR-FOO-21"
},
"summary" : "some review summary.",
"state" : "Review",
"type" : "REVIEW",
"allowReviewersToJoin" : true,
"metricsVersion" : 4,
"createDate" : "2022-06-20T09:37:11.621+0000",
"dueDate" : "2022-06-23T09:37:11.621+0000",
"reminderDate" : "2022-06-21T09:37:11.621+0000",
"linkedIssues" : [ "DEF-456", "ABC-123", "GHI-789" ],
"jiraIssueKey" : "ABC-123"
},
"patch" : "Index: emptytests/notempty/a.txt\n===================================================================\ndiff -u -N -r1.31 -r1.32\n--- emptytests/notempty/a.txt\t22 Sep 2004 00:38:15 -0000\t1.31\n+++ emptytests/notempty/a.txt\t5 Dec 2004 01:04:25 -0000\t1.32\n## -4,4 +4,5 ##\n hello there :D\n CRU-123\n http://madbean.com/blog/\n-!\n\\ No newline at end of file\n+!\n+foobie\n\\ No newline at end of file\nIndex: test/a.txt\n===================================================================\ndiff -u -N -r1.31 -r1.32\n--- test/a.txt\t22 Sep 2004 00:38:15 -0000\t1.31\n+++ test/a.txt\t5 Dec 2004 01:04:25 -0000\t1.32\n## -4,4 +4,5 ##\n hello there :D\n CRU-123\n http://madbean.com/blog/\n-!\n\\ No newline at end of file\n+!\n+foobie\n\\ No newline at end of file",
"anchor" : {
"anchorPath" : "/",
"anchorRepository" : "REPO",
"stripCount" : 2
},
"changesets" : {
"changesetData" : [ {
"id" : "63452"
} ],
"repository" : "REPO"
}
}

How do I compare two nested documents using mongo_dart

Here is my database entry structure, it has a nested document called friends, I want to compare two different _id's friends list in dart using mongo_dart
{
"_id" : ObjectId("60ae06074e162995281b4666"),
"email" : "one#one.com",
"emailverified" : false,
"username" : "one#one.com",
"displayName" : "complete n00b",
"phonenumber" : "",
"dob" : "",
"points" : 0,
"friends" : [
{
"username" : "three#one.com",
"sent" : ISODate("2021-05-26T10:01:30.616Z")
},
{
"username" : "six#one.com",
"sent" : ISODate("2021-05-26T10:43:16.822Z")
}
]
}
Here is my code, but I am not getting any returns
Future<Map> commonFriends(store, myObjectId, theirObjectId) async {
var commonList = await store.aggregate([
{
'\$project': {
'friends': 1,
'commonToBoth': {
'\$setIntersection': [
{'_id': myObjectId, 'friends': '\$username'},
{'_id': theirObjectId, 'friends': '\$username'}
]
},
}
}
]);
return commonList;
}
I am getting an error from db.dart which is apart of mongo_dart package. The error is "Exception has occurred.
Map (4 items)"

firebase database filtering is not working. Need some assistance

I have a simple test database that I cant get to filter. I indexed the category in the rules:
"questions":{
".indexOn": ["category"]
},
My filter for the quiz app:
/questions.json?orderBy="category"&equalTo="Basics"&print=pretty
and my database:
"-MKoucSP33zm4jC43AnY" : {
"title" : {
"answers" : [ {
"score" : 30,
"text" : "Pineapple"
}, {
"score" : 5,
"text" : "Ham"
}, {
"score" : 20,
"text" : "Yogurt"
}, {
"score" : 10,
"text" : "Crab"
} ],
"category" : "Basics",
"questionId" : "101",
"questionImage" : "",
"questionLink" : "",
"questionText" : "What topping do you like the best on pizza?"
}
}
The category property is nested under the title node, so the property you need to order/filter on is title/category:
/questions.json?orderBy="title/category"&equalTo="Basics"&print=pretty
You'll also need to update your index definition for that path, so:
"questions": { ".indexOn": "title/category" }
Working example: https://stackoverflow.firebaseio.com/64596200/questions.json?orderBy="title/category"&equalTo="Basics"

Extracting a specific part from JSON

Hello I am trying to extract only the id value from a JSON. However if I
print(response!["id"])
result = "not created" is outputted. reponse is already in JSON format.
Is there something that I am doing wrong?
Update 1
[
{
"user" : {
"last_name" : "test",
"email" : "test#test.com",
},
"id" : 902,
"scale" : 7,
"created_at" : "2018-02-24 06:45:33",
},
{
"user" : {
"last_name" : "test",
"email" : "test#test.com",
},
"id" : 903,
"scale" : 7,
"created_at" : "2018-02-24 06:45:33",
},
{
"user" : {
"last_name" : "test",
"email" : "test#test.com",
},
"id" : 904,
"scale" : 7,
"created_at" : "2018-02-24 06:45:33",
},
]
The best way to access the id of any object from the array of response will be:
let id = response[index]["id"]
// Here index is the index of the object you want to access.
I am assuming you want to extract ids from this JSON array. You can do this using
response.map({ $0["id"]})
Which will give you another array containing only ids

FIRDatabaseQuery: how to do an inner join

I'm trying to do an inner join on a FIRDatabaseQuery object.
below is the database structure. I have some posts that are linked to post-comments. I am trying to get all the posts that a specific user added a comment on:
{
"posts" : {
"-KIycKhZU55dmKnHbbxv" : {
"author" : "John Doe",
"body" : "This is a post test",
"title" : "test",
"uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2"
},
"-KIyg_ks1O5QL4_4dfq_" : {
"author" : "Jane Doe",
"body" : "This is a post test",
"title" : "test2",
"uid" : "x5leSBGArnd10JilD9YDyNBNfZ03"
},...
}
"post-comments" : {
"-KIycKhZU55dmKnHbbxv" : {
"-KIycMyL0Vy1BHVdI4zc" : {
"author" : "toto",
"text" : "test",
"uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2"
},
"-KIyg_ks1O5QL4_4dfq_" : {
"author" : "toto",
"text" : "test",
"uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2"
}
},...
}
in SQL this will be translated into a inner join query similar to:
Select * from post inner join post-comments on post-id where post-comments.uid = "user id"
Does anybody know how o get something similar to an inner join in firebase?
Thanks a lot,
Yacine
You may want to consider
"posts" : {
"-KIycKhZU55dmKnHbbxv" : {
"author" : "John Doe",
"body" : "This is a post test",
"title" : "test",
"uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2"
"commented_by"
"SHaH36BLwgPvwgi9cDmRnsnONFB2": true
Then you can simply query for
posts.child("commented_by/uid").queryEqualToValue(true)
or, change the post-comments around to better match the queries you want to do:
"post-comments" : {
"-KIycMyL0Vy1BHVdI4zc" : {
"author" : "toto",
"post_data":
post_id: "-KIycKhZU55dmKnHbbxv",
post_title: "test"
"text" : "test",
"uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2"
},
That makes the query a snap as the post-comments node can be queried for the uid which return all of the post_id's of the posts they commented on. It wouldn't return the post itself, however, you may just be looking for the title so you can populate a list. Once the user taps it clicks it you can retrieve the actual data.
You will need to work with nested firebase calls. You can find a javascript example in this question but your swift code should look like the following:
ref.child("posts").observeEventType(.ChildAdded, withBlock: { (snapshot) in
if let postId = snapshot.key as! String {
let commentsRef = ref.child("post-comments")
commentsRef.child(postId).queryOrderedByChild("uid").queryEqualToValue(userId).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
for child in snapshot.children.allObjects as [FDataSnapshot] {
print(child.value)
}
}) { (error) in
print(error.localizedDescription)
}
}
})

Resources