import 'package:pocketbase/pocketbase.dart'; import 'package:shoppinglist/models/models.dart'; class ApiService { final PocketBase pb; ApiService(this.pb); String? get userId => pb.authStore.model?.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, }, ); await login(email, password); } void logout() { pb.authStore.clear(); } 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> 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 updateItem(String itemId, {bool? checked}) async { if (checked == null) return; await pb.collection('items').update(itemId, body: {'checked': checked}); } }