마스터 고객/제품/창고 테스트 및 UI 구현
This commit is contained in:
142
lib/features/masters/warehouse/data/dtos/warehouse_dto.dart
Normal file
142
lib/features/masters/warehouse/data/dtos/warehouse_dto.dart
Normal file
@@ -0,0 +1,142 @@
|
||||
import 'package:superport_v2/core/common/models/paginated_result.dart';
|
||||
|
||||
import '../../domain/entities/warehouse.dart';
|
||||
|
||||
class WarehouseDto {
|
||||
WarehouseDto({
|
||||
this.id,
|
||||
required this.warehouseCode,
|
||||
required this.warehouseName,
|
||||
this.zipcode,
|
||||
this.addressDetail,
|
||||
this.isActive = true,
|
||||
this.isDeleted = false,
|
||||
this.note,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
final int? id;
|
||||
final String warehouseCode;
|
||||
final String warehouseName;
|
||||
final WarehouseZipcodeDto? zipcode;
|
||||
final String? addressDetail;
|
||||
final bool isActive;
|
||||
final bool isDeleted;
|
||||
final String? note;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
factory WarehouseDto.fromJson(Map<String, dynamic> json) {
|
||||
return WarehouseDto(
|
||||
id: json['id'] as int?,
|
||||
warehouseCode: json['warehouse_code'] as String,
|
||||
warehouseName: json['warehouse_name'] as String,
|
||||
zipcode: json['zipcode'] is Map<String, dynamic>
|
||||
? WarehouseZipcodeDto.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,
|
||||
'warehouse_code': warehouseCode,
|
||||
'warehouse_name': warehouseName,
|
||||
'zipcode': zipcode?.toJson(),
|
||||
'address_detail': addressDetail,
|
||||
'is_active': isActive,
|
||||
'is_deleted': isDeleted,
|
||||
'note': note,
|
||||
'created_at': createdAt?.toIso8601String(),
|
||||
'updated_at': updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
Warehouse toEntity() => Warehouse(
|
||||
id: id,
|
||||
warehouseCode: warehouseCode,
|
||||
warehouseName: warehouseName,
|
||||
zipcode: zipcode?.toEntity(),
|
||||
addressDetail: addressDetail,
|
||||
isActive: isActive,
|
||||
isDeleted: isDeleted,
|
||||
note: note,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt,
|
||||
);
|
||||
|
||||
static PaginatedResult<Warehouse> parsePaginated(Map<String, dynamic>? json) {
|
||||
final items = (json?['items'] as List<dynamic>? ?? [])
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(WarehouseDto.fromJson)
|
||||
.map((dto) => dto.toEntity())
|
||||
.toList();
|
||||
return PaginatedResult<Warehouse>(
|
||||
items: items,
|
||||
page: json?['page'] as int? ?? 1,
|
||||
pageSize: json?['page_size'] as int? ?? items.length,
|
||||
total: json?['total'] as int? ?? items.length,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class WarehouseZipcodeDto {
|
||||
WarehouseZipcodeDto({
|
||||
required this.zipcode,
|
||||
this.sido,
|
||||
this.sigungu,
|
||||
this.roadName,
|
||||
});
|
||||
|
||||
final String zipcode;
|
||||
final String? sido;
|
||||
final String? sigungu;
|
||||
final String? roadName;
|
||||
|
||||
factory WarehouseZipcodeDto.fromJson(Map<String, dynamic> json) {
|
||||
return WarehouseZipcodeDto(
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
WarehouseZipcode toEntity() => WarehouseZipcode(
|
||||
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> warehouseInputToJson(WarehouseInput input) {
|
||||
final map = input.toPayload();
|
||||
map.removeWhere((key, value) => value == null);
|
||||
return map;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
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/warehouse.dart';
|
||||
import '../../domain/repositories/warehouse_repository.dart';
|
||||
import '../dtos/warehouse_dto.dart';
|
||||
|
||||
class WarehouseRepositoryRemote implements WarehouseRepository {
|
||||
WarehouseRepositoryRemote({required ApiClient apiClient}) : _api = apiClient;
|
||||
|
||||
final ApiClient _api;
|
||||
|
||||
static const _basePath = '/warehouses';
|
||||
|
||||
@override
|
||||
Future<PaginatedResult<Warehouse>> list({
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
String? query,
|
||||
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 (isActive != null) 'is_active': isActive,
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
return WarehouseDto.parsePaginated(response.data ?? const {});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Warehouse> create(WarehouseInput input) async {
|
||||
final response = await _api.post<Map<String, dynamic>>(
|
||||
_basePath,
|
||||
data: warehouseInputToJson(input),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return WarehouseDto.fromJson(data).toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Warehouse> update(int id, WarehouseInput input) async {
|
||||
final response = await _api.patch<Map<String, dynamic>>(
|
||||
'$_basePath/$id',
|
||||
data: warehouseInputToJson(input),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return WarehouseDto.fromJson(data).toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(int id) async {
|
||||
await _api.delete<void>('$_basePath/$id');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Warehouse> 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 WarehouseDto.fromJson(data).toEntity();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user