User make appointments in a clinic.I want to get the appointment time of the last user, which is in the top document in my firestore database. Am able to get it but if I delete the top document and make another appointment it uses the time of the deleted document not the new top document. If there is a better way to do it please share.
Collection col = firestore.collection("Clinic Name");
//Get 1 document the top one
Query query = col.orderBy("Time",Query.Direction.DESCENDING).limit(1);
query.addSnapshotListener(MetadataChanges.INCLUDE, new
EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots,
@javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
return;
}
if (queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()) {
for (DocumentSnapshot dc : queryDocumentSnapshots) {
String prevTime = dc.getString("Time");
//Add the data
AddAppointment(prevTime);
}
}
//Function that add the data
void AddAppointment(String time){
if(time.isEmpty){
time = "08:00:00";
}
LocalTime localTime = LocalTime.parse(time);
localTime = String.valueOf(localTime.plusMinutes(8));
Map<String, Object> data = new HashMap<>();
data.put("Time",localTime);
//more data ...
//Adding the document
firebase.collection("ClinicName").document("userId").set(data)...
}