|
|
- import '/models/conversations.dart';
- import '/utils/storage/database.dart';
-
- Future<ConversationUser> getConversationUser(Conversation conversation, String userId) async {
- final db = await getDatabaseConnection();
-
- final List<Map<String, dynamic>> maps = await db.query(
- 'conversation_users',
- where: 'conversation_id = ? AND user_id = ?',
- whereArgs: [conversation.id, userId],
- );
-
- if (maps.length != 1) {
- throw ArgumentError('Invalid conversation_id or username');
- }
-
- return ConversationUser(
- id: maps[0]['id'],
- userId: maps[0]['user_id'],
- conversationId: maps[0]['conversation_id'],
- username: maps[0]['username'],
- associationKey: maps[0]['association_key'],
- admin: maps[0]['admin'] == 1,
- );
-
- }
-
- // A method that retrieves all the dogs from the dogs table.
- Future<List<ConversationUser>> getConversationUsers(Conversation conversation) async {
- final db = await getDatabaseConnection();
-
- final List<Map<String, dynamic>> maps = await db.query(
- 'conversation_users',
- where: 'conversation_id = ?',
- whereArgs: [conversation.id],
- orderBy: 'admin',
- );
-
- return List.generate(maps.length, (i) {
- return ConversationUser(
- id: maps[i]['id'],
- userId: maps[i]['user_id'],
- conversationId: maps[i]['conversation_id'],
- username: maps[i]['username'],
- associationKey: maps[i]['association_key'],
- admin: maps[i]['admin'] == 1,
- );
- });
- }
-
- class ConversationUser{
- String id;
- String userId;
- String conversationId;
- String username;
- String associationKey;
- bool admin;
- ConversationUser({
- required this.id,
- required this.userId,
- required this.conversationId,
- required this.username,
- required this.associationKey,
- required this.admin,
- });
-
- factory ConversationUser.fromJson(Map<String, dynamic> json, String conversationId) {
- return ConversationUser(
- id: json['id'],
- userId: json['user_id'],
- conversationId: conversationId,
- username: json['username'],
- associationKey: json['association_key'],
- admin: json['admin'] == 'true',
- );
- }
-
- Map<String, dynamic> toJson() {
- return {
- 'id': id,
- 'user_id': userId,
- 'username': username,
- 'association_key': associationKey,
- 'admin': admin ? 'true' : 'false',
- };
- }
-
- Map<String, dynamic> toMap() {
- return {
- 'id': id,
- 'user_id': userId,
- 'conversation_id': conversationId,
- 'username': username,
- 'association_key': associationKey,
- 'admin': admin ? 1 : 0,
- };
- }
- }
|