After reading the documentation they recommend not using nested data structure. I am thinking this is the best way for my data but I am not sure how to add the tripId to the hotels collection.
My other option is having the user enter the "BOL" for each hotel entry... My problem with that method is how do I query the date to see all the hotels that match the "trip"?
users:
- gCFiwSLwBeg87N6GfeofFxIAZyi2
- UID: gCFiwSLwBeg87N6GfeofFxIAZyi2
- firstName: "James"
- email: "
[email protected]"
trips:
- "random document id"
- bol: "1234567"
- startDate: "1/3/2022"
- destination: "Sacramento, CA"
- userID: "Users UID"
- foodTotal: "100"
- hotelTotal: "240"
- "random document id"
- bol: "256976"
- startDate: "2/5/2022"
- destination: "Orlando, FL"
- userID: "Users UID"
- foodTotal: "150"
- hotelTotal: "400"
hotels:
- "random document id"
- tripId: ?
- date: "1/4/2022"
- destination: "Sacramento, CA"
- userID: "Users UID"
- name: "Hotel Inn"
- cost: "120"
- "random document id"
- tripId: ?
- date: "1/5/2022"
- destination: "Sacramento, CA"
- userID: "Users UID"
- name: "Hotel Inn"
- cost: "120"
Here is the function I am currently using to add Hotels to firebase.
class FirebaseHotelRepository: HotelRepositoryProtocol {
private let db = Firestore.firestore()
func addHotel(hotel: Hotel, completion: @escaping (Result) -> Void) {
do {
var addedHotel = hotel
addedHotel.userId = Auth.auth().currentUser?.uid
let _ = try db.collection("hotels").addDocument(from: addedHotel)
} catch {
fatalError("Unable to encode trip: \(error.localizedDescription)")
}
}
}
class AddHotelViewModel: ObservableObject {
private let repo: HotelRepositoryProtocol
var name: String = ""
var location: String = ""
var date = Date()
var cost: String = ""
@Published var saved: Bool = false
init(repo: HotelRepositoryProtocol) {
self.repo = repo
}
func addHotel() {
let hotel = Hotel(bol: bol,
name: name,
location: location,
date: date,
cost: cost,
color: UIColor(color).hexStringFromColor())
repo.addHotel(hotel: hotel) { result in
switch result {
case .success(let savedHotel):
DispatchQueue.main.async {
self.saved = savedHotel == nil ? false : true
}
case .failure(let error):
print(error.localizedDescription)
}
}
}
}
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)