-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 71 additions & 71 deletions
142
HappyAnding/HappyAnding/Repository/SearchRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,74 @@ | ||
//// | ||
//// SearchRepository.swift | ||
//// HappyAnding | ||
//// | ||
//// Created by 임정욱 on 5/2/24. | ||
//// | ||
// | ||
// SearchRepository.swift | ||
// HappyAnding | ||
//import Foundation | ||
//import FirebaseCore | ||
//import FirebaseFirestore | ||
// | ||
// Created by 임정욱 on 5/2/24. | ||
// | ||
|
||
import Foundation | ||
import FirebaseCore | ||
import FirebaseFirestore | ||
|
||
|
||
class SearchRepository { | ||
|
||
|
||
private let db = Firestore.firestore() | ||
private let postCollection: String = "Post" | ||
private let shortcutCollection: String = "Shortcut" | ||
|
||
|
||
|
||
public func searchContentAndShortcuts(keyword: String, completion: @escaping ([[Any]]) -> Void) { | ||
let db = Firestore.firestore() | ||
let postsRef = db.collection(postCollection) | ||
let shortcutsRef = db.collection(shortcutCollection) | ||
|
||
let group = DispatchGroup() | ||
var postsResults = [Post]() | ||
var shortcutsResults = [Shortcuts]() | ||
var errors: [Error] = [] | ||
|
||
let searchFieldsShortcuts = ["title", "subtitle", "description"] | ||
for field in searchFieldsShortcuts { | ||
group.enter() | ||
shortcutsRef.whereField(field, arrayContains: keyword).getDocuments { (snapshot, error) in | ||
defer { group.leave() } | ||
if let snapshot = snapshot { | ||
shortcutsResults += snapshot.documents.compactMap { document -> Shortcuts? in | ||
try? document.data(as: Shortcuts.self) | ||
} | ||
} else if let error = error { | ||
errors.append(error) | ||
} | ||
} | ||
} | ||
|
||
let searchFieldsPosts = ["title", "content"] | ||
for field in searchFieldsPosts { | ||
group.enter() | ||
postsRef.whereField(field, arrayContains: keyword).getDocuments { (snapshot, error) in | ||
defer { group.leave() } | ||
if let snapshot = snapshot { | ||
postsResults += snapshot.documents.compactMap { document -> Post? in | ||
try? document.data(as: Post.self) | ||
} | ||
} else if let error = error { | ||
errors.append(error) | ||
} | ||
} | ||
} | ||
|
||
group.notify(queue: .main) { | ||
if errors.isEmpty { | ||
let combinedResults = [shortcutsResults as [Any], postsResults as [Any]] | ||
completion(combinedResults) | ||
} else { | ||
completion([]) | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
//class SearchRepository { | ||
// | ||
// | ||
// private let db = Firestore.firestore() | ||
// private let postCollection: String = "Post" | ||
// private let shortcutCollection: String = "Shortcut" | ||
// | ||
// | ||
// | ||
// public func searchContentAndShortcuts(keyword: String, completion: @escaping ([[Any]]) -> Void) { | ||
// let db = Firestore.firestore() | ||
// let postsRef = db.collection(postCollection) | ||
// let shortcutsRef = db.collection(shortcutCollection) | ||
// | ||
// let group = DispatchGroup() | ||
// var postsResults = [Post]() | ||
// var shortcutsResults = [Shortcuts]() | ||
// var errors: [Error] = [] | ||
// | ||
// let searchFieldsShortcuts = ["title", "subtitle", "description"] | ||
// for field in searchFieldsShortcuts { | ||
// group.enter() | ||
// shortcutsRef.whereField(field, arrayContains: keyword).getDocuments { (snapshot, error) in | ||
// defer { group.leave() } | ||
// if let snapshot = snapshot { | ||
// shortcutsResults += snapshot.documents.compactMap { document -> Shortcuts? in | ||
// try? document.data(as: Shortcuts.self) | ||
// } | ||
// } else if let error = error { | ||
// errors.append(error) | ||
// } | ||
// } | ||
// } | ||
// | ||
// let searchFieldsPosts = ["title", "content"] | ||
// for field in searchFieldsPosts { | ||
// group.enter() | ||
// postsRef.whereField(field, arrayContains: keyword).getDocuments { (snapshot, error) in | ||
// defer { group.leave() } | ||
// if let snapshot = snapshot { | ||
// postsResults += snapshot.documents.compactMap { document -> Post? in | ||
// try? document.data(as: Post.self) | ||
// } | ||
// } else if let error = error { | ||
// errors.append(error) | ||
// } | ||
// } | ||
// } | ||
// | ||
// group.notify(queue: .main) { | ||
// if errors.isEmpty { | ||
// let combinedResults = [shortcutsResults as [Any], postsResults as [Any]] | ||
// completion(combinedResults) | ||
// } else { | ||
// completion([]) | ||
// } | ||
// } | ||
// } | ||
// | ||
// | ||
// | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters