44 lines
1.4 KiB
Dart
44 lines
1.4 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:trainhub_flutter/data/database/app_database.dart';
|
|
|
|
part 'chat_dao.g.dart';
|
|
|
|
@DriftAccessor(tables: [ChatSessions, ChatMessages])
|
|
class ChatDao extends DatabaseAccessor<AppDatabase> with _$ChatDaoMixin {
|
|
ChatDao(super.db);
|
|
|
|
Future<List<ChatSession>> getAllSessions() =>
|
|
(select(chatSessions)
|
|
..orderBy([
|
|
(t) => OrderingTerm(
|
|
expression: t.createdAt, mode: OrderingMode.desc)
|
|
]))
|
|
.get();
|
|
|
|
Future<ChatSession?> getSession(String id) =>
|
|
(select(chatSessions)..where((t) => t.id.equals(id)))
|
|
.getSingleOrNull();
|
|
|
|
Future<void> insertSession(ChatSessionsCompanion entry) =>
|
|
into(chatSessions).insert(entry);
|
|
|
|
Future<void> deleteSession(String id) =>
|
|
(delete(chatSessions)..where((t) => t.id.equals(id))).go();
|
|
|
|
Future<void> updateSessionTitle(String id, String title) =>
|
|
(update(chatSessions)..where((t) => t.id.equals(id)))
|
|
.write(ChatSessionsCompanion(title: Value(title)));
|
|
|
|
Future<List<ChatMessage>> getMessages(String sessionId) =>
|
|
(select(chatMessages)
|
|
..where((t) => t.sessionId.equals(sessionId))
|
|
..orderBy([
|
|
(t) =>
|
|
OrderingTerm(expression: t.createdAt, mode: OrderingMode.asc)
|
|
]))
|
|
.get();
|
|
|
|
Future<void> insertMessage(ChatMessagesCompanion entry) =>
|
|
into(chatMessages).insert(entry);
|
|
}
|