Skip to content

Commit

Permalink
Merge pull request #163 from gtiosclub/feed-sameer-getUserNumResponses
Browse files Browse the repository at this point in the history
Added await getUserNumResponses() function
  • Loading branch information
jifonthespoon authored Mar 3, 2025
2 parents 5671ac0 + cdd137f commit d00bf43
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
22 changes: 21 additions & 1 deletion Gauge/Feed/ViewModels/PostFirebase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ class PostFirebase: ObservableObject {
}


func deleteComment(postId: String, commentId: String){
func deleteComment(postId: String, commentId: String) {
Firebase.db.collection("POSTS").document(postId).collection("COMMENTS").document(commentId).delete(){ error in
if let error = error{
print("Error deleting Comment: \(error)")
Expand All @@ -523,6 +523,26 @@ class PostFirebase: ObservableObject {
}
}

func getUserNumResponses(postIds: [String]) async -> Int? {
do {
var totalResponses = 0

for postId in postIds {
let documentRef = Firebase.db.collection("POSTS").document(postId).collection("RESPONSES")
let querySnapshot = try await documentRef.getDocuments()
let count = querySnapshot.documents.count
print("Number of responses under \(postId): \(count)")
totalResponses += count
}

return totalResponses
} catch {
print("Error getting responses: \(error)")
return nil
}
}


func removeView(postId: String, userId: String) {
let viewRef = Firebase.db.collection("POSTS")
.document(postId)
Expand Down
17 changes: 16 additions & 1 deletion Gauge/Feed/Views/FirebaseTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct FirebaseTesting: View {
}
}

Section("Read Data") {
Section(header: Text("Read Data")) {
Button("Get posts by userId") {
userVM.getPosts(userId: "tfeGCRCgt8UbJhCmKgNmuIFVzD73") { postIds in
self.postIds = postIds
Expand Down Expand Up @@ -139,6 +139,21 @@ struct FirebaseTesting: View {
print(object)
}
}

Button("Get number of responses for a list of posts") {
Task {
if let count = await postVM.getUserNumResponses(postIds: [
"B2A9F081-A10C-4957-A6B8-0295F0C700A2",
"examplePost"
]) {
print("Number of responses: \(count)")
} else {
print("Failed to get number of responses")
}
}
}


}

Section("Update Data") {
Expand Down

0 comments on commit d00bf43

Please sign in to comment.