upsert and select for checklists

This commit is contained in:
marcoabat
2023-08-05 16:58:46 +02:00
parent 4d8f66bbde
commit ce57940c21
2 changed files with 43 additions and 8 deletions

View File

@@ -51,6 +51,8 @@ class _DashboardPageState extends State<DashboardPage> {
itemBuilder: (context, index) => itemBuilder: (context, index) =>
_listBuilder(context, index, snapshot.data!), _listBuilder(context, index, snapshot.data!),
); );
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else { } else {
return const CircularProgressIndicator(); return const CircularProgressIndicator();
} }
@@ -59,13 +61,13 @@ class _DashboardPageState extends State<DashboardPage> {
Widget? _listBuilder(BuildContext context, int index, List<Checklist> list) { Widget? _listBuilder(BuildContext context, int index, List<Checklist> list) {
Checklist cl = list.elementAt(index); Checklist cl = list.elementAt(index);
return ListTile( return ListTile(
title: Text(cl.title), title: Text(cl.title == '' ? 'Unnamed ${cl.id}' : cl.title),
subtitle: Text(cl.description), subtitle: Text(cl.description),
); );
} }
void _onAddTapped() { void _onAddTapped() {
DbHelper.addChecklist().then((id) { DbHelper.addOrUpdateChecklist(null).then((id) {
checklistProvider.updateSelectedChecklist(id, silent: true); checklistProvider.updateSelectedChecklist(id, silent: true);
}); });
Navigator.of(context).pushNamed(DetailChecklistPage.routeName); Navigator.of(context).pushNamed(DetailChecklistPage.routeName);

View File

@@ -6,6 +6,9 @@ import '../models/listitem.dart';
class DbHelper { class DbHelper {
static late final SupabaseClient _client; static late final SupabaseClient _client;
static const checklistsTableName = 'checklists';
static const itemsTableName = 'items';
static const checkedItemsTableName = 'checkedItems';
static Future<void> init() async { static Future<void> init() async {
await Supabase.initialize( await Supabase.initialize(
@@ -28,15 +31,45 @@ class DbHelper {
} }
static Future<List<Checklist>> get fetchChecklist async { static Future<List<Checklist>> get fetchChecklist async {
//TODO replace example data List<Checklist> checklists = [];
await Future.delayed(const Duration(seconds: 2)); final res = await _client
return ed.checklists; .from(checklistsTableName)
.select<List<Map<String, dynamic>>>();
for (final element in res) {
Checklist cl = Checklist(
element['id'],
element['ownerId'],
element['title'],
element['description'],
DateTime.parse(element['createdTime']),
[],
);
checklists.add(cl);
}
return checklists;
} }
/// returns id of newly created checklist /// returns id of newly created checklist
static Future<int> addChecklist() async { static Future<int> addOrUpdateChecklist(Checklist? checklist) async {
//TODO Add checklist final ownerId = _client.auth.currentSession!.user.id;
return 0;
Map<String, dynamic> upsertMap = {
'ownerId': ownerId,
};
if (checklist != null) {
List<MapEntry<String, dynamic>> entries = [
MapEntry('id', checklist.id),
MapEntry('title', checklist.title),
MapEntry('description', checklist.description),
];
upsertMap.addEntries(entries);
}
final res = await _client
.from('checklists')
.upsert(upsertMap)
.select<List<Map<String, dynamic>>>('id');
return res.last['id'] as int;
} }
static Future<Checklist> getChecklistById(int id) async { static Future<Checklist> getChecklistById(int id) async {