add structure
This commit is contained in:
86
lib/model/reg_model.dart
Normal file
86
lib/model/reg_model.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:path/path.dart' as Path;
|
||||
import 'package:fcs/vo/buyer.dart';
|
||||
|
||||
import 'base_model.dart';
|
||||
import 'constants.dart';
|
||||
import 'firebase_helper.dart';
|
||||
|
||||
class Attachments {
|
||||
File nricFront, nricBack;
|
||||
}
|
||||
|
||||
class RegModel extends BaseModel {
|
||||
Buyer reg = Buyer();
|
||||
bool isLoaded = false;
|
||||
|
||||
StreamSubscription regListener;
|
||||
|
||||
void initUser(user) {
|
||||
super.initUser(user);
|
||||
if (user.isRegisteredBuyer()) {
|
||||
_loadReg();
|
||||
} else {
|
||||
reg = Buyer();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
if (regListener != null) await regListener.cancel();
|
||||
reg = Buyer();
|
||||
}
|
||||
|
||||
Future<void> _loadReg() async {
|
||||
if (regListener != null) {
|
||||
regListener.cancel();
|
||||
}
|
||||
regListener = getDocSnapshot(
|
||||
"/$biz_collection/${setting.okEnergyId}/$buyer_collection",
|
||||
"${user.docID}")
|
||||
.listen((snap) async {
|
||||
if (snap.exists) {
|
||||
reg = Buyer.fromMap(snap.data, snap.documentID);
|
||||
QuerySnapshot q = await getSnapshot(
|
||||
"/$biz_collection/${setting.okEnergyId}/$buyer_collection/${user.docID}/$product_collection");
|
||||
reg.buyerProducts.clear();
|
||||
q.documents.forEach((d) {
|
||||
reg.buyerProducts.add(BuyerProduct.fromMap(d.data, d.documentID));
|
||||
});
|
||||
} else {
|
||||
reg = Buyer();
|
||||
}
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> register(Buyer buyer, Attachments attachments) async {
|
||||
String path = Path.join(reg_files_path, user.docID);
|
||||
String urlFront = await uploadStorage(path, attachments.nricFront);
|
||||
buyer.nricFrontUrl = urlFront;
|
||||
String urlBack = await uploadStorage(path, attachments.nricBack);
|
||||
buyer.nricBackUrl = urlBack;
|
||||
|
||||
await request("/reg", "POST",
|
||||
payload: buyer.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> update(Buyer buyer, Attachments attachments) async {
|
||||
String path = Path.join(reg_files_path, user.docID);
|
||||
if (attachments.nricFront != null) {
|
||||
String urlFront = await uploadStorage(path, attachments.nricFront);
|
||||
buyer.nricFrontUrl = urlFront;
|
||||
}
|
||||
if (attachments.nricBack != null) {
|
||||
String urlBack = await uploadStorage(path, attachments.nricBack);
|
||||
buyer.nricBackUrl = urlBack;
|
||||
}
|
||||
|
||||
await request("/buyer/update", "PUT",
|
||||
payload: buyer.toMap(), token: await getToken());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user