0

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)...
      }
  • How do you add another document to Firestore? – Alex Mamo Mar 18 '19 at 13:03
  • You say "it uses the time of the deleted document". Where are you adding that "time"? – Alex Mamo Mar 18 '19 at 13:16
  • @AlexMamo I have updated the question I included the function where I am adding the time – Uncle Marvin Mar 18 '19 at 13:28
  • **[This](https://stackoverflow.com/questions/43584244/how-to-save-the-current-date-time-when-i-add-new-value-to-firebase-realtime-data)** is a correct way to add a timestamp to a Firestore database. – Alex Mamo Mar 18 '19 at 13:31
  • @AlexMamo thanks for that. My problem is if I delete the document with the current timestamp and make another appointment it will get the timestamp of the deleted document, because I use the previous timestamp to make the next appointment – Uncle Marvin Mar 18 '19 at 13:38

0 Answers0