123 lines
3.1 KiB
Dart
123 lines
3.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import '../../app.dart';
|
|
|
|
class AddressesApi {
|
|
static String className = 'AddressesApi';
|
|
|
|
static List<StateModel> states = [];
|
|
|
|
static Future<AddressModel?> create(Map<String, dynamic> params) async {
|
|
try {
|
|
// first get states
|
|
await getStates();
|
|
|
|
const String path = Constants.BASE_URL + 'customer/addresses';
|
|
|
|
final response = await HttpUtil().post(path, data: jsonEncode(params));
|
|
|
|
final newAddress = new AddressModel.fromJson(response['data']);
|
|
|
|
final stateTr = states.firstWhere((state) => state.code == newAddress.stateCode).translations;
|
|
|
|
newAddress.stateTranslations = [...stateTr];
|
|
|
|
return newAddress;
|
|
} catch (error) {
|
|
debugPrint('AddressesApi create error: ${error.toString()}');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<List<AddressModel>?> get() async {
|
|
final String fnName = 'get';
|
|
|
|
debugPrint('class: $className, method: $fnName');
|
|
|
|
try {
|
|
// first get states
|
|
await getStates();
|
|
|
|
final List<AddressModel> addresses = [];
|
|
const String path = Constants.BASE_URL + 'customer/addresses';
|
|
|
|
final response = await HttpUtil().get(path: path);
|
|
|
|
for (final address in response['data']) {
|
|
final newAddress = new AddressModel.fromJson(address);
|
|
|
|
final stateTr = states.firstWhere((state) => state.code == newAddress.stateCode).translations;
|
|
|
|
newAddress.stateTranslations = [...stateTr];
|
|
|
|
addresses.add(newAddress);
|
|
}
|
|
|
|
return addresses;
|
|
} catch (error) {
|
|
debugPrint('AddressesApi get error: $error');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<bool> delete(int id) async {
|
|
final String fnName = 'delete';
|
|
try {
|
|
String path = Constants.BASE_URL + 'customer/addresses/$id';
|
|
|
|
debugPrint('class: $className, method: $fnName, path: ' + path);
|
|
|
|
final response = await HttpUtil().delete(path);
|
|
|
|
debugPrint('delete address response $response');
|
|
|
|
return true;
|
|
} catch (error) {
|
|
debugPrint('AddressesApi get error: $error');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<List<StateModel>> getStates() async {
|
|
final String fnName = 'getStates';
|
|
|
|
if (states.isNotEmpty) return states;
|
|
|
|
try {
|
|
String path = Constants.BASE_URL + 'v1/countries/states/groups';
|
|
|
|
debugPrint('class: $className, method: $fnName, path: ' + path);
|
|
|
|
final response = await HttpUtil().get(path: path);
|
|
|
|
states = [...StateModel.listFromJson(response['data']['TM'] as List)];
|
|
|
|
debugPrint('states $states');
|
|
|
|
return states;
|
|
} catch (error) {
|
|
debugPrint('getStates get error: $error');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<ContactsModel?> getContacts() async {
|
|
final String fnName = 'getContacts';
|
|
|
|
try {
|
|
String path = Constants.BASE_URL + 'contacts';
|
|
|
|
debugPrint('class: $className, method: $fnName, path: ' + path);
|
|
|
|
final response = await HttpUtil().get(path: path);
|
|
|
|
return ContactsModel.fromJson(response);
|
|
} catch (error) {
|
|
debugPrint('$fnName error: $error');
|
|
return null;
|
|
}
|
|
}
|
|
}
|