마스터 고객/제품/창고 테스트 및 UI 구현
This commit is contained in:
160
lib/features/masters/customer/data/dtos/customer_dto.dart
Normal file
160
lib/features/masters/customer/data/dtos/customer_dto.dart
Normal file
@@ -0,0 +1,160 @@
|
||||
import 'package:superport_v2/core/common/models/paginated_result.dart';
|
||||
|
||||
import '../../domain/entities/customer.dart';
|
||||
|
||||
class CustomerDto {
|
||||
CustomerDto({
|
||||
this.id,
|
||||
required this.customerCode,
|
||||
required this.customerName,
|
||||
this.isPartner = false,
|
||||
this.isGeneral = true,
|
||||
this.email,
|
||||
this.mobileNo,
|
||||
this.zipcode,
|
||||
this.addressDetail,
|
||||
this.isActive = true,
|
||||
this.isDeleted = false,
|
||||
this.note,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
final int? id;
|
||||
final String customerCode;
|
||||
final String customerName;
|
||||
final bool isPartner;
|
||||
final bool isGeneral;
|
||||
final String? email;
|
||||
final String? mobileNo;
|
||||
final CustomerZipcodeDto? zipcode;
|
||||
final String? addressDetail;
|
||||
final bool isActive;
|
||||
final bool isDeleted;
|
||||
final String? note;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
factory CustomerDto.fromJson(Map<String, dynamic> json) {
|
||||
return CustomerDto(
|
||||
id: json['id'] as int?,
|
||||
customerCode: json['customer_code'] as String,
|
||||
customerName: json['customer_name'] as String,
|
||||
isPartner: (json['is_partner'] as bool?) ?? false,
|
||||
isGeneral: (json['is_general'] as bool?) ?? true,
|
||||
email: json['email'] as String?,
|
||||
mobileNo: json['mobile_no'] as String?,
|
||||
zipcode: json['zipcode'] is Map<String, dynamic>
|
||||
? CustomerZipcodeDto.fromJson(json['zipcode'] as Map<String, dynamic>)
|
||||
: null,
|
||||
addressDetail: json['address_detail'] as String?,
|
||||
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']),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'customer_code': customerCode,
|
||||
'customer_name': customerName,
|
||||
'is_partner': isPartner,
|
||||
'is_general': isGeneral,
|
||||
'email': email,
|
||||
'mobile_no': mobileNo,
|
||||
'zipcode': zipcode?.toJson(),
|
||||
'address_detail': addressDetail,
|
||||
'is_active': isActive,
|
||||
'is_deleted': isDeleted,
|
||||
'note': note,
|
||||
'created_at': createdAt?.toIso8601String(),
|
||||
'updated_at': updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
Customer toEntity() => Customer(
|
||||
id: id,
|
||||
customerCode: customerCode,
|
||||
customerName: customerName,
|
||||
isPartner: isPartner,
|
||||
isGeneral: isGeneral,
|
||||
email: email,
|
||||
mobileNo: mobileNo,
|
||||
zipcode: zipcode?.toEntity(),
|
||||
addressDetail: addressDetail,
|
||||
isActive: isActive,
|
||||
isDeleted: isDeleted,
|
||||
note: note,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt,
|
||||
);
|
||||
|
||||
static PaginatedResult<Customer> parsePaginated(Map<String, dynamic>? json) {
|
||||
final items = (json?['items'] as List<dynamic>? ?? [])
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(CustomerDto.fromJson)
|
||||
.map((dto) => dto.toEntity())
|
||||
.toList();
|
||||
return PaginatedResult<Customer>(
|
||||
items: items,
|
||||
page: json?['page'] as int? ?? 1,
|
||||
pageSize: json?['page_size'] as int? ?? items.length,
|
||||
total: json?['total'] as int? ?? items.length,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CustomerZipcodeDto {
|
||||
CustomerZipcodeDto({
|
||||
required this.zipcode,
|
||||
this.sido,
|
||||
this.sigungu,
|
||||
this.roadName,
|
||||
});
|
||||
|
||||
final String zipcode;
|
||||
final String? sido;
|
||||
final String? sigungu;
|
||||
final String? roadName;
|
||||
|
||||
factory CustomerZipcodeDto.fromJson(Map<String, dynamic> json) {
|
||||
return CustomerZipcodeDto(
|
||||
zipcode: json['zipcode'] as String,
|
||||
sido: json['sido'] as String?,
|
||||
sigungu: json['sigungu'] as String?,
|
||||
roadName: json['road_name'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'zipcode': zipcode,
|
||||
'sido': sido,
|
||||
'sigungu': sigungu,
|
||||
'road_name': roadName,
|
||||
};
|
||||
}
|
||||
|
||||
CustomerZipcode toEntity() => CustomerZipcode(
|
||||
zipcode: zipcode,
|
||||
sido: sido,
|
||||
sigungu: sigungu,
|
||||
roadName: roadName,
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Map<String, dynamic> customerInputToJson(CustomerInput input) {
|
||||
final map = input.toPayload();
|
||||
map.removeWhere((key, value) => value == null);
|
||||
return map;
|
||||
}
|
||||
Reference in New Issue
Block a user