Skip to content

Instantly share code, notes, and snippets.

@austenstrine
Created August 15, 2019 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austenstrine/492dd916c4b3ddbafe912f5841eed915 to your computer and use it in GitHub Desktop.
Save austenstrine/492dd916c4b3ddbafe912f5841eed915 to your computer and use it in GitHub Desktop.
import 'package:json_annotation/json_annotation.dart';
import 'package:gogreen_utility_belt/interface_class/JSONSerializable.dart';
part 'Product.g.dart'; //flutter pub run build_runner build
@JsonSerializable()
class Product extends JSONSerializable {
static final Product example = Product(//comment out on build_runner build
upc: '10000000000',
inventory: 0,
categories: ['Rum'],
desc: 'This is a product',
discontinued: false,
earliestShipDate: DateTime(2019),
earliestShipDateQuantity: 0,
imageBase64: '',
imageURL: 'https://8.8.4.4',
listPrice: 0.50,
minSaleQty: 2,
mongoID: {'\$id': ''},
mpn: '0000',
price: 0.75,
productID: 10000,
title: 'This is the title',
uom: 'ea',
uomQty: 1);
final Map<String, String> mongoID;
final String title;
@JsonKey(fromJson: _toInteger)
final int productID;
final String mpn;
final double listPrice;
final double price;
final String uom;
@JsonKey(fromJson: _toInteger)
final int uomQty;
@JsonKey(fromJson: _toInteger)
final int inventory;
@JsonKey(fromJson: _toInteger)
final int minSaleQty;
final String desc;
List<String> categories;
final String imageURL;
final String imageBase64;
final String upc;
final bool discontinued;
@JsonKey(fromJson: _toDateTime)
final DateTime earliestShipDate;
@JsonKey(fromJson: _toInteger)
final int earliestShipDateQuantity;
Product(
{this.categories,
this.desc,
this.discontinued,
this.earliestShipDate,
this.earliestShipDateQuantity,
this.imageBase64,
this.imageURL,
this.inventory,
this.listPrice,
this.minSaleQty,
this.mongoID,
this.mpn,
this.price,
this.productID,
this.title,
this.uom,
this.uomQty,
this.upc});
@override
factory Product.fromJson(Map<String, dynamic> json) =>
_$ProductFromJson(json);
@override
Map<String, dynamic> toJson() => _$ProductToJson(this);
@override
List<String> propertyNames() {
List<String> list = [];
for (String str in toJson().keys) {
list.add(str);
}
print('returning property names');
return list;
}
@override
List<String> uniqueProperties() {
return ['upc', 'mpn', 'productID'];
}
static int _toInteger(dynamic givenValue) {
print('converting to integer');
if(givenValue == null) {
return 0;
}
else if(givenValue is int){
return givenValue;
}
else if(givenValue is String) {
return int.parse(givenValue);
}
else return 0;
}
static DateTime _toDateTime(dynamic givenValue) {
print('converting to date time');
if(givenValue == null){
return null;
}
String stringValue = givenValue as String;
if (stringValue == '') {
return null;
}
List<String> splitValues = stringValue.split('/');
if(splitValues.length == 3) {
String year = splitValues[2];
String month = splitValues[0];
String day = splitValues[1];
String properFormat = [year, month, day].join ('-');
// print(properFormat);
return DateTime.parse (properFormat);
}
else {
print(givenValue);
return null;
}
}
}
enum ProductSearchParameter {
categories,
desc,
earliestShipDate,
mpn,
productID,
title,
upc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment