[英]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.