Initial commit
This commit is contained in:
84
lib/data/repositories/chat_repository_impl.dart
Normal file
84
lib/data/repositories/chat_repository_impl.dart
Normal file
@@ -0,0 +1,84 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:trainhub_flutter/core/utils/id_generator.dart';
|
||||
import 'package:trainhub_flutter/data/database/app_database.dart';
|
||||
import 'package:trainhub_flutter/data/database/daos/chat_dao.dart';
|
||||
import 'package:trainhub_flutter/data/mappers/chat_mapper.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/chat_session.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/chat_message.dart';
|
||||
import 'package:trainhub_flutter/domain/repositories/chat_repository.dart';
|
||||
|
||||
class ChatRepositoryImpl implements ChatRepository {
|
||||
final ChatDao _dao;
|
||||
|
||||
ChatRepositoryImpl(this._dao);
|
||||
|
||||
@override
|
||||
Future<List<ChatSessionEntity>> getAllSessions() async {
|
||||
final rows = await _dao.getAllSessions();
|
||||
return rows.map(ChatMapper.sessionToEntity).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ChatSessionEntity?> getSession(String id) async {
|
||||
final row = await _dao.getSession(id);
|
||||
return row == null ? null : ChatMapper.sessionToEntity(row);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ChatSessionEntity> createSession() async {
|
||||
final String id = IdGenerator.generate();
|
||||
final String now = DateTime.now().toIso8601String();
|
||||
await _dao.insertSession(
|
||||
ChatSessionsCompanion.insert(
|
||||
id: id,
|
||||
title: const Value('New Chat'),
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
),
|
||||
);
|
||||
final row = await _dao.getSession(id);
|
||||
return ChatMapper.sessionToEntity(row!);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteSession(String id) async {
|
||||
await _dao.deleteSession(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ChatMessageEntity>> getMessages(String sessionId) async {
|
||||
final rows = await _dao.getMessages(sessionId);
|
||||
return rows.map(ChatMapper.messageToEntity).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ChatMessageEntity> addMessage({
|
||||
required String sessionId,
|
||||
required String role,
|
||||
required String content,
|
||||
}) async {
|
||||
final String id = IdGenerator.generate();
|
||||
final String now = DateTime.now().toIso8601String();
|
||||
await _dao.insertMessage(
|
||||
ChatMessagesCompanion.insert(
|
||||
id: id,
|
||||
sessionId: sessionId,
|
||||
role: role,
|
||||
content: content,
|
||||
createdAt: now,
|
||||
),
|
||||
);
|
||||
return ChatMessageEntity(
|
||||
id: id,
|
||||
sessionId: sessionId,
|
||||
role: role,
|
||||
content: content,
|
||||
createdAt: now,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateSessionTitle(String sessionId, String title) async {
|
||||
await _dao.updateSessionTitle(sessionId, title);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user