Working on saving products

This commit is contained in:
unknown 2018-05-27 07:14:55 -07:00
parent e111260be6
commit 0ce144ea59
8 changed files with 74 additions and 369 deletions

View File

@ -94,47 +94,3 @@ abstract class DashboardEntity implements Built<DashboardEntity, DashboardEntity
//factory DashboardEntity([updates(DashboardEntityBuilder b)]) = _$DashboardEntity;
static Serializer<DashboardEntity> get serializer => _$dashboardEntitySerializer;
}
abstract class ProductResponse implements Built<ProductResponse, ProductResponseBuilder> {
BuiltList<ProductEntity> get data;
ProductResponse._();
factory ProductResponse([updates(ProductResponseBuilder b)]) = _$ProductResponse;
static Serializer<ProductResponse> get serializer => _$productResponseSerializer;
}
abstract class ProductEntity implements Built<ProductEntity, ProductEntityBuilder> {
int get id;
@BuiltValueField(wireName: 'product_key')
String get productKey;
String get notes;
double get cost;
//@JsonKey(name: 'tax_name1')
//String taxName1;
//@JsonKey(name: 'tax_rate1')
//double taxRate1;
//@JsonKey(name: 'tax_name2')
//String taxName2;
//@JsonKey(name: 'tax_rate2')
//double taxRate2;
//@JsonKey(name: 'updated_at')
//int updatedAt;
//@JsonKey(name: 'archived_at')
//int archivedAt;
//@JsonKey(name: 'custom_value1')
//String customValue1;
//@JsonKey(name: 'custom_value2')
//String customValue2;
//@JsonKey(name: 'is_deleted')
//bool isDeleted;
ProductEntity._();
factory ProductEntity([updates(ProductEntityBuilder b)]) = _$ProductEntity;
static Serializer<ProductEntity> get serializer => _$productEntitySerializer;
}

View File

