177 lines
5.6 KiB
Dart
177 lines
5.6 KiB
Dart
import 'package:diameter/components/data_table.dart';
|
|
import 'package:diameter/models/log_entry.dart';
|
|
import 'package:diameter/models/log_event_type.dart';
|
|
import 'package:diameter/utils/date_time_utils.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
|
|
|
|
class LogEvent extends DataTableContent {
|
|
late String? objectId;
|
|
late String logEntry;
|
|
late String? endLogEntry;
|
|
late String eventType;
|
|
late DateTime time;
|
|
late DateTime? endTime;
|
|
late bool hasEndTime;
|
|
late String? notes;
|
|
|
|
LogEvent(ParseObject? object) {
|
|
if (object != null) {
|
|
objectId = object.get<String>('objectId');
|
|
logEntry = object.get<ParseObject>('logEntry')!.get<String>('objectId')!;
|
|
endLogEntry =
|
|
object.get<ParseObject>('endLogEntry')?.get<String>('objectId');
|
|
eventType =
|
|
object.get<ParseObject>('eventType')!.get<String>('objectId')!;
|
|
time = object.get<DateTime>('time')!.toLocal();
|
|
endTime = object.get<DateTime>('endTime')?.toLocal();
|
|
hasEndTime = object.get<bool>('hasEndTime')!;
|
|
notes = object.get<String>('notes');
|
|
}
|
|
}
|
|
|
|
static Future<LogEvent?> get(String objectId) async {
|
|
QueryBuilder<ParseObject> query =
|
|
QueryBuilder<ParseObject>(ParseObject('LogEvent'))
|
|
..whereEqualTo('objectId', objectId);
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
return LogEvent(apiResponse.result.first);
|
|
}
|
|
}
|
|
|
|
static Future<List<LogEvent>> fetchAllActive() async {
|
|
QueryBuilder<ParseObject> query =
|
|
QueryBuilder<ParseObject>(ParseObject('LogEvent'))
|
|
..whereEqualTo('hasEndTime', true)
|
|
..whereEqualTo('endTime', null);
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
return apiResponse.results!
|
|
.map((e) => LogEvent(e as ParseObject))
|
|
.toList();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<List<LogEvent>> fetchAllForLogEntry(LogEntry logEntry) async {
|
|
QueryBuilder<ParseObject> query = QueryBuilder<ParseObject>(
|
|
ParseObject('LogEvent'))
|
|
..whereEqualTo('logEntry',
|
|
(ParseObject('LogEntry')..objectId = logEntry.objectId!).toPointer());
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
return apiResponse.results!
|
|
.map((e) => LogEvent(e as ParseObject))
|
|
.toList();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<List<LogEvent>> fetchAllEndedByEntry(LogEntry logEntry) async {
|
|
QueryBuilder<ParseObject> query = QueryBuilder<ParseObject>(
|
|
ParseObject('LogEvent'))
|
|
..whereEqualTo('endLogEntry',
|
|
(ParseObject('LogEntry')..objectId = logEntry.objectId!).toPointer());
|
|
final ParseResponse apiResponse = await query.query();
|
|
|
|
if (apiResponse.success && apiResponse.results != null) {
|
|
return apiResponse.results!
|
|
.map((e) => LogEvent(e as ParseObject))
|
|
.toList();
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<void> save({
|
|
required String logEntry,
|
|
required String eventType,
|
|
required DateTime time,
|
|
required bool hasEndTime,
|
|
String? notes,
|
|
}) async {
|
|
final logEvent = ParseObject('LogEvent')
|
|
..set('logEntry',
|
|
(ParseObject('LogEntry')..objectId = logEntry).toPointer())
|
|
..set('eventType',
|
|
(ParseObject('LogEventType')..objectId = eventType).toPointer())
|
|
..set('time', time.toUtc())
|
|
..set('hasEndTime', hasEndTime)
|
|
..set('notes', notes);
|
|
await logEvent.save();
|
|
}
|
|
|
|
static Future<void> update(
|
|
String objectId, {
|
|
String? eventType,
|
|
String? endLogEntry,
|
|
DateTime? time,
|
|
DateTime? endTime,
|
|
bool? hasEndTime,
|
|
String? notes,
|
|
}) async {
|
|
var logEvent = ParseObject('LogEvent')..objectId = objectId;
|
|
if (eventType != null) {
|
|
logEvent.set('eventType',
|
|
(ParseObject('LogEventType')..objectId = eventType).toPointer());
|
|
}
|
|
if (endLogEntry != null) {
|
|
logEvent.set('endLogEntry',
|
|
(ParseObject('LogEntry')..objectId = endLogEntry).toPointer());
|
|
}
|
|
if (time != null) {
|
|
logEvent.set('time', time.toUtc());
|
|
}
|
|
if (endTime != null) {
|
|
logEvent.set('endTime', endTime.toUtc());
|
|
}
|
|
if (hasEndTime != null) {
|
|
logEvent.set('hasEndTime', hasEndTime);
|
|
}
|
|
if (notes != null) {
|
|
logEvent.set('notes', notes);
|
|
}
|
|
await logEvent.save();
|
|
}
|
|
|
|
Future<void> delete() async {
|
|
var logEvent = ParseObject('LogEvent')..objectId = objectId;
|
|
await logEvent.delete();
|
|
}
|
|
//
|
|
// @override
|
|
// List<DataCell> asDataTableCells(List<Widget> actions,
|
|
// {List<LogEventType>? types}) {
|
|
// return [
|
|
// DataCell(Text(
|
|
// types?.firstWhere((element) => element.objectId == eventType).value ??
|
|
// types?.length.toString() ??
|
|
// '')),
|
|
// DataCell(Text(DateTimeUtils.displayDateTime(time))),
|
|
// DataCell(Text(hasEndTime
|
|
// ? DateTimeUtils.displayDateTime(endTime, fallback: 'ongoing')
|
|
// : '-')),
|
|
// DataCell(
|
|
// Row(
|
|
// children: actions,
|
|
// ),
|
|
// ),
|
|
// ];
|
|
// }
|
|
//
|
|
// static List<DataColumn> asDataTableColumns() {
|
|
// return [
|
|
// const DataColumn(label: Expanded(child: Text('Event Type'))),
|
|
// const DataColumn(label: Expanded(child: Text('Start Time'))),
|
|
// const DataColumn(label: Expanded(child: Text('End Time'))),
|
|
// const DataColumn(label: Expanded(child: Text('Actions'))),
|
|
// ];
|
|
// }
|
|
}
|