사용자 마스터 UI 및 테스트 구현
This commit is contained in:
102
lib/features/masters/user/data/dtos/user_dto.dart
Normal file
102
lib/features/masters/user/data/dtos/user_dto.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'package:superport_v2/core/common/models/paginated_result.dart';
|
||||
|
||||
import '../../domain/entities/user.dart';
|
||||
|
||||
class UserDto {
|
||||
UserDto({
|
||||
this.id,
|
||||
required this.employeeNo,
|
||||
required this.employeeName,
|
||||
this.email,
|
||||
this.mobileNo,
|
||||
this.group,
|
||||
this.isActive = true,
|
||||
this.isDeleted = false,
|
||||
this.note,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
final int? id;
|
||||
final String employeeNo;
|
||||
final String employeeName;
|
||||
final String? email;
|
||||
final String? mobileNo;
|
||||
final UserGroupDto? group;
|
||||
final bool isActive;
|
||||
final bool isDeleted;
|
||||
final String? note;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
factory UserDto.fromJson(Map<String, dynamic> json) {
|
||||
return UserDto(
|
||||
id: json['id'] as int?,
|
||||
employeeNo: json['employee_no'] as String,
|
||||
employeeName: json['employee_name'] as String,
|
||||
email: json['email'] as String?,
|
||||
mobileNo: json['mobile_no'] as String?,
|
||||
group: json['group'] is Map<String, dynamic>
|
||||
? UserGroupDto.fromJson(json['group'] as Map<String, dynamic>)
|
||||
: null,
|
||||
isActive: (json['is_active'] as bool?) ?? true,
|
||||
isDeleted: (json['is_deleted'] as bool?) ?? false,
|
||||
note: json['note'] as String?,
|
||||
createdAt: _parseDate(json['created_at']),
|
||||
updatedAt: _parseDate(json['updated_at']),
|
||||
);
|
||||
}
|
||||
|
||||
UserAccount toEntity() => UserAccount(
|
||||
id: id,
|
||||
employeeNo: employeeNo,
|
||||
employeeName: employeeName,
|
||||
email: email,
|
||||
mobileNo: mobileNo,
|
||||
group: group?.toEntity(),
|
||||
isActive: isActive,
|
||||
isDeleted: isDeleted,
|
||||
note: note,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt,
|
||||
);
|
||||
|
||||
static PaginatedResult<UserAccount> parsePaginated(
|
||||
Map<String, dynamic>? json,
|
||||
) {
|
||||
final items = (json?['items'] as List<dynamic>? ?? [])
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(UserDto.fromJson)
|
||||
.map((dto) => dto.toEntity())
|
||||
.toList();
|
||||
return PaginatedResult<UserAccount>(
|
||||
items: items,
|
||||
page: json?['page'] as int? ?? 1,
|
||||
pageSize: json?['page_size'] as int? ?? items.length,
|
||||
total: json?['total'] as int? ?? items.length,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UserGroupDto {
|
||||
UserGroupDto({required this.id, required this.groupName});
|
||||
|
||||
final int id;
|
||||
final String groupName;
|
||||
|
||||
factory UserGroupDto.fromJson(Map<String, dynamic> json) {
|
||||
return UserGroupDto(
|
||||
id: json['id'] as int,
|
||||
groupName: json['group_name'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
UserGroup toEntity() => UserGroup(id: id, groupName: groupName);
|
||||
}
|
||||
|
||||
DateTime? _parseDate(Object? value) {
|
||||
if (value == null) return null;
|
||||
if (value is DateTime) return value;
|
||||
if (value is String) return DateTime.tryParse(value);
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:superport_v2/core/common/models/paginated_result.dart';
|
||||
import 'package:superport_v2/core/network/api_client.dart';
|
||||
|
||||
import '../../domain/entities/user.dart';
|
||||
import '../../domain/repositories/user_repository.dart';
|
||||
import '../dtos/user_dto.dart';
|
||||
|
||||
class UserRepositoryRemote implements UserRepository {
|
||||
UserRepositoryRemote({required ApiClient apiClient}) : _api = apiClient;
|
||||
|
||||
final ApiClient _api;
|
||||
|
||||
static const _basePath = '/employees';
|
||||
|
||||
@override
|
||||
Future<PaginatedResult<UserAccount>> list({
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
String? query,
|
||||
int? groupId,
|
||||
bool? isActive,
|
||||
}) async {
|
||||
final response = await _api.get<Map<String, dynamic>>(
|
||||
_basePath,
|
||||
query: {
|
||||
'page': page,
|
||||
'page_size': pageSize,
|
||||
if (query != null && query.isNotEmpty) 'q': query,
|
||||
if (groupId != null) 'group_id': groupId,
|
||||
if (isActive != null) 'is_active': isActive,
|
||||
'include': 'group',
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
return UserDto.parsePaginated(response.data ?? const {});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UserAccount> create(UserInput input) async {
|
||||
final response = await _api.post<Map<String, dynamic>>(
|
||||
_basePath,
|
||||
data: input.toPayload(),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return UserDto.fromJson(data).toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UserAccount> update(int id, UserInput input) async {
|
||||
final response = await _api.patch<Map<String, dynamic>>(
|
||||
'$_basePath/$id',
|
||||
data: input.toPayload(),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return UserDto.fromJson(data).toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(int id) async {
|
||||
await _api.delete<void>('$_basePath/$id');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UserAccount> restore(int id) async {
|
||||
final response = await _api.post<Map<String, dynamic>>(
|
||||
'$_basePath/$id/restore',
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return UserDto.fromJson(data).toEntity();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user