마스터 고객/제품/창고 테스트 및 UI 구현
This commit is contained in:
171
lib/features/masters/product/data/dtos/product_dto.dart
Normal file
171
lib/features/masters/product/data/dtos/product_dto.dart
Normal file
@@ -0,0 +1,171 @@
|
||||
import 'package:superport_v2/core/common/models/paginated_result.dart';
|
||||
|
||||
import '../../domain/entities/product.dart';
|
||||
|
||||
class ProductDto {
|
||||
ProductDto({
|
||||
this.id,
|
||||
required this.productCode,
|
||||
required this.productName,
|
||||
this.vendor,
|
||||
this.uom,
|
||||
this.isActive = true,
|
||||
this.isDeleted = false,
|
||||
this.note,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
final int? id;
|
||||
final String productCode;
|
||||
final String productName;
|
||||
final ProductVendorDto? vendor;
|
||||
final ProductUomDto? uom;
|
||||
final bool isActive;
|
||||
final bool isDeleted;
|
||||
final String? note;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
factory ProductDto.fromJson(Map<String, dynamic> json) {
|
||||
return ProductDto(
|
||||
id: json['id'] as int?,
|
||||
productCode: json['product_code'] as String,
|
||||
productName: json['product_name'] as String,
|
||||
vendor: json['vendor'] is Map<String, dynamic>
|
||||
? ProductVendorDto.fromJson(json['vendor'] as Map<String, dynamic>)
|
||||
: null,
|
||||
uom: json['uom'] is Map<String, dynamic>
|
||||
? ProductUomDto.fromJson(json['uom'] 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']),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'product_code': productCode,
|
||||
'product_name': productName,
|
||||
'vendor': vendor?.toJson(),
|
||||
'uom': uom?.toJson(),
|
||||
'is_active': isActive,
|
||||
'is_deleted': isDeleted,
|
||||
'note': note,
|
||||
'created_at': createdAt?.toIso8601String(),
|
||||
'updated_at': updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
Product toEntity() => Product(
|
||||
id: id,
|
||||
productCode: productCode,
|
||||
productName: productName,
|
||||
vendor: vendor?.toEntity(),
|
||||
uom: uom?.toEntity(),
|
||||
isActive: isActive,
|
||||
isDeleted: isDeleted,
|
||||
note: note,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt,
|
||||
);
|
||||
|
||||
static ProductDto fromEntity(Product entity) => ProductDto(
|
||||
id: entity.id,
|
||||
productCode: entity.productCode,
|
||||
productName: entity.productName,
|
||||
vendor: entity.vendor == null
|
||||
? null
|
||||
: ProductVendorDto(
|
||||
id: entity.vendor!.id,
|
||||
vendorCode: entity.vendor!.vendorCode,
|
||||
vendorName: entity.vendor!.vendorName,
|
||||
),
|
||||
uom: entity.uom == null
|
||||
? null
|
||||
: ProductUomDto(id: entity.uom!.id, uomName: entity.uom!.uomName),
|
||||
isActive: entity.isActive,
|
||||
isDeleted: entity.isDeleted,
|
||||
note: entity.note,
|
||||
createdAt: entity.createdAt,
|
||||
updatedAt: entity.updatedAt,
|
||||
);
|
||||
|
||||
static PaginatedResult<Product> parsePaginated(Map<String, dynamic>? json) {
|
||||
final items = (json?['items'] as List<dynamic>? ?? [])
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(ProductDto.fromJson)
|
||||
.map((dto) => dto.toEntity())
|
||||
.toList();
|
||||
return PaginatedResult<Product>(
|
||||
items: items,
|
||||
page: json?['page'] as int? ?? 1,
|
||||
pageSize: json?['page_size'] as int? ?? items.length,
|
||||
total: json?['total'] as int? ?? items.length,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ProductVendorDto {
|
||||
ProductVendorDto({
|
||||
required this.id,
|
||||
required this.vendorCode,
|
||||
required this.vendorName,
|
||||
});
|
||||
|
||||
final int id;
|
||||
final String vendorCode;
|
||||
final String vendorName;
|
||||
|
||||
factory ProductVendorDto.fromJson(Map<String, dynamic> json) {
|
||||
return ProductVendorDto(
|
||||
id: json['id'] as int,
|
||||
vendorCode: json['vendor_code'] as String,
|
||||
vendorName: json['vendor_name'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {'id': id, 'vendor_code': vendorCode, 'vendor_name': vendorName};
|
||||
}
|
||||
|
||||
ProductVendor toEntity() =>
|
||||
ProductVendor(id: id, vendorCode: vendorCode, vendorName: vendorName);
|
||||
}
|
||||
|
||||
class ProductUomDto {
|
||||
ProductUomDto({required this.id, required this.uomName});
|
||||
|
||||
final int id;
|
||||
final String uomName;
|
||||
|
||||
factory ProductUomDto.fromJson(Map<String, dynamic> json) {
|
||||
return ProductUomDto(
|
||||
id: json['id'] as int,
|
||||
uomName: json['uom_name'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {'id': id, 'uom_name': uomName};
|
||||
}
|
||||
|
||||
ProductUom toEntity() => ProductUom(id: id, uomName: uomName);
|
||||
}
|
||||
|
||||
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> productInputToJson(ProductInput input) {
|
||||
final map = input.toPayload();
|
||||
map.removeWhere((key, value) => value == null);
|
||||
return map;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
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/product.dart';
|
||||
import '../../domain/repositories/product_repository.dart';
|
||||
import '../dtos/product_dto.dart';
|
||||
|
||||
class ProductRepositoryRemote implements ProductRepository {
|
||||
ProductRepositoryRemote({required ApiClient apiClient}) : _api = apiClient;
|
||||
|
||||
final ApiClient _api;
|
||||
|
||||
static const _basePath = '/products';
|
||||
|
||||
@override
|
||||
Future<PaginatedResult<Product>> list({
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
String? query,
|
||||
int? vendorId,
|
||||
int? uomId,
|
||||
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 (vendorId != null) 'vendor_id': vendorId,
|
||||
if (uomId != null) 'uom_id': uomId,
|
||||
if (isActive != null) 'is_active': isActive,
|
||||
'include': 'vendor,uom',
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
return ProductDto.parsePaginated(response.data ?? const {});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Product> create(ProductInput input) async {
|
||||
final response = await _api.post<Map<String, dynamic>>(
|
||||
_basePath,
|
||||
data: productInputToJson(input),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return ProductDto.fromJson(data).toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Product> update(int id, ProductInput input) async {
|
||||
final response = await _api.patch<Map<String, dynamic>>(
|
||||
'$_basePath/$id',
|
||||
data: productInputToJson(input),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return ProductDto.fromJson(data).toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(int id) async {
|
||||
await _api.delete<void>('$_basePath/$id');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Product> 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 ProductDto.fromJson(data).toEntity();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user