83 lines
2.6 KiB
Dart
83 lines
2.6 KiB
Dart
import 'package:json_annotation/json_annotation.dart';
|
||
import 'package:networks_sdk/networks_sdk.dart';
|
||
|
||
import '../../../core/foundation/api_paths.dart';
|
||
import '../../../domain/entities/user.dart';
|
||
|
||
part 'get_profile_request.g.dart';
|
||
|
||
/// # /user/profile — 获取用户资料(GET 请求示例)
|
||
///
|
||
/// 演示:GET 请求 + 无 body 参数的模式。
|
||
/// GET 请求的 toJson() 结果会自动作为 URL query parameters 发送。
|
||
///
|
||
/// ## 数据流位置
|
||
///
|
||
/// ```
|
||
/// UserRepositoryImpl.getProfile()
|
||
/// → _client.executeRequest( ★ GetProfileRequest ★ ) ← 你在这里
|
||
/// → 服务端 GET /user/profile
|
||
/// → 响应 JSON → ★ ProfileData ★ ← 也在这里
|
||
/// → ProfileData.toEntity() → User
|
||
/// ```
|
||
|
||
// ─────────────────────────────────────────────
|
||
// Response DTO
|
||
// ─────────────────────────────────────────────
|
||
|
||
/// 用户资料响应 DTO(只需反序列化,禁止生成无用的 toJson)
|
||
@JsonSerializable(createToJson: false)
|
||
class ProfileData {
|
||
@JsonKey(name: 'user_id')
|
||
final String userId;
|
||
final String email;
|
||
final String? nickname;
|
||
final String? avatar;
|
||
|
||
const ProfileData({
|
||
required this.userId,
|
||
required this.email,
|
||
this.nickname,
|
||
this.avatar,
|
||
});
|
||
|
||
factory ProfileData.fromJson(Map<String, dynamic> json) =>
|
||
_$ProfileDataFromJson(json);
|
||
|
||
/// DTO → Domain Entity
|
||
User toEntity() {
|
||
return User(
|
||
id: userId,
|
||
email: email,
|
||
nickname: nickname,
|
||
avatar: avatar,
|
||
);
|
||
}
|
||
}
|
||
|
||
// ─────────────────────────────────────────────
|
||
// Request
|
||
// ─────────────────────────────────────────────
|
||
|
||
/// 获取用户资料请求(GET,无参数)
|
||
///
|
||
/// GET 请求无 body,toJson() 返回空 map。
|
||
/// 如需 query 参数(如分页),添加字段即可,
|
||
/// toJson() 会自动将字段序列化为 URL query string。
|
||
@ApiRequest(
|
||
path: ApiPaths.userProfile,
|
||
method: HttpMethod.get,
|
||
responseType: ProfileData,
|
||
)
|
||
@JsonSerializable()
|
||
class GetProfileRequest extends ApiRequestable<ProfileData>
|
||
with _$GetProfileRequestApi {
|
||
GetProfileRequest();
|
||
|
||
factory GetProfileRequest.fromJson(Map<String, dynamic> json) =>
|
||
_$GetProfileRequestFromJson(json);
|
||
|
||
@override
|
||
Map<String, dynamic> toJson() => _$GetProfileRequestToJson(this);
|
||
}
|