Reputation: 59
So I created a booking app which sends data into booking collection taking userID as Document Name. But when the user books again it overwrites the previous booking userID document. What I want to do is: 1) Implement autoincrement on userID, and also 2) display the autoincremented booking too (Notice I'm using userID because I want to keep it unique to that user only). 3) I also want to implement a limit on booking if someone can help with that.
The code to send data into BOOKING COLLECTION and creating DOCUMENT with userID:
public class bookingpage extends AppCompatActivity {
EditText mFirstnamelastname,mMobnum,mPincode,mFlatno,mArea,mLandmark,mTown,mState;
Button mBook;
String userID;
FirebaseAuth fAuth;
FirebaseFirestore fstore;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookingpage);
//pickup
mFirstnamelastname=findViewById(R.id.firstlastname);
mMobnum=findViewById(R.id.mobnum);
mPincode=findViewById(R.id.pincode);
mFlatno=findViewById(R.id.flatno);
mArea=findViewById(R.id.area);
mLandmark=findViewById(R.id.landmark);
mTown=findViewById(R.id.town);
mState=findViewById(R.id.state);
mBook=findViewById(R.id.editbook);
progressBar=findViewById(R.id.progressBar4);
fAuth=FirebaseAuth.getInstance();
fstore=FirebaseFirestore.getInstance();
mBook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//pickup
String firstname = mFirstnamelastname.getText().toString().trim();
String phoneno = mMobnum.getText().toString().trim();
String pincode = mPincode.getText().toString().trim();
String flatno = mFlatno.getText().toString().trim();
String area = mArea.getText().toString().trim();
String landmark = mLandmark.getText().toString().trim();
String town = mTown.getText().toString().trim();
String state = mState.getText().toString().trim();
progressBar.setVisibility(View.VISIBLE);
//saving data
userID=fAuth.getCurrentUser().getUid();
//creating a document reference creating a collection booking and making a new doc using user id
DocumentReference documentReference = fstore.collection("Booking").document(userID);
//creating a hashmap to send data
Map<String,Object> book = new HashMap<>();
//setting status
book.put("Status -","Active");
//pickup
book.put("a1 - Fullname",firstname);
book.put("a2 - PhoneNo",phoneno);
book.put("a3 - Pincode",pincode);
book.put("a4 - Flatno",flatno);
book.put("a5 - Area",area);
book.put("a6 - Landmark",landmark);
book.put("a7 - Town",town);
book.put("a8 - State",state);
//using the document reference to set user document
documentReference.set(book).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(bookingpage.this, "Booking Successful", Toast.LENGTH_SHORT).show();
Log.d("Tag","onSuccess: Successfully booked for "+ userID);
startActivity(new Intent(getApplicationContext(),MainActivity2.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(bookingpage.this, "Error!" + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
progressBar.setVisibility(View.INVISIBLE);
}
});
}
}
The code to display the booking collection's document with userID:
final DocumentReference documentReference = fstore.collection("Booking").document(userID);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
//putting if else fixed crashing
if (e != null) {
Log.d("Tag", "Error:" + e.getMessage());
} else {
mStatus.setText(documentSnapshot.getString("Status -"));
mFirstnamelastname.setText(documentSnapshot.getString("a1 - Fullname"));
mMobnum.setText(documentSnapshot.getString("a2 - PhoneNo"));
mPincode.setText(documentSnapshot.getString("a3 - Pincode"));
mFlatno.setText(documentSnapshot.getString("a4 - Flatno"));
mArea.setText(documentSnapshot.getString("a5 - Area"));
mLandmark.setText(documentSnapshot.getString("a6 - Landmark"));
mTown.setText(documentSnapshot.getString("a7 - Town"));
mState.setText(documentSnapshot.getString("a8 - State"));
}
}
});
I'm a beginner please try to explain your answer so that I can understand & learn more :)
Upvotes: 0
Views: 134
Reputation: 1236
Well... you just shouldn't use the UserId's as document names in the Booking collection. It's just goes against logic and best-practices.
You should instead let Firestore create a BookingId. It would be your current booking document + a new field (String) holding the UserId of the user who made the booking.
This would be a more logical (and scalable) way.
To limit the number of bookings, you could add a field in your UserId documents (in users collection), called bookingCount (Integer). Each time a User books, check if the bookingCount >= bookingLimit (arbitrary value of your choosing).
If bookingCount < bookingLimit, then allow them to book and increment the bookingCount by 1.
Upvotes: 1