import 'dart:async'; import 'package:pocketbase/pocketbase.dart'; import 'package:shoppinglist/models/models.dart'; class ApiService { final PocketBase pb; ApiService(this.pb); String? get userId => pb.authStore.record?.id; Future login(String email, String password) async { await pb.collection('users').authWithPassword(email, password); } Future register(String email, String password) async { await pb .collection('users') .create( body: { 'email': email, 'password': password, 'passwordConfirm': password, 'emailVisibility': true, }, ); await login(email, password); } void logout() { pb.authStore.clear(); } Stream watchLists() { if (userId == null) return Stream.value(null); late final StreamController controller; Future Function()? unsubscribe; controller = StreamController( onListen: () async { unsubscribe = await pb.collection('lists').subscribe('*', (e) { if (!controller.isClosed) { controller.add(null); } }, filter: 'owner = "$userId" || members ?~ "$userId"'); }, onCancel: () { unsubscribe?.call(); }, ); return controller.stream; } Future> getLists() async { if (userId == null) return []; final List res = await pb .collection('lists') .getFullList(filter: 'owner = "$userId" || members ?~ "$userId"'); return res.map((r) => Liste.fromJson(r.toJson())).toList(); } Future createList(String title) async { if (userId == null) throw Exception('Nicht eingeloggt'); final RecordModel rec = await pb .collection('lists') .create(body: {'title': title, 'owner': userId}); return Liste.fromJson(rec.toJson()); } Future updateList( String listId, { String? title, List? members, }) async { if (title == null && members == null) return; final body = {}; if (title != null) { body['title'] = title; } if (members != null) { body['members'] = members; } await pb.collection('lists').update(listId, body: body); } Future deleteList(String listId) async { await pb.collection('lists').delete(listId); } Stream watchItems(String listId) { late final StreamController controller; Future Function()? unsubscribe; controller = StreamController( onListen: () async { unsubscribe = await pb.collection('items').subscribe('*', (e) { if (!controller.isClosed) { controller.add(null); } }, filter: 'list = "$listId"'); }, onCancel: () { unsubscribe?.call(); }, ); return controller.stream; } Future> getItems(String listId) async { final List res = await pb .collection('items') .getFullList(filter: 'list = "$listId"', sort: 'position'); return res.map((r) => Item.fromJson(r.toJson())).toList(); } Future createItem(String listId, String name, int position) async { final RecordModel rec = await pb .collection('items') .create( body: { 'list': listId, 'name': name, 'position': position, 'checked': false, }, ); return Item.fromJson(rec.toJson()); } Future deleteItem(String itemId) async { await pb.collection('items').delete(itemId); } Future updateItem(String itemId, {bool? checked, String? name}) async { if (checked == null && name == null) return; final Map body = {}; if (checked != null) { body['checked'] = checked; } if (name != null) { body['name'] = name; } await pb.collection('items').update(itemId, body: body); } Future findUserByEmail(String email) async { try { return await pb.collection('users').getFirstListItem('email = "$email"'); } catch (e) { return null; } } Future> getUsersByIds(List ids) async { if (ids.isEmpty) return []; final String filter = ids.map((id) => 'id = "$id"').join(' || '); return await pb.collection('users').getFullList(filter: filter); } }