@ -24,10 +24,6 @@ Serializer<DashboardResponse> _$dashboardResponseSerializer =
new _$DashboardResponseSerializer();
Serializer<DashboardEntity> _$dashboardEntitySerializer =
new _$DashboardEntitySerializer();
Serializer<ProductResponse> _$productResponseSerializer =
new _$ProductResponseSerializer();
Serializer<ProductEntity> _$productEntitySerializer =
new _$ProductEntitySerializer();
class _$ErrorMessageSerializer implements StructuredSerializer<ErrorMessage> {
@override
@ -317,109 +313,6 @@ class _$DashboardEntitySerializer
}
}
class _$ProductResponseSerializer
implements StructuredSerializer<ProductResponse> {
@override
final Iterable<Type> types = const [ProductResponse, _$ProductResponse];
@override
final String wireName = 'ProductResponse';
@override
Iterable serialize(Serializers serializers, ProductResponse object,
{FullType specifiedType: FullType.unspecified}) {
final result = <Object>[
'data',
serializers.serialize(object.data,
specifiedType:
const FullType(BuiltList, const [const FullType(ProductEntity)])),
];
return result;
}
@override
ProductResponse deserialize(Serializers serializers, Iterable serialized,
{FullType specifiedType: FullType.unspecified}) {
final result = new ProductResponseBuilder();
final iterator = serialized.iterator;
while (iterator.moveNext()) {
final key = iterator.current as String;
iterator.moveNext();
final dynamic value = iterator.current;
switch (key) {
case 'data':
result.data.replace(serializers.deserialize(value,
specifiedType: const FullType(
BuiltList, const [const FullType(ProductEntity)]))
as BuiltList);
break;
}
}
return result.build();
}
}
class _$ProductEntitySerializer implements StructuredSerializer<ProductEntity> {
@override
final Iterable<Type> types = const [ProductEntity, _$ProductEntity];
@override
final String wireName = 'ProductEntity';
@override
Iterable serialize(Serializers serializers, ProductEntity object,
{FullType specifiedType: FullType.unspecified}) {
final result = <Object>[
'id',
serializers.serialize(object.id, specifiedType: const FullType(int)),
'product_key',
serializers.serialize(object.productKey,
specifiedType: const FullType(String)),
'notes',
serializers.serialize(object.notes,
specifiedType: const FullType(String)),
'cost',
serializers.serialize(object.cost, specifiedType: const FullType(double)),
];
return result;
}
@override
ProductEntity deserialize(Serializers serializers, Iterable serialized,
{FullType specifiedType: FullType.unspecified}) {
final result = new ProductEntityBuilder();
final iterator = serialized.iterator;
while (iterator.moveNext()) {
final key = iterator.current as String;
iterator.moveNext();
final dynamic value = iterator.current;
switch (key) {
case 'id':
result.id = serializers.deserialize(value,
specifiedType: const FullType(int)) as int;
break;
case 'product_key':
result.productKey = serializers.deserialize(value,
specifiedType: const FullType(String)) as String;
break;
case 'notes':
result.notes = serializers.deserialize(value,
specifiedType: const FullType(String)) as String;
break;
case 'cost':
result.cost = serializers.deserialize(value,
specifiedType: const FullType(double)) as double;
break;
}
}
return result.build();
}
}
class _$ErrorMessage extends ErrorMessage {
@override
final String message;
@ -989,206 +882,3 @@ class DashboardEntityBuilder
return _$result;
}
}
class _$ProductResponse extends ProductResponse {
@override
final BuiltList<ProductEntity> data;
factory _$ProductResponse([void updates(ProductResponseBuilder b)]) =>
(new ProductResponseBuilder()..update(updates)).build();
_$ProductResponse._({this.data}) : super._() {
if (data == null)
throw new BuiltValueNullFieldError('ProductResponse', 'data');
}
@override
ProductResponse rebuild(void updates(ProductResponseBuilder b)) =>
(toBuilder()..update(updates)).build();
@override
ProductResponseBuilder toBuilder() =>
new ProductResponseBuilder()..replace(this);
@override
bool operator ==(dynamic other) {
if (identical(other, this)) return true;
if (other is! ProductResponse) return false;
return data == other.data;
}
@override
int get hashCode {
return $jf($jc(0, data.hashCode));
}
@override
String toString() {
return (newBuiltValueToStringHelper('ProductResponse')..add('data', data))
.toString();
}
}
class ProductResponseBuilder
implements Builder<ProductResponse, ProductResponseBuilder> {
_$ProductResponse _$v;
ListBuilder<ProductEntity> _data;
ListBuilder<ProductEntity> get data =>
_$this._data ??= new ListBuilder<ProductEntity>();
set data(ListBuilder<ProductEntity> data) => _$this._data = data;
ProductResponseBuilder();
ProductResponseBuilder get _$this {
if (_$v != null) {
_data = _$v.data?.toBuilder();
_$v = null;
}
return this;
}
@override
void replace(ProductResponse other) {
if (other == null) throw new ArgumentError.notNull('other');
_$v = other as _$ProductResponse;
}
@override
void update(void updates(ProductResponseBuilder b)) {
if (updates != null) updates(this);
}
@override
_$ProductResponse build() {
_$ProductResponse _$result;
try {
_$result = _$v ?? new _$ProductResponse._(data: data.build());
} catch (_) {
String _$failedField;
try {
_$failedField = 'data';
data.build();
} catch (e) {
throw new BuiltValueNestedFieldError(
'ProductResponse', _$failedField, e.toString());
}
rethrow;
}
replace(_$result);
return _$result;
}
}
class _$ProductEntity extends ProductEntity {
@override
final int id;
@override
final String productKey;
@override
final String notes;
@override
final double cost;
factory _$ProductEntity([void updates(ProductEntityBuilder b)]) =>
(new ProductEntityBuilder()..update(updates)).build();
_$ProductEntity._({this.id, this.productKey, this.notes, this.cost})
: super._() {
if (id == null) throw new BuiltValueNullFieldError('ProductEntity', 'id');
if (productKey == null)
throw new BuiltValueNullFieldError('ProductEntity', 'productKey');
if (notes == null)
throw new BuiltValueNullFieldError('ProductEntity', 'notes');
if (cost == null)
throw new BuiltValueNullFieldError('ProductEntity', 'cost');
}
@override
ProductEntity rebuild(void updates(ProductEntityBuilder b)) =>
(toBuilder()..update(updates)).build();
@override
ProductEntityBuilder toBuilder() => new ProductEntityBuilder()..replace(this);
@override
bool operator ==(dynamic other) {
if (identical(other, this)) return true;
if (other is! ProductEntity) return false;
return id == other.id &&
productKey == other.productKey &&
notes == other.notes &&
cost == other.cost;
}
@override
int get hashCode {
return $jf($jc(
$jc($jc($jc(0, id.hashCode), productKey.hashCode), notes.hashCode),
cost.hashCode));
}
@override
String toString() {
return (newBuiltValueToStringHelper('ProductEntity')
..add('id', id)
..add('productKey', productKey)
..add('notes', notes)
..add('cost', cost))
.toString();
}
}
class ProductEntityBuilder
implements Builder<ProductEntity, ProductEntityBuilder> {
_$ProductEntity _$v;
int _id;
int get id => _$this._id;
set id(int id) => _$this._id = id;
String _productKey;
String get productKey => _$this._productKey;
set productKey(String productKey) => _$this._productKey = productKey;
String _notes;
String get notes => _$this._notes;
set notes(String notes) => _$this._notes = notes;
double _cost;
double get cost => _$this._cost;
set cost(double cost) => _$this._cost = cost;
ProductEntityBuilder();
ProductEntityBuilder get _$this {
if (_$v != null) {
_id = _$v.id;
_productKey = _$v.productKey;
_notes = _$v.notes;
_cost = _$v.cost;
_$v = null;
}
return this;
}
@override
void replace(ProductEntity other) {
if (other == null) throw new ArgumentError.notNull('other');
_$v = other as _$ProductEntity;
}
@override
void update(void updates(ProductEntityBuilder b)) {
if (updates != null) updates(this);
}
@override
_$ProductEntity build() {
final _$result = _$v ??
new _$ProductEntity._(
id: id, productKey: productKey, notes: notes, cost: cost);
replace(_$result);
return _$result;
}
}

View File

@ -1 +1,2 @@
export 'package:invoiceninja/data/models/entities.dart';
export 'package:invoiceninja/data/models/entities.dart';
export 'package:invoiceninja/data/models/product.dart';

View File

@ -0,0 +1,57 @@
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'product.g.dart';
abstract class ProductListResponse implements Built<ProductListResponse, ProductListResponseBuilder> {
BuiltList<ProductEntity> get data;
ProductListResponse._();
factory ProductListResponse([updates(ProductListResponseBuilder b)]) = _$ProductListResponse;
static Serializer<ProductListResponse> get serializer => _$productListResponseSerializer;
}
abstract class ProductItemResponse implements Built<ProductItemResponse, ProductItemResponseBuilder> {
ProductEntity get data;
ProductItemResponse._();
factory ProductItemResponse([updates(ProductItemResponseBuilder b)]) = _$ProductItemResponse;
static Serializer<ProductItemResponse> get serializer => _$productItemResponseSerializer;
}
abstract class ProductEntity implements Built<ProductEntity, ProductEntityBuilder> {
int get id;
@BuiltValueField(wireName: 'product_key')
String get productKey;
String get notes;
double get cost;
//@JsonKey(name: 'tax_name1')
//String taxName1;
//@JsonKey(name: 'tax_rate1')
//double taxRate1;
//@JsonKey(name: 'tax_name2')
//String taxName2;
//@JsonKey(name: 'tax_rate2')
//double taxRate2;
//@JsonKey(name: 'updated_at')
//int updatedAt;
//@JsonKey(name: 'archived_at')
//int archivedAt;
//@JsonKey(name: 'custom_value1')
//String customValue1;
//@JsonKey(name: 'custom_value2')
//String customValue2;
//@JsonKey(name: 'is_deleted')
//bool isDeleted;
ProductEntity._();
factory ProductEntity([updates(ProductEntityBuilder b)]) = _$ProductEntity;
static Serializer<ProductEntity> get serializer => _$productEntitySerializer;
}

View File

@ -1,6 +1,7 @@
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:invoiceninja/data/models/models.dart';
import 'package:invoiceninja/data/models/entities.dart';
part 'serializers.g.dart';
@ -8,7 +9,8 @@ part 'serializers.g.dart';
@SerializersFor(const [
LoginResponse,
DashboardResponse,
ProductResponse,
ProductListResponse,
ProductItemResponse,
])
final Serializers serializers =
(_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

View File

@ -21,7 +21,8 @@ Serializers _$serializers = (new Serializers().toBuilder()
..add(ErrorMessage.serializer)
..add(LoginResponse.serializer)
..add(ProductEntity.serializer)
..add(ProductResponse.serializer)
..add(ProductItemResponse.serializer)
..add(ProductListResponse.serializer)
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(CompanyEntity)]),
() => new ListBuilder<CompanyEntity>())

View File

@ -6,7 +6,7 @@ import 'package:invoiceninja/data/models/serializers.dart';
import 'package:built_collection/built_collection.dart';
import 'package:invoiceninja/redux/auth/auth_state.dart';
import 'package:invoiceninja/data/models/entities.dart';
import 'package:invoiceninja/data/models/models.dart';
import 'package:invoiceninja/data/file_storage.dart';
import 'package:invoiceninja/data/web_client.dart';
@ -24,26 +24,20 @@ class ProductsRepository {
final response = await webClient.get(
auth.url + '/products', company.token);
ProductResponse productResponse = serializers.deserializeWith(
ProductResponse.serializer, response);
ProductListResponse productResponse = serializers.deserializeWith(
ProductListResponse.serializer, response);
return productResponse.data;
}
Future saveData(CompanyEntity company, AuthState auth, ProductEntity product) async {
print(auth.url + '/products/' + product.id.toString());
print(company.token);
var data = serializers.serializeWith(ProductEntity.serializer, product);
final response = await webClient.put(
auth.url + '/products/' + product.id.toString(), company.token, json.encode(data));
print('== SAVE RESPONSE: POST');
print(response);
ProductResponse productResponse = serializers.deserializeWith(
ProductResponse.serializer, response);
ProductItemResponse productResponse = serializers.deserializeWith(
ProductItemResponse.serializer, response);
return productResponse.data;
}

View File

@ -25,7 +25,11 @@ List<Middleware<AppState>> createStoreProductsMiddleware([
Middleware<AppState> _createSaveProduct(ProductsRepository repository) {
return (Store<AppState> store, action, NextDispatcher next) {
repository.saveData(store.state.selectedCompany(), store.state.authState, action.product);
repository.saveData(store.state.selectedCompany(), store.state.authState, action.product).then(
(product) => store.dispatch(SaveProductSuccess(product))
).catchError((error) {
store.dispatch(SaveProductFailure(error));
});
/*
repository.login(action.email, action.password, action.url).then(