DietaryGuard/lib/models/food_data.dart

398 lines
12 KiB
Dart
Raw Normal View History

2024-08-14 17:26:42 +00:00
class FoodDataQueryResponse {
int totalHits;
int currentPage;
int totalPages;
List<int> pageList;
FoodSearchCriteria foodSearchCriteria;
List<FoodData> foods;
Aggregations aggregations;
FoodDataQueryResponse({
required this.totalHits,
required this.currentPage,
required this.totalPages,
required this.pageList,
required this.foodSearchCriteria,
required this.foods,
required this.aggregations,
});
factory FoodDataQueryResponse.fromJson(Map<String, dynamic> json) =>
FoodDataQueryResponse(
totalHits: json["totalHits"],
currentPage: json["currentPage"],
totalPages: json["totalPages"],
pageList: List<int>.from(json["pageList"].map((x) => x)),
foodSearchCriteria:
FoodSearchCriteria.fromJson(json["foodSearchCriteria"]),
foods:
List<FoodData>.from(json["foods"].map((x) => FoodData.fromJson(x))),
aggregations: Aggregations.fromJson(json["aggregations"]),
);
Map<String, dynamic> toJson() => {
"totalHits": totalHits,
"currentPage": currentPage,
"totalPages": totalPages,
"pageList": List<dynamic>.from(pageList.map((x) => x)),
"foodSearchCriteria": foodSearchCriteria.toJson(),
"foods": List<dynamic>.from(foods.map((x) => x.toJson())),
"aggregations": aggregations.toJson(),
};
}
class Aggregations {
DataType dataType;
Nutrients nutrients;
Aggregations({
required this.dataType,
required this.nutrients,
});
factory Aggregations.fromJson(Map<String, dynamic> json) => Aggregations(
dataType: DataType.fromJson(json["dataType"]),
nutrients: Nutrients.fromJson(json["nutrients"]),
);
Map<String, dynamic> toJson() => {
"dataType": dataType.toJson(),
"nutrients": nutrients.toJson(),
};
}
class DataType {
2024-08-14 17:38:29 +00:00
int? branded;
int? surveyFndds;
int? srLegacy;
int? foundation;
2024-08-14 17:26:42 +00:00
DataType({
required this.branded,
required this.surveyFndds,
required this.srLegacy,
required this.foundation,
});
factory DataType.fromJson(Map<String, dynamic> json) => DataType(
branded: json["Branded"],
surveyFndds: json["Survey (FNDDS)"],
srLegacy: json["SR Legacy"],
foundation: json["Foundation"],
);
Map<String, dynamic> toJson() => {
"Branded": branded,
"Survey (FNDDS)": surveyFndds,
"SR Legacy": srLegacy,
"Foundation": foundation,
};
}
class Nutrients {
Nutrients();
factory Nutrients.fromJson(Map<String, dynamic> json) => Nutrients();
Map<String, dynamic> toJson() => {};
}
class FoodSearchCriteria {
List<FoodType?> dataType;
2024-08-14 17:26:42 +00:00
String query;
String generalSearchInput;
int pageNumber;
String sortBy;
String sortOrder;
int numberOfResultsPerPage;
int pageSize;
bool requireAllWords;
List<FoodType?> foodTypes;
2024-08-14 17:26:42 +00:00
FoodSearchCriteria({
required this.dataType,
required this.query,
required this.generalSearchInput,
required this.pageNumber,
required this.sortBy,
required this.sortOrder,
required this.numberOfResultsPerPage,
required this.pageSize,
required this.requireAllWords,
required this.foodTypes,
});
factory FoodSearchCriteria.fromJson(Map<String, dynamic> json) =>
FoodSearchCriteria(
dataType: List<FoodType?>.from(
2024-08-15 03:53:01 +00:00
json["dataType"]?.map((x) => typeValues.map[x]) ?? List.empty()),
2024-08-14 17:26:42 +00:00
query: json["query"],
generalSearchInput: json["generalSearchInput"],
pageNumber: json["pageNumber"],
sortBy: json["sortBy"],
sortOrder: json["sortOrder"],
numberOfResultsPerPage: json["numberOfResultsPerPage"],
pageSize: json["pageSize"],
requireAllWords: json["requireAllWords"],
foodTypes: List<FoodType?>.from(
2024-08-15 03:53:01 +00:00
json["foodTypes"]?.map((x) => typeValues.map[x]) ?? List.empty()),
2024-08-14 17:26:42 +00:00
);
Map<String, dynamic> toJson() => {
"dataType":
List<dynamic>.from(dataType.map((x) => typeValues.reverse[x])),
"query": query,
"generalSearchInput": generalSearchInput,
"pageNumber": pageNumber,
"sortBy": sortBy,
"sortOrder": sortOrder,
"numberOfResultsPerPage": numberOfResultsPerPage,
"pageSize": pageSize,
"requireAllWords": requireAllWords,
"foodTypes":
List<dynamic>.from(foodTypes.map((x) => typeValues.reverse[x])),
};
}
enum FoodType { FOUNDATION, SR_LEGACY }
2024-08-14 17:26:42 +00:00
final typeValues = EnumValues(
{"Foundation": FoodType.FOUNDATION, "SR Legacy": FoodType.SR_LEGACY});
2024-08-14 17:26:42 +00:00
class FoodData {
int fdcId;
String description;
2024-08-15 03:53:01 +00:00
String? commonNames;
String? additionalDescriptions;
FoodType? dataType;
2024-08-15 03:53:01 +00:00
int? ndbNumber;
2024-08-14 17:26:42 +00:00
DateTime publishedDate;
String? foodCategory;
2024-08-14 17:26:42 +00:00
DateTime? mostRecentAcquisitionDate;
String allHighlightFields;
double score;
List<dynamic> microbes;
List<FoodNutrient> foodNutrients;
List<dynamic> finalFoodInputFoods;
List<dynamic> foodMeasures;
List<dynamic> foodAttributes;
List<dynamic> foodAttributeTypes;
List<dynamic> foodVersionIds;
FoodData({
required this.fdcId,
required this.description,
required this.commonNames,
required this.additionalDescriptions,
required this.dataType,
required this.ndbNumber,
required this.publishedDate,
required this.foodCategory,
this.mostRecentAcquisitionDate,
required this.allHighlightFields,
required this.score,
required this.microbes,
required this.foodNutrients,
required this.finalFoodInputFoods,
required this.foodMeasures,
required this.foodAttributes,
required this.foodAttributeTypes,
required this.foodVersionIds,
});
factory FoodData.fromJson(Map<String, dynamic> json) => FoodData(
fdcId: json["fdcId"],
description: json["description"],
commonNames: json["commonNames"],
additionalDescriptions: json["additionalDescriptions"],
2024-08-15 03:53:01 +00:00
dataType: typeValues.map[json["dataType"]],
2024-08-14 17:26:42 +00:00
ndbNumber: json["ndbNumber"],
publishedDate: DateTime.parse(json["publishedDate"]),
foodCategory: json["foodCategory"],
2024-08-14 17:26:42 +00:00
mostRecentAcquisitionDate: json["mostRecentAcquisitionDate"] == null
? null
: DateTime.parse(json["mostRecentAcquisitionDate"]),
allHighlightFields: json["allHighlightFields"],
score: json["score"]?.toDouble(),
microbes: List<dynamic>.from(json["microbes"].map((x) => x)),
foodNutrients: List<FoodNutrient>.from(
json["foodNutrients"].map((x) => FoodNutrient.fromJson(x))),
finalFoodInputFoods:
List<dynamic>.from(json["finalFoodInputFoods"].map((x) => x)),
foodMeasures: List<dynamic>.from(json["foodMeasures"].map((x) => x)),
foodAttributes:
List<dynamic>.from(json["foodAttributes"].map((x) => x)),
foodAttributeTypes:
List<dynamic>.from(json["foodAttributeTypes"].map((x) => x)),
foodVersionIds:
List<dynamic>.from(json["foodVersionIds"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"fdcId": fdcId,
"description": description,
"commonNames": commonNames,
"additionalDescriptions": additionalDescriptions,
"dataType": typeValues.reverse[dataType],
"ndbNumber": ndbNumber,
"publishedDate":
"${publishedDate.year.toString().padLeft(4, '0')}-${publishedDate.month.toString().padLeft(2, '0')}-${publishedDate.day.toString().padLeft(2, '0')}",
"foodCategory": foodCategory,
2024-08-14 17:26:42 +00:00
"mostRecentAcquisitionDate":
"${mostRecentAcquisitionDate!.year.toString().padLeft(4, '0')}-${mostRecentAcquisitionDate!.month.toString().padLeft(2, '0')}-${mostRecentAcquisitionDate!.day.toString().padLeft(2, '0')}",
"allHighlightFields": allHighlightFields,
"score": score,
"microbes": List<dynamic>.from(microbes.map((x) => x)),
"foodNutrients":
List<dynamic>.from(foodNutrients.map((x) => x.toJson())),
"finalFoodInputFoods":
List<dynamic>.from(finalFoodInputFoods.map((x) => x)),
"foodMeasures": List<dynamic>.from(foodMeasures.map((x) => x)),
"foodAttributes": List<dynamic>.from(foodAttributes.map((x) => x)),
"foodAttributeTypes":
List<dynamic>.from(foodAttributeTypes.map((x) => x)),
"foodVersionIds": List<dynamic>.from(foodVersionIds.map((x) => x)),
};
}
class FoodNutrient {
int nutrientId;
String nutrientName;
String nutrientNumber;
2024-08-15 03:53:01 +00:00
UnitName? unitName;
2024-08-14 17:26:42 +00:00
DerivationCode? derivationCode;
String? derivationDescription;
int? derivationId;
double? value;
int? foodNutrientSourceId;
String? foodNutrientSourceCode;
FoodNutrientSourceDescription? foodNutrientSourceDescription;
int rank;
int indentLevel;
int foodNutrientId;
int? dataPoints;
double? min;
double? max;
double? median;
FoodNutrient({
required this.nutrientId,
required this.nutrientName,
required this.nutrientNumber,
required this.unitName,
this.derivationCode,
this.derivationDescription,
this.derivationId,
this.value,
this.foodNutrientSourceId,
this.foodNutrientSourceCode,
this.foodNutrientSourceDescription,
required this.rank,
required this.indentLevel,
required this.foodNutrientId,
this.dataPoints,
this.min,
this.max,
this.median,
});
factory FoodNutrient.fromJson(Map<String, dynamic> json) => FoodNutrient(
nutrientId: json["nutrientId"],
nutrientName: json["nutrientName"],
nutrientNumber: json["nutrientNumber"],
2024-08-15 03:53:01 +00:00
unitName: unitNameValues.map[json["unitName"]],
derivationCode: derivationCodeValues.map[json["derivationCode"]],
2024-08-14 17:26:42 +00:00
derivationDescription: json["derivationDescription"],
derivationId: json["derivationId"],
value: json["value"]?.toDouble(),
foodNutrientSourceId: json["foodNutrientSourceId"],
foodNutrientSourceCode: json["foodNutrientSourceCode"],
foodNutrientSourceDescription: foodNutrientSourceDescriptionValues
2024-08-15 03:53:01 +00:00
.map[json["foodNutrientSourceDescription"]],
2024-08-14 17:26:42 +00:00
rank: json["rank"],
indentLevel: json["indentLevel"],
foodNutrientId: json["foodNutrientId"],
dataPoints: json["dataPoints"],
min: json["min"]?.toDouble(),
max: json["max"]?.toDouble(),
median: json["median"]?.toDouble(),
);
Map<String, dynamic> toJson() => {
"nutrientId": nutrientId,
"nutrientName": nutrientName,
"nutrientNumber": nutrientNumber,
"unitName": unitNameValues.reverse[unitName],
"derivationCode": derivationCodeValues.reverse[derivationCode],
"derivationDescription": derivationDescription,
"derivationId": derivationId,
"value": value,
"foodNutrientSourceId": foodNutrientSourceId,
"foodNutrientSourceCode": foodNutrientSourceCode,
"foodNutrientSourceDescription": foodNutrientSourceDescriptionValues
.reverse[foodNutrientSourceDescription],
"rank": rank,
"indentLevel": indentLevel,
"foodNutrientId": foodNutrientId,
"dataPoints": dataPoints,
"min": min,
"max": max,
"median": median,
};
}
enum DerivationCode { A, AS, BFFN, BFNN, BFZN, CAZN, LC, NC, NR, T, Z }
final derivationCodeValues = EnumValues({
"A": DerivationCode.A,
"AS": DerivationCode.AS,
"BFFN": DerivationCode.BFFN,
"BFNN": DerivationCode.BFNN,
"BFZN": DerivationCode.BFZN,
"CAZN": DerivationCode.CAZN,
"LC": DerivationCode.LC,
"NC": DerivationCode.NC,
"NR": DerivationCode.NR,
"T": DerivationCode.T,
"Z": DerivationCode.Z
});
enum FoodNutrientSourceDescription {
ANALYTICAL_OR_DERIVED_FROM_ANALYTICAL,
ASSUMED_ZERO,
CALCULATED_FROM_NUTRIENT_LABEL_BY_NDL,
CALCULATED_OR_IMPUTED
}
final foodNutrientSourceDescriptionValues = EnumValues({
"Analytical or derived from analytical":
FoodNutrientSourceDescription.ANALYTICAL_OR_DERIVED_FROM_ANALYTICAL,
"Assumed zero": FoodNutrientSourceDescription.ASSUMED_ZERO,
"Calculated from nutrient label by NDL":
FoodNutrientSourceDescription.CALCULATED_FROM_NUTRIENT_LABEL_BY_NDL,
"Calculated or imputed": FoodNutrientSourceDescription.CALCULATED_OR_IMPUTED
});
enum UnitName { G, IU, KCAL, K_J, MG, UG }
final unitNameValues = EnumValues({
"G": UnitName.G,
"IU": UnitName.IU,
"KCAL": UnitName.KCAL,
"kJ": UnitName.K_J,
"MG": UnitName.MG,
"UG": UnitName.UG
});
class EnumValues<T> {
Map<String, T> map;
late Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
reverseMap = map.map((k, v) => MapEntry(v, k));
return reverseMap;
}
}