From f320539daf72fb54e7b8ea552781950a4965f658 Mon Sep 17 00:00:00 2001 From: marcoabat Date: Sun, 6 Aug 2023 16:50:25 +0200 Subject: [PATCH] add item funzt endlich --- lib/pages/detail_checklist_page.dart | 8 ++++-- lib/services/dbhelper.dart | 43 +++++++++++++++++++++------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/pages/detail_checklist_page.dart b/lib/pages/detail_checklist_page.dart index 8f2c245..2f939d9 100644 --- a/lib/pages/detail_checklist_page.dart +++ b/lib/pages/detail_checklist_page.dart @@ -109,8 +109,12 @@ class _DetailChecklistPageState extends State { } void _itemSaved(String title, String description) { - DbHelper.addOrUpdateItem(_checklistProvider.selectedChecklistId!, title, - description, _selectedItemId); + DbHelper.addOrUpdateItem( + _checklistProvider.selectedChecklistId!, + title, + description, + _selectedItemId, + ).onError((error, stackTrace) => print(error)); } @override diff --git a/lib/services/dbhelper.dart b/lib/services/dbhelper.dart index fd7a301..11784b2 100644 --- a/lib/services/dbhelper.dart +++ b/lib/services/dbhelper.dart @@ -38,10 +38,10 @@ class DbHelper { for (final element in res) { Checklist cl = Checklist( element['id'], - element['ownerId'], + element['owner_id'], element['title'], element['description'], - DateTime.parse(element['createdTime']), + DateTime.parse(element['created_time']), ); checklists.add(cl); } @@ -53,7 +53,7 @@ class DbHelper { final ownerId = _client.auth.currentSession!.user.id; Map upsertMap = { - 'ownerId': ownerId, + 'owner_id': ownerId, }; if (checklist != null) { List> entries = [ @@ -71,6 +71,32 @@ class DbHelper { return res.last['id'] as int; } + static Future addOrUpdateItem( + int checklistId, + String title, + String description, + int? itemId, + ) async { + try { + final ownerId = _client.auth.currentSession!.user.id; + + Map upsertMap = { + 'checklist_id': checklistId, + 'owner_id': ownerId, + 'title': title, + 'description': description, + }; + if (itemId != null) { + upsertMap.addEntries([ + MapEntry('id', itemId), + ]); + } + await _client.from(itemsTableName).upsert(upsertMap); + } catch (e) { + print(e); + } + } + static Future> getItemsByChecklistId(int checklistId) async { final List items = []; @@ -86,7 +112,7 @@ class DbHelper { item['owner_id'], item['title'], item['description'], - DateTime.parse(item['createdTime']), + DateTime.parse(item['created_time']), null, ), ); @@ -104,18 +130,13 @@ class DbHelper { return Checklist( checklistRes['id'], - checklistRes['ownerId'], + checklistRes['owner_id'], checklistRes['title'], checklistRes['description'], - DateTime.parse(checklistRes['createdTime']), + DateTime.parse(checklistRes['created_time']), ); } - static Future addOrUpdateItem( - int checklistId, String title, String description, int? itemId) async { - // TODO implement addOrUpdateItem - } - static Stream get authChangeEventStream => _client.auth.onAuthStateChange; }