add rate service
This commit is contained in:
@@ -132,6 +132,8 @@ class AuthFb {
|
||||
String privileges = idToken.claims["pr"];
|
||||
if (privileges != null && privileges != "") {
|
||||
user.privileges = privileges.split(":").toList();
|
||||
} else {
|
||||
user.privileges = [];
|
||||
}
|
||||
controller.add(user);
|
||||
}
|
||||
@@ -198,18 +200,6 @@ class AuthFb {
|
||||
return token.token;
|
||||
}
|
||||
|
||||
Future<Setting> getSetting() async {
|
||||
var snap = await Firestore.instance
|
||||
.collection(config_collection)
|
||||
.document(setting_doc_id)
|
||||
.get();
|
||||
if (!snap.exists) {
|
||||
return null;
|
||||
}
|
||||
// _listSetting();
|
||||
return Setting.fromMap(snap.data);
|
||||
}
|
||||
|
||||
Stream<Setting> settings() async* {
|
||||
Stream<DocumentSnapshot> snapshot = Firestore.instance
|
||||
.collection(config_collection)
|
||||
|
||||
181
lib/data/provider/rate_data_provider.dart
Normal file
181
lib/data/provider/rate_data_provider.dart
Normal file
@@ -0,0 +1,181 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:fcs/domain/constants.dart';
|
||||
import 'package:fcs/domain/entities/cargo_type.dart';
|
||||
import 'package:fcs/domain/entities/custom_duty.dart';
|
||||
import 'package:fcs/domain/entities/discount_by_weight.dart';
|
||||
import 'package:fcs/domain/entities/rate.dart';
|
||||
import 'package:fcs/helpers/api_helper.dart';
|
||||
import 'package:fcs/helpers/firebase_helper.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
class RateDataProvider {
|
||||
final log = Logger('RateDataProvider');
|
||||
|
||||
static final RateDataProvider instance = RateDataProvider._();
|
||||
RateDataProvider._();
|
||||
|
||||
StreamController<Rate> controller;
|
||||
static Rate _rate = Rate();
|
||||
|
||||
Stream<Rate> _rateStream() async* {
|
||||
Stream<DocumentSnapshot> snapshot = Firestore.instance
|
||||
.collection(config_collection)
|
||||
.document(rate_doc_id)
|
||||
.snapshots();
|
||||
await for (var snap in snapshot) {
|
||||
Rate rate = Rate.fromMap(snap.data);
|
||||
yield rate;
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<CargoType>> _cargoTypeStream() async* {
|
||||
List<CargoType> cargoTypes = [];
|
||||
Stream<QuerySnapshot> snapshots = Firestore.instance
|
||||
.collection(config_collection)
|
||||
.document(rate_doc_id)
|
||||
.collection(cargo_types_collection)
|
||||
.snapshots();
|
||||
|
||||
await for (var snaps in snapshots) {
|
||||
cargoTypes = [];
|
||||
cargoTypes = snaps.documents.map((snap) {
|
||||
return CargoType.fromMap(snap.data, snap.documentID);
|
||||
}).toList();
|
||||
|
||||
yield cargoTypes;
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<CustomDuty>> _customDutiesStream() async* {
|
||||
List<CustomDuty> customDuries = [];
|
||||
Stream<QuerySnapshot> snapshots = Firestore.instance
|
||||
.collection(config_collection)
|
||||
.document(rate_doc_id)
|
||||
.collection(custom_duties_collection)
|
||||
.snapshots();
|
||||
|
||||
await for (var snaps in snapshots) {
|
||||
customDuries = [];
|
||||
customDuries = snaps.documents.map((snap) {
|
||||
return CustomDuty.fromMap(snap.data, snap.documentID);
|
||||
}).toList();
|
||||
yield customDuries;
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<DiscountByWeight>> _discountByWeightStream() async* {
|
||||
List<DiscountByWeight> discountByWeight = [];
|
||||
Stream<QuerySnapshot> snapshots = Firestore.instance
|
||||
.collection(config_collection)
|
||||
.document(rate_doc_id)
|
||||
.collection(discounts_by_weights_collection)
|
||||
.snapshots();
|
||||
|
||||
await for (var snaps in snapshots) {
|
||||
discountByWeight = [];
|
||||
discountByWeight = snaps.documents.map((snap) {
|
||||
return DiscountByWeight.fromMap(snap.data, snap.documentID);
|
||||
}).toList();
|
||||
yield discountByWeight;
|
||||
}
|
||||
}
|
||||
|
||||
StreamSubscription<Rate> rateListener;
|
||||
StreamSubscription<List<CargoType>> cargoListener;
|
||||
StreamSubscription<List<CustomDuty>> customListener;
|
||||
StreamSubscription<List<DiscountByWeight>> discountListener;
|
||||
Stream<Rate> rate() {
|
||||
Future<void> _start() async {
|
||||
rateListener = _rateStream().listen((rate) {
|
||||
_rate.deliveryFee = rate.deliveryFee;
|
||||
_rate.freeDeliveryWeight = rate.freeDeliveryWeight;
|
||||
_rate.volumetricRatio = rate.volumetricRatio;
|
||||
controller.add(_rate);
|
||||
});
|
||||
cargoListener = _cargoTypeStream().listen((cargoTypes) {
|
||||
_rate.cargoTypes = cargoTypes;
|
||||
controller.add(_rate);
|
||||
});
|
||||
customListener = _customDutiesStream().listen((customDuties) {
|
||||
_rate.customDuties = customDuties;
|
||||
controller.add(_rate);
|
||||
});
|
||||
discountListener = _discountByWeightStream().listen((discountByWeights) {
|
||||
_rate.discountByWeights = discountByWeights;
|
||||
controller.add(_rate);
|
||||
});
|
||||
}
|
||||
|
||||
void _stop() {
|
||||
if (rateListener != null) {
|
||||
rateListener.cancel();
|
||||
}
|
||||
if (cargoListener != null) {
|
||||
cargoListener.cancel();
|
||||
}
|
||||
if (customListener != null) {
|
||||
customListener.cancel();
|
||||
}
|
||||
if (discountListener != null) {
|
||||
discountListener.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
controller = StreamController<Rate>(
|
||||
onListen: _start, onPause: _stop, onResume: _start, onCancel: _stop);
|
||||
|
||||
return controller.stream;
|
||||
}
|
||||
|
||||
Future<void> updateRates(Rate rate) async {
|
||||
return await requestAPI("/rates", "PUT",
|
||||
payload: rate.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> createCargoType(CargoType cargoType) async {
|
||||
return await requestAPI("/cargo_types", "POST",
|
||||
payload: cargoType.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> updateCargoType(CargoType cargoType) async {
|
||||
return await requestAPI("/cargo_types", "PUT",
|
||||
payload: cargoType.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> deleteCargoType(String id) async {
|
||||
return await requestAPI("/cargo_types", "DELETE",
|
||||
payload: {"id": id}, token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> createCustomDuty(CustomDuty customDuty) async {
|
||||
return await requestAPI("/custom_duties", "POST",
|
||||
payload: customDuty.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> updateCustomDuty(CustomDuty customDuty) async {
|
||||
return await requestAPI("/custom_duties", "PUT",
|
||||
payload: customDuty.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> deleteCustomDuty(String id) async {
|
||||
return await requestAPI("/custom_duties", "PUT",
|
||||
payload: {"id": id}, token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> createDiscountByWeight(DiscountByWeight discountByWeight) async {
|
||||
return await requestAPI("/discounts_by_weight", "POST",
|
||||
payload: discountByWeight.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> updateDiscountByWeight(DiscountByWeight discountByWeight) async {
|
||||
return await requestAPI("/discounts_by_weight", "PUT",
|
||||
payload: discountByWeight.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> deleteDiscountByWeight(String id) async {
|
||||
return await requestAPI("/discounts_by_weight", "PUT",
|
||||
payload: {"id": id}, token: await getToken());
|
||||
}
|
||||
}
|
||||
75
lib/data/services/rate_imp.dart
Normal file
75
lib/data/services/rate_imp.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
import 'package:fcs/data/provider/rate_data_provider.dart';
|
||||
import 'package:fcs/domain/entities/cargo_type.dart';
|
||||
import 'package:fcs/domain/entities/connectivity.dart';
|
||||
import 'package:fcs/domain/entities/discount_by_weight.dart';
|
||||
import 'package:fcs/domain/entities/custom_duty.dart';
|
||||
import 'package:fcs/domain/entities/rate.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'rate_service.dart';
|
||||
|
||||
class RateServiceImp implements RateService {
|
||||
RateServiceImp({
|
||||
@required this.rateDataProvider,
|
||||
@required this.connectivity,
|
||||
});
|
||||
|
||||
final Connectivity connectivity;
|
||||
final RateDataProvider rateDataProvider;
|
||||
|
||||
@override
|
||||
Stream<Rate> getRateStream() {
|
||||
return rateDataProvider.rate();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createCargoType(CargoType cargoType) {
|
||||
return rateDataProvider.createCargoType(cargoType);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createCustomDuty(CustomDuty customDuty) {
|
||||
return rateDataProvider.createCustomDuty(customDuty);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createDiscountByWeight(DiscountByWeight discountByWeight) {
|
||||
return rateDataProvider.createDiscountByWeight(discountByWeight);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteCargoType(String id) {
|
||||
return rateDataProvider.deleteCargoType(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteCustomDuty(String id) {
|
||||
return rateDataProvider.deleteCustomDuty(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteDiscountByWeight(String id) {
|
||||
return rateDataProvider.deleteDiscountByWeight(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateCargoType(CargoType cargoType) {
|
||||
return rateDataProvider.updateCargoType(cargoType);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateCustomDuty(CustomDuty customDuty) {
|
||||
return rateDataProvider.updateCustomDuty(customDuty);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateDiscountByWeight(DiscountByWeight discountByWeight) {
|
||||
return rateDataProvider.updateDiscountByWeight(discountByWeight);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateRate(Rate rate) {
|
||||
// TODO: implement updateRate
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
22
lib/data/services/rate_service.dart
Normal file
22
lib/data/services/rate_service.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
import 'package:fcs/domain/entities/cargo_type.dart';
|
||||
import 'package:fcs/domain/entities/custom_duty.dart';
|
||||
import 'package:fcs/domain/entities/discount_by_weight.dart';
|
||||
import 'package:fcs/domain/entities/rate.dart';
|
||||
|
||||
abstract class RateService {
|
||||
Stream<Rate> getRateStream();
|
||||
|
||||
Future<void> updateRate(Rate rate);
|
||||
|
||||
Future<void> createCargoType(CargoType cargoType);
|
||||
Future<void> updateCargoType(CargoType cargoType);
|
||||
Future<void> deleteCargoType(String id);
|
||||
|
||||
Future<void> createCustomDuty(CustomDuty customDuty);
|
||||
Future<void> updateCustomDuty(CustomDuty customDuty);
|
||||
Future<void> deleteCustomDuty(String id);
|
||||
|
||||
Future<void> createDiscountByWeight(DiscountByWeight discountByWeight);
|
||||
Future<void> updateDiscountByWeight(DiscountByWeight discountByWeight);
|
||||
Future<void> deleteDiscountByWeight(String id);
|
||||
}
|
||||
@@ -3,12 +3,14 @@ import 'package:fcs/data/provider/common_data_provider.dart';
|
||||
import 'package:fcs/data/provider/delivery_address_data_provider.dart';
|
||||
import 'package:fcs/data/provider/fcs_shipment_data_provider.dart';
|
||||
import 'package:fcs/data/provider/package_data_provider.dart';
|
||||
import 'package:fcs/data/provider/rate_data_provider.dart';
|
||||
import 'package:fcs/data/provider/user_data_provider.dart';
|
||||
import 'package:fcs/data/services/delivery_address_imp.dart';
|
||||
import 'package:fcs/data/services/delivery_address_service.dart';
|
||||
import 'package:fcs/data/services/fcs_shipment_imp.dart';
|
||||
import 'package:fcs/data/services/fcs_shipment_service.dart';
|
||||
import 'package:fcs/domain/vo/delivery_address.dart';
|
||||
import 'package:fcs/data/services/rate_imp.dart';
|
||||
import 'package:fcs/data/services/rate_service.dart';
|
||||
|
||||
import 'auth_imp.dart';
|
||||
import 'auth_service.dart';
|
||||
@@ -31,6 +33,7 @@ class Services {
|
||||
CommonService _commonService;
|
||||
FcsShipmentService _fcsShipmentService;
|
||||
DeliveryAddressService _deliveryAddressService;
|
||||
RateService _rateService;
|
||||
Services._() {
|
||||
_authService = AuthServiceImp(
|
||||
authFb: AuthFb.instance,
|
||||
@@ -47,6 +50,8 @@ class Services {
|
||||
_deliveryAddressService = DeliveryAddressImp(
|
||||
connectivity: null,
|
||||
deliveryAddressDataProvider: DeliveryAddressDataProvider());
|
||||
_rateService = RateServiceImp(
|
||||
rateDataProvider: RateDataProvider.instance, connectivity: null);
|
||||
}
|
||||
|
||||
AuthService get authService => _authService;
|
||||
@@ -56,4 +61,5 @@ class Services {
|
||||
CommonService get commonService => _commonService;
|
||||
FcsShipmentService get fcsShipmentService => _fcsShipmentService;
|
||||
DeliveryAddressService get deliveryAddressService => _deliveryAddressService;
|
||||
RateService get rateService => _rateService;
|
||||
}
|
||||
|
||||
23
lib/data/services/shipment_service.dart
Normal file
23
lib/data/services/shipment_service.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:fcs/domain/entities/cargo_type.dart';
|
||||
import 'package:fcs/domain/entities/custom_duty.dart';
|
||||
import 'package:fcs/domain/entities/discount_by_weight.dart';
|
||||
import 'package:fcs/domain/entities/shipment.dart';
|
||||
import 'package:fcs/domain/entities/rate.dart';
|
||||
|
||||
abstract class RateService {
|
||||
Stream<Rate> getRateStream();
|
||||
|
||||
Future<void> createShipment(Shipment shipment);
|
||||
|
||||
Future<void> createCargoType(CargoType cargoType);
|
||||
Future<void> updateCargoType(CargoType cargoType);
|
||||
Future<void> deleteCargoType(String id);
|
||||
|
||||
Future<void> createCustomDuty(CustomDuty customDuty);
|
||||
Future<void> updateCustomDuty(CustomDuty customDuty);
|
||||
Future<void> deleteCustomDuty(String id);
|
||||
|
||||
Future<void> createDiscountByWeight(DiscountByWeight discountByWeight);
|
||||
Future<void> updateDiscountByWeight(DiscountByWeight discountByWeight);
|
||||
Future<void> deleteDiscountByWeight(String id);
|
||||
}
|
||||
Reference in New Issue
Block a user