Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added await getUserNumResponses() function #163

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Gauge/Feed/ViewModels/PostFirebase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,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 @@ -517,6 +517,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