![](/img/trans.png)
[英]How to check if Cloud Firestore collection exists? ( querysnapshot)
[英]Trying to add a check before accepting a document into a collection Cloud Firestore
我正在構建一個qr出勤掃描儀,希望查看掃描是否在我的session
集合的start
時間和end
時間字段的范圍內。 如果掃描在兩次之間進行,則應接受,否則將不接受,並顯示祝酒詞。 目前,我正在檢查會話是否僅存在,而在開始時間和結束時間字段之間不存在。 rawData是從QR碼中提取的sessionID
,提取后將檢查其是否為有效的sessionID。 現在,我只需要檢查掃描時間。 任何幫助都會很棒:
private void barcodeRecognition(Bitmap photo) {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(photo);
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
.getVisionBarcodeDetector();
Task<List<FirebaseVisionBarcode>> result = detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
@Override
public void onSuccess(List<FirebaseVisionBarcode> barcodes) {
for (FirebaseVisionBarcode barcode: barcodes) {
Rect bounds = barcode.getBoundingBox();
Point[] corners = barcode.getCornerPoints();
final String rawValue = barcode.getRawValue();
int valueType = barcode.getValueType();
mAuth.getCurrentUser();
// TRY LINKING TO userUID on Attendance on Firebase console
FirebaseUser currentUser = mAuth.getCurrentUser();
final String user = currentUser.getUid(); // USED TO GET CURRENT USER UID OF PERSON LOGGED IN
//THIS CAN BE SET TO .getUserName() or getEmail() depending on preference
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
final String time = format.format(calendar.getTime());
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference docRef = rootRef.collection("Session").document(rawValue);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if (document.exists()){
Attendance attendance = new Attendance(rawValue,user,time);
attendanceRef3.add(attendance);
Toast.makeText(StudentAccount.this, "Your attendance has been recorded", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(StudentAccount.this, "Session is not available", Toast.LENGTH_SHORT).show();
}
}
}
});
更新的代碼
final FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
final DocumentReference docRef = rootRef.collection("Session").document(rawValue);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if (document.exists()){
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("Session")
.whereGreaterThanOrEqualTo("startTime", calendar.getTime())
.whereLessThan("endTime", calendar.getTime());
Attendance attendance = new Attendance(rawValue,user,time);
attendanceRef3.add(attendance);
Toast.makeText(StudentAccount.this, "Your attendance has been recorded", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(StudentAccount.this, "Session is not available", Toast.LENGTH_SHORT).show();
}
}
}
});
編輯代碼
private void barcodeRecognition(Bitmap photo) {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(photo);
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
.getVisionBarcodeDetector();
Task<List<FirebaseVisionBarcode>> result = detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
@Override
public void onSuccess(List<FirebaseVisionBarcode> barcodes) {
for (FirebaseVisionBarcode barcode: barcodes) {
Rect bounds = barcode.getBoundingBox();
Point[] corners = barcode.getCornerPoints();
final String rawValue = barcode.getRawValue();
int valueType = barcode.getValueType();
mAuth.getCurrentUser();
// TRY LINKING TO userUID on Attendance on Firebase console
FirebaseUser currentUser = mAuth.getCurrentUser();
final String user = currentUser.getUid(); // USED TO GET CURRENT USER UID OF PERSON LOGGED IN
//THIS CAN BE SET TO .getUserName() or getEmail() depending on preference
final Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
final String time = format.format(calendar.getTime());
final FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
final DocumentReference docRef = rootRef.collection("Session").document(rawValue);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Date timeToCheck = calendar.getTime();
Session session = document.toObject(Session.class);
Date startTime = session.getStartTime();
Date endTime = session.getEndTime();
if (timeToCheck.after(startTime) && (timeToCheck.before(endTime))) {
Attendance attendance = new Attendance(rawValue, user, time);
attendanceRef3.add(attendance);
Toast.makeText(StudentAccount.this, "Your attendance has been recorded", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(StudentAccount.this, "Your attendance attempt is too late", Toast.LENGTH_SHORT).show();
}
}
}
});
根據您的數據庫結構,要根據startTime
和endTime
屬性的限制過濾數據,請使用以下查詢:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("session")
.whereGreaterThanOrEqualTo("startTime", timeToCheck)
.whereLessThanOrEqualTo("endTime", timeToCheck);
如果不想相等,還可以使用以下代碼:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("session")
.whereGreaterThan("startTime", timeToCheck)
.whereLessThan("endTime", timeToCheck);
因此,使用此代碼,您將可以獲得在會話集合的開始時間和結束時間字段范圍內的所有session
對象。
編輯:
如果要檢查單個文檔,則需要使用另一個if語句。 因此,在您的實際if (document.exists()){
創建另一個如下所示的代碼:
Attendance attendance = document.toObject(Attendance.class);
Date startTime = attendance.getStartTime();
Date endTime = attendance.getEndTime();
if(timeToCheck.after(startTime) && (timeToCheck.before(endTime)) {
//Do what you need to do
}
其中timeToCheck
是Date
類型的對象,您需要根據數據庫中的實際日期進行檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.