createBookmark method

Future<BookmarkModel> createBookmark({
  1. required BookmarkKind kind,
  2. required String targetId,
  3. String? note,
})

Creates a bookmark on the given target. Returns the persisted row. Throws if the user is not authenticated, or if the target is already bookmarked (Postgres unique-violation surfaces as a PostgrestException).

Implementation

Future<BookmarkModel> createBookmark({
  required BookmarkKind kind,
  required String targetId,
  String? note,
}) async {
  final userId = _client.auth.currentUser?.id;
  if (userId == null) {
    throw StateError('createBookmark requires an authenticated user');
  }
  final row = <String, dynamic>{
    'user_id': userId,
    'kind': BookmarkModel.kindToString(kind),
    'note': note,
    switch (kind) {
      BookmarkKind.topic => 'topic_id',
      BookmarkKind.chunk => 'chunk_id',
      BookmarkKind.session => 'session_id',
    }: targetId,
  };
  final response = await _client
      .from('user_bookmarks')
      .insert(row)
      .select()
      .single();
  return BookmarkModel.fromJson(response);
}