148 lines
3.9 KiB
Dart
148 lines
3.9 KiB
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] 之间的双向转换。
|
||
class UserDto {
|
||
final int uid;
|
||
final String? uuid;
|
||
final int? lastOnline;
|
||
final String? profilePic;
|
||
final String? profilePicGaussian;
|
||
final String? nickname;
|
||
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.uid,
|
||
this.uuid,
|
||
this.lastOnline,
|
||
this.profilePic,
|
||
this.profilePicGaussian,
|
||
this.nickname,
|
||
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) => 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() => {
|
||
'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() => 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) => 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),
|
||
);
|
||
} |