1)修改数据库命名,不和业务使用重合。
2)修改user 表,uid为unique, 聊天室文件夹name unique移除
This commit is contained in:
@@ -1,68 +1,148 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
import '../../domain/entities/user.dart';
|
||||
|
||||
part 'user_dto.g.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:im_app/data/local/drift/app_database.dart';
|
||||
import 'package:im_app/domain/entities/user.dart';
|
||||
|
||||
/// 用户 DTO(Data Transfer Object)
|
||||
///
|
||||
/// local / remote 共用的数据传输对象,放在 data/models/。
|
||||
/// 提供与 Domain Entity [User] 之间的双向转换。
|
||||
///
|
||||
/// ## 数据流位置(本地存储场景)
|
||||
///
|
||||
/// ```
|
||||
/// 写入本地:
|
||||
/// LoginData.toEntity() → User
|
||||
/// → UserDto.fromEntity(user) → ★ UserDto ★ ← 你在这里
|
||||
/// → toJson() → SQLite / SharedPreferences
|
||||
///
|
||||
/// 读取本地:
|
||||
/// SQLite / SharedPreferences → JSON
|
||||
/// → ★ UserDto.fromJson() ★ ← 你在这里
|
||||
/// → UserDto.toEntity() → User
|
||||
/// → ViewModel.state → View
|
||||
/// ```
|
||||
///
|
||||
/// 注意:登录接口的 Response DTO 是 [LoginData](含 token),
|
||||
/// 本类用于纯用户信息的本地持久化,不含 token。
|
||||
@JsonSerializable()
|
||||
class UserDto {
|
||||
@JsonKey(name: 'user_id')
|
||||
final String userId;
|
||||
final String email;
|
||||
final int uid;
|
||||
final String? uuid;
|
||||
final int? lastOnline;
|
||||
final String? profilePic;
|
||||
final String? profilePicGaussian;
|
||||
final String? nickname;
|
||||
final String? avatar;
|
||||
final String? contact;
|
||||
final String? countryCode;
|
||||
final String? email;
|
||||
final String? recoveryEmail;
|
||||
final String? username;
|
||||
final String? bio;
|
||||
final int? relationship;
|
||||
final String? userAlias;
|
||||
final int? channelId;
|
||||
final int? channelGroupId;
|
||||
final String? hint;
|
||||
|
||||
const UserDto({
|
||||
required this.userId,
|
||||
required this.email,
|
||||
required this.uid,
|
||||
this.uuid,
|
||||
this.lastOnline,
|
||||
this.profilePic,
|
||||
this.profilePicGaussian,
|
||||
this.nickname,
|
||||
this.avatar,
|
||||
this.contact,
|
||||
this.countryCode,
|
||||
this.email,
|
||||
this.recoveryEmail,
|
||||
this.username,
|
||||
this.bio,
|
||||
this.relationship,
|
||||
this.userAlias,
|
||||
this.channelId,
|
||||
this.channelGroupId,
|
||||
this.hint,
|
||||
});
|
||||
|
||||
factory UserDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$UserDtoFromJson(json);
|
||||
factory UserDto.fromJson(Map<String, dynamic> json) => UserDto(
|
||||
uid: json['uid'] as int,
|
||||
uuid: json['uuid'],
|
||||
lastOnline: json['last_online'],
|
||||
profilePic: json['profile_pic'],
|
||||
profilePicGaussian: json['profile_pic_gaussian'] ?? '',
|
||||
nickname: json['nickname'],
|
||||
contact: json['contact'],
|
||||
countryCode: json['country_code'],
|
||||
email: json['email'],
|
||||
recoveryEmail: json['recovery_email'] ?? '',
|
||||
username: json['username'],
|
||||
bio: json['bio'] ?? '',
|
||||
relationship: json['relationship'],
|
||||
userAlias: json['user_alias'],
|
||||
channelId: json['channel_id'],
|
||||
channelGroupId: json['channel_group_id'],
|
||||
hint: json['hint'],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => _$UserDtoToJson(this);
|
||||
Map<String, dynamic> toJson() => {
|
||||
'uid': uid,
|
||||
'uuid': uuid,
|
||||
'last_online': lastOnline,
|
||||
'profile_pic': profilePic,
|
||||
'profile_pic_gaussian': profilePicGaussian,
|
||||
'nickname': nickname,
|
||||
'contact': contact,
|
||||
'country_code': countryCode,
|
||||
'email': email,
|
||||
'recovery_email': recoveryEmail,
|
||||
'username': username,
|
||||
'bio': bio,
|
||||
'relationship': relationship,
|
||||
'user_alias': userAlias,
|
||||
'channel_id': channelId,
|
||||
'channel_group_id': channelGroupId,
|
||||
'hint': hint,
|
||||
};
|
||||
|
||||
/// DTO → Domain Entity
|
||||
User toEntity() {
|
||||
return User(
|
||||
id: userId,
|
||||
email: email,
|
||||
nickname: nickname,
|
||||
avatar: avatar,
|
||||
);
|
||||
}
|
||||
User toEntity() => User(
|
||||
uid: uid,
|
||||
uuid: uuid,
|
||||
lastOnline: lastOnline,
|
||||
profilePic: profilePic,
|
||||
profilePicGaussian: profilePicGaussian,
|
||||
nickname: nickname,
|
||||
contact: contact,
|
||||
countryCode: countryCode,
|
||||
email: email,
|
||||
recoveryEmail: recoveryEmail,
|
||||
username: username,
|
||||
bio: bio,
|
||||
relationship: relationship,
|
||||
userAlias: userAlias,
|
||||
channelId: channelId,
|
||||
channelGroupId: channelGroupId,
|
||||
hint: hint,
|
||||
);
|
||||
|
||||
/// Domain Entity → DTO
|
||||
factory UserDto.fromEntity(User user) {
|
||||
return UserDto(
|
||||
userId: user.id,
|
||||
email: user.email,
|
||||
nickname: user.nickname,
|
||||
avatar: user.avatar,
|
||||
);
|
||||
}
|
||||
}
|
||||
factory UserDto.fromEntity(User user) => UserDto(
|
||||
uid: user.uid,
|
||||
uuid: user.uuid,
|
||||
lastOnline: user.lastOnline,
|
||||
profilePic: user.profilePic,
|
||||
profilePicGaussian: user.profilePicGaussian,
|
||||
nickname: user.nickname,
|
||||
contact: user.contact,
|
||||
countryCode: user.countryCode,
|
||||
email: user.email,
|
||||
recoveryEmail: user.recoveryEmail,
|
||||
username: user.username,
|
||||
bio: user.bio,
|
||||
relationship: user.relationship,
|
||||
userAlias: user.userAlias,
|
||||
channelId: user.channelId,
|
||||
channelGroupId: user.channelGroupId,
|
||||
hint: user.hint,
|
||||
);
|
||||
|
||||
/// DTO → Drift Companion (for DB insert/update)
|
||||
UsersCompanion toCompanion() => UsersCompanion(
|
||||
uid: Value(uid),
|
||||
uuid: Value(uuid),
|
||||
lastOnline: Value(lastOnline),
|
||||
profilePic: Value(profilePic),
|
||||
profilePicGaussian: Value(profilePicGaussian ?? ''),
|
||||
nickname: Value(nickname),
|
||||
contact: Value(contact),
|
||||
countryCode: Value(countryCode),
|
||||
email: Value(email),
|
||||
recoveryEmail: Value(recoveryEmail),
|
||||
username: Value(username),
|
||||
bio: Value(bio),
|
||||
relationship: Value(relationship),
|
||||
userAlias: Value(userAlias),
|
||||
hint: Value(hint),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user