49 lines
1.3 KiB
Dart
49 lines
1.3 KiB
Dart
import 'package:postgres/postgres.dart';
|
|
|
|
/// Per-question link between a user, quiz row, and shared spelling item.
|
|
class SpellingQuestionLinksDb {
|
|
SpellingQuestionLinksDb(this._connection);
|
|
|
|
final Connection _connection;
|
|
|
|
/// Creates a link when the spelling tile loads; idempotent per [questionId].
|
|
Future<void> linkQuestion({
|
|
required String questionId,
|
|
required String firebaseUid,
|
|
required String spellingItemId,
|
|
}) async {
|
|
await _connection.execute(
|
|
Sql.named(
|
|
'''
|
|
INSERT INTO spelling_question_links (
|
|
question_id, firebase_uid, spelling_item_id
|
|
) VALUES (
|
|
@question_id::uuid, @uid, @item_id::uuid
|
|
)
|
|
ON CONFLICT (question_id) DO NOTHING
|
|
''',
|
|
),
|
|
parameters: <String, dynamic>{
|
|
'question_id': questionId,
|
|
'uid': firebaseUid,
|
|
'item_id': spellingItemId,
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<bool> hasLink(String questionId) async {
|
|
final Result result = await _connection.execute(
|
|
Sql.named(
|
|
'''
|
|
SELECT 1
|
|
FROM spelling_question_links
|
|
WHERE question_id = @question_id::uuid
|
|
LIMIT 1
|
|
''',
|
|
),
|
|
parameters: <String, dynamic>{'question_id': questionId},
|
|
);
|
|
return result.isNotEmpty;
|
|
}
|
|
}
|