158 lines
5.1 KiB
Dart
158 lines
5.1 KiB
Dart
// import 'package:diameter/models/accuracy.dart';
|
|
// import 'package:diameter/models/meal_category.dart';
|
|
// import 'package:diameter/models/meal_portion_type.dart';
|
|
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
|
|
|
|
class MealSource {
|
|
late String? objectId;
|
|
late String value;
|
|
late String? defaultCarbsRatioAccuracy;
|
|
late String? defaultPortionSizeAccuracy;
|
|
late String? defaultMealCategory;
|
|
late String? defaultMealPortionType;
|
|
late String? notes;
|
|
|
|
MealSource(ParseObject? object) {
|
|
if (object != null) {
|
|
objectId = object.get<String>('objectId');
|
|
value = object.get<String>('value')!;
|
|
defaultCarbsRatioAccuracy =
|
|
object.get<ParseObject>('defaultCarbsRatioAccuracy') != null
|
|
? object
|
|
.get<ParseObject>('defaultCarbsRatioAccuracy')!
|
|
.get<String>('objectId')
|
|
: null;
|
|
defaultPortionSizeAccuracy =
|
|
object.get<ParseObject>('defaultPortionSizeAccuracy') != null
|
|
? object
|
|
.get<ParseObject>('defaultPortionSizeAccuracy')!
|
|
.get<String>('objectId')
|
|
: null;
|
|
defaultMealCategory =
|
|
object.get<ParseObject>('defaultMealCategory') != null
|
|
? object
|
|
.get<ParseObject>('defaultMealCategory')!
|
|
.get<String>('objectId')
|
|
: null;
|
|
defaultMealPortionType =
|
|
object.get<ParseObject>('defaultMealPortionType') != null
|
|
? object
|
|
.get<ParseObject>('defaultMealPortionType')!
|
|
.get<String>('objectId')
|
|
: null;
|
|
notes = object.get<String>('notes');
|
|
}
|
|
}
|
|
|
|
static Future<List<MealSource>> fetchAll() async {
|
|
QueryBuilder<ParseObject> query =
|
|
QueryBuilder<ParseObject>(ParseObject('MealSource'));
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
return apiResponse.results!
|
|
.map((e) => MealSource(e as ParseObject))
|
|
.toList();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<MealSource?> get(String objectId) async {
|
|
QueryBuilder<ParseObject> query =
|
|
QueryBuilder<ParseObject>(ParseObject('MealSource'))
|
|
..whereEqualTo('objectId', objectId);
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
return MealSource(apiResponse.result.first);
|
|
}
|
|
}
|
|
|
|
static Future<void> save({
|
|
required String value,
|
|
String? defaultCarbsRatioAccuracy,
|
|
String? defaultPortionSizeAccuracy,
|
|
String? defaultMealCategory,
|
|
String? defaultMealPortionType,
|
|
String? notes,
|
|
}) async {
|
|
final mealSource = ParseObject('MealSource')
|
|
..set('value', value)
|
|
..set('notes', notes);
|
|
if (defaultCarbsRatioAccuracy != null) {
|
|
mealSource.set(
|
|
'defaultCarbsRatioAccuracy',
|
|
(ParseObject('Accuracy')..objectId = defaultCarbsRatioAccuracy)
|
|
.toPointer());
|
|
}
|
|
if (defaultPortionSizeAccuracy != null) {
|
|
mealSource.set(
|
|
'defaultCarbsRatioAccuracy',
|
|
(ParseObject('Accuracy')..objectId = defaultPortionSizeAccuracy)
|
|
.toPointer());
|
|
}
|
|
if (defaultMealCategory != null) {
|
|
mealSource.set(
|
|
'defaultMealCategory',
|
|
(ParseObject('MealCategory')..objectId = defaultMealCategory)
|
|
.toPointer());
|
|
}
|
|
if (defaultMealPortionType != null) {
|
|
mealSource.set(
|
|
'defaultMealPortionType',
|
|
(ParseObject('MealPortionType')..objectId = defaultMealPortionType)
|
|
.toPointer());
|
|
}
|
|
await mealSource.save();
|
|
}
|
|
|
|
static Future<void> update(
|
|
String objectId, {
|
|
String? value,
|
|
String? defaultCarbsRatioAccuracy,
|
|
String? defaultPortionSizeAccuracy,
|
|
String? defaultMealCategory,
|
|
String? defaultMealPortionType,
|
|
String? notes,
|
|
}) async {
|
|
final mealSource = ParseObject('MealSource')..objectId = objectId;
|
|
if (value != null) {
|
|
mealSource.set('value', value);
|
|
}
|
|
if (defaultCarbsRatioAccuracy != null) {
|
|
mealSource.set(
|
|
'defaultCarbsRatioAccuracy',
|
|
(ParseObject('Accuracy')..objectId = defaultCarbsRatioAccuracy)
|
|
.toPointer());
|
|
}
|
|
if (defaultPortionSizeAccuracy != null) {
|
|
mealSource.set(
|
|
'defaultCarbsRatioAccuracy',
|
|
(ParseObject('Accuracy')..objectId = defaultPortionSizeAccuracy)
|
|
.toPointer());
|
|
}
|
|
if (defaultMealCategory != null) {
|
|
mealSource.set(
|
|
'defaultMealCategory',
|
|
(ParseObject('MealCategory')..objectId = defaultMealCategory)
|
|
.toPointer());
|
|
}
|
|
if (defaultMealPortionType != null) {
|
|
mealSource.set(
|
|
'defaultMealPortionType',
|
|
(ParseObject('MealPortionType')..objectId = defaultMealPortionType)
|
|
.toPointer());
|
|
}
|
|
if (notes != null) {
|
|
mealSource.set('notes', notes);
|
|
}
|
|
await mealSource.save();
|
|
}
|
|
|
|
Future<void> delete() async {
|
|
var mealSource = ParseObject('MealSource')..objectId = objectId;
|
|
await mealSource.delete();
|
|
}
|
|
}
|