Big update: can now update entries from flutter
This commit is contained in:
@@ -137,6 +137,49 @@ class DatabaseAPI extends ChangeNotifier {
|
||||
return response.documents;
|
||||
}
|
||||
|
||||
Future<Document> updateEntry(
|
||||
{String? dateISO,
|
||||
List<dynamic>? hours,
|
||||
int? mood,
|
||||
String? comments}) async {
|
||||
String date = formatter.format(DateTime.parse(dateISO!));
|
||||
|
||||
int entryIndex = _entries.indexWhere((element) => elDate(element) == date);
|
||||
|
||||
hours ??= _entries[entryIndex].data['hours'];
|
||||
comments ??= _entries[entryIndex].data['comments'];
|
||||
mood ??= _entries[entryIndex].data['mood'];
|
||||
|
||||
Document newEntry = await databases.updateDocument(
|
||||
databaseId: APPWRITE_DATABASE_ID,
|
||||
collectionId: COLLECTION,
|
||||
documentId: date,
|
||||
data: {'hours': hours, 'mood': mood, 'comments': comments},
|
||||
);
|
||||
|
||||
_entries.removeAt(entryIndex);
|
||||
_entries.add(newEntry);
|
||||
notifyListeners();
|
||||
return newEntry;
|
||||
}
|
||||
|
||||
updateHours(dayEntry, index, value) {
|
||||
List<dynamic> hours = dayEntry.data['hours'];
|
||||
try {
|
||||
hours[index] = num.parse(value);
|
||||
} catch (e) {
|
||||
if (hours.length == index) {
|
||||
hours.add(num.parse(value));
|
||||
} else {
|
||||
print(List.generate(index - hours.length, (i) => -1));
|
||||
|
||||
hours.addAll(List.generate(index - hours.length, (i) => -1));
|
||||
hours.add(num.parse(value));
|
||||
}
|
||||
}
|
||||
updateEntry(dateISO: dayEntry.data['date'], hours: hours);
|
||||
}
|
||||
|
||||
Future<Document> addEntry(
|
||||
{required String date,
|
||||
List hours = const [],
|
||||
|
||||
Reference in New Issue
Block a user