Click here to Skip to main content
14,926,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am using firebase back end as a service for my app, where I am simply creating a collection named "Restaurant" inside of that collection, I am storing all my Restaurant data, and for accessing each document I am using a PIN, which I the custom id of the collection set by the user while adding the data, everything works fine.
Now the problem I am facing is, I am not sure how to check if the PIN of restaurant data which is my custom document id is already present in a database or not.
In my case what happened is I have store data with custom token id 4444 and now some other users pick up the same custom token id while registration process. In that case both the collection data getting merge together.
So how can I check if there is already a document present with this collection id? thank you for your help.
Below is my code


What I have tried:

private void ValidateProductData() {
RestaurantName = InputProductName.getText().toString();
Description = InputProductPrice.getText().toString();
Price = InputProductDesc.getText().toString();
COP = Nopbefore.getText().toString();
SOP = Nopafter.getText().toString();
uidS = uid.getText().toString();
NopTotalS=NopTotal.getText().toString();
if (ImageUri == null) {

Picasso.get()
.load(ImageUri)
.resize(640, 480)
.centerInside()
.placeholder(R.drawable.ic_baseline_photo_camera_24)
.error(R.drawable.ic_baseline_photo_camera_24)
.into(InputProductImage);
// Toast.makeText(this, "Please Update Restaurant logo is mandatory", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(RestaurantName)) {
Toast.makeText(this, "Please , Enter restaurant name", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(Description)) {
Toast.makeText(this, "Please , Enter restaurant address", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(Price)) {
Toast.makeText(this, "Please , Enter restaurant phone number", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(uidS)) {
Toast.makeText(this, "Please Re-Enter , Enter CustomID Token", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(COP)) {
Toast.makeText(this, "Please , Enter Capacity no. pre-covid", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(SOP)) {
Toast.makeText(this, "Please , Enter Capacity no. after SOP", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(NopTotalS)) {
Toast.makeText(this, "Please , Enter Capacity no. after SOP", Toast.LENGTH_SHORT).show();
} else {
StoreProductInfo();
}
pin.setText("Pin Changed Successfully!");
}
private void StoreProductInfo() {
mProProgress.setTitle("Adding New Product");
mProProgress.setMessage("Please wait while we are adding new product !");
mProProgress.setCanceledOnTouchOutside(false);
mProProgress.show();
Calendar calendar = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("MMM dd, yyyy");
saveCurrentDate = currentDate.format(calendar.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(calendar.getTime());
customId = uidS;
final StorageReference filepath = ProductImageRefs.child(ImageUri.getLastPathSegment() + customId + ".jpg");
final UploadTask uploadTask = filepath.putFile(ImageUri);
uploadTask.addOnFailureListener(new OnFailureListener() {
u/Override
public void onFailure(@NonNull Exception e) {
String message = e.toString();
Toast.makeText(MainActivity.this, "Error:" + message, Toast.LENGTH_SHORT).show();
mProProgress.dismiss();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
u/Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Product Image Uploaded Successfully", Toast.LENGTH_SHORT).show();
Task<Uri> uriTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
u/Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
downloadImageUrl = filepath.getDownloadUrl().toString();
return filepath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
u/Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
downloadImageUrl = task.getResult().toString();
Toast.makeText(MainActivity.this, "got Product Image Saved to Database Successfully", Toast.LENGTH_SHORT).show();
storeData();
}
}
});
}
});
}
private void storeData() {
HashMap<String, Object> productMap = new HashMap<>();
productMap.put("image", downloadImageUrl);
productMap.put("PName", RestaurantName);
productMap.put("Address", Price);
productMap.put("description", Description);
productMap.put("Pid", customId);
productMap.put("date", saveCurrentDate);
productMap.put("time", saveCurrentTime);
productMap.put("NOTotal",NopTotalS);
productMap.put("ContactTracing","No");
productMap.put("SanitFreqs","0");
productMap.put("MaskEmp","Yes");
productMap.put("Nopive","0");
productMap.put("NOPBefore",COP);
productMap.put("NOPPresent",SOP);
productMap.put("search",RestaurantName.toLowerCase());
productMap.put("contactless","No");
Paper.book().write(Prevalent.Id,customId);
String secondS="true";
Paper.book().write(Prevalent.second,secondS);
firebaseFirestore.collection("Restaurants").document(customId).set(productMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
if (customId.equals(uidS)){
Log.d(TAG,"TAKEN");
Toast.makeText(MainActivity.this, "PIN TAKEN", Toast.LENGTH_SHORT).show();
}
mProProgress.dismiss();
Toast.makeText(MainActivity.this, "Restaurant Data is Stored Successfully", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(MainActivity.this, Adminhomepage.class);
startActivity(mainIntent);
finish();
} else {

String error = task.getException().getMessage();
Toast.makeText(MainActivity.this, "(FIRESTORE Error) : " + error, Toast.LENGTH_LONG).show();
}
mProProgress.dismiss();
}
})
};
Posted
Comments
Gerry Schmitz 4-Mar-21 16:15pm
   
"You" issue document id's; the customer doesn't get to determine what it is. If it's an "alias" you need, then it's just a property of the customer, and not something you build a system around.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900