75 lines
2.0 KiB
Dart
75 lines
2.0 KiB
Dart
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<void> login(String email, String password) async {
|
|
await pb.collection('users').authWithPassword(email, password);
|
|
}
|
|
|
|
Future<void> 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<List<Liste>> getLists() async {
|
|
if (userId == null) return [];
|
|
final List<RecordModel> res = await pb
|
|
.collection('lists')
|
|
.getFullList(filter: 'owner = "$userId" || members ?~ "$userId"');
|
|
return res.map((r) => Liste.fromJson(r.toJson())).toList();
|
|
}
|
|
|
|
Future<Liste> 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<List<Item>> getItems(String listId) async {
|
|
final List<RecordModel> res = await pb
|
|
.collection('items')
|
|
.getFullList(filter: 'list = "$listId"', sort: 'position');
|
|
return res.map((r) => Item.fromJson(r.toJson())).toList();
|
|
}
|
|
|
|
Future<Item> 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<void> updateItem(String itemId, {bool? checked}) async {
|
|
if (checked == null) return;
|
|
await pb.collection('items').update(itemId, body: {'checked': checked});
|
|
}
|
|
}
|