2021-10-22 23:08:09 +00:00
|
|
|
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
|
|
|
|
import 'package:diameter/models/basal.dart';
|
|
|
|
|
|
|
|
class BasalProfile {
|
|
|
|
late String? objectId;
|
|
|
|
late String name;
|
|
|
|
late bool active = false;
|
|
|
|
late Future<List<Basal>> basalRates;
|
|
|
|
late String? notes;
|
|
|
|
|
|
|
|
BasalProfile(ParseObject? object) {
|
|
|
|
if (object != null) {
|
|
|
|
objectId = object.get<String>('objectId');
|
|
|
|
name = object.get<String>('name')!;
|
|
|
|
active = object.get<bool>('active')!;
|
|
|
|
basalRates = Basal.fetchAllForBasalProfile(this);
|
|
|
|
notes = object.get<String>('notes');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<List<BasalProfile>> fetchAll() async {
|
|
|
|
QueryBuilder<ParseObject> query =
|
|
|
|
QueryBuilder<ParseObject>(ParseObject('BasalProfile'));
|
|
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
|
|
return apiResponse.results!
|
|
|
|
.map((e) => BasalProfile(e as ParseObject))
|
|
|
|
.toList();
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<BasalProfile?> get(String objectId) async {
|
|
|
|
QueryBuilder<ParseObject> query =
|
|
|
|
QueryBuilder<ParseObject>(ParseObject('BasalProfile'))
|
|
|
|
..whereEqualTo('objectId', objectId);
|
|
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
|
|
return BasalProfile(apiResponse.result.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 00:19:25 +00:00
|
|
|
static Future<int> getActiveCount() async {
|
|
|
|
QueryBuilder<ParseObject> query =
|
|
|
|
QueryBuilder<ParseObject>(ParseObject('BasalProfile'))
|
|
|
|
..whereEqualTo('active', true);
|
|
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
|
|
return apiResponse.results!.length;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> setAllInactive({String? exception}) async {
|
2021-10-22 23:08:09 +00:00
|
|
|
QueryBuilder<ParseObject> query =
|
|
|
|
QueryBuilder<ParseObject>(ParseObject('BasalProfile'));
|
|
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
|
|
for (var basalProfile in apiResponse.results as List<ParseObject>) {
|
|
|
|
basalProfile.set(
|
2021-10-23 00:19:25 +00:00
|
|
|
'active', basalProfile.objectId == exception ? true : false);
|
2021-10-22 23:08:09 +00:00
|
|
|
await basalProfile.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> save(
|
|
|
|
{required String name, bool active = false, String? notes}) async {
|
|
|
|
final basalProfile = ParseObject('BasalProfile')
|
|
|
|
..set('name', name)
|
|
|
|
..set('active', active)
|
|
|
|
..set('notes', notes);
|
|
|
|
await basalProfile.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> update(String objectId,
|
|
|
|
{String? name, bool? active, String? notes}) async {
|
|
|
|
var basalProfile = ParseObject('BasalProfile')..objectId = objectId;
|
|
|
|
if (name != null) {
|
|
|
|
basalProfile.set('name', name);
|
|
|
|
}
|
|
|
|
if (active != null) {
|
|
|
|
basalProfile.set('active', active);
|
|
|
|
}
|
|
|
|
if (notes != null) {
|
|
|
|
basalProfile.set('notes', notes);
|
|
|
|
}
|
|
|
|
await basalProfile.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> delete() async {
|
|
|
|
var basalProfile = ParseObject('BasalProfile')..objectId = objectId;
|
|
|
|
await basalProfile.delete();
|
|
|
|
}
|
|
|
|
}
|