pmu_flutter/lec6/lib/data/mapper/characters_mapper.dart

38 lines
1.0 KiB
Dart
Raw Normal View History

2024-10-18 01:15:10 +04:00
import 'package:test_app/data/dto/characters_dto.dart';
import 'package:test_app/domain/card.dart';
import 'package:test_app/domain/home.dart';
const _imagePlaceholder =
'https://www.bellcold.com/wp-content/uploads/2024/04/Person-Placeholder-Icon.jpeg';
extension CharactersDtoToModel on CharactersDto {
HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(),
currentPage: meta?.pagination?.current,
nextPage: meta?.pagination?.next,
);
}
extension CharacterDtoToModel on CharacterDto {
CardData toDomain() => CardData(
attributes?.name ?? 'UNKNOWN',
descriptionText:
_makeDescriptionText(attributes?.born, attributes?.died),
id: id,
imageUrl: attributes?.image ?? _imagePlaceholder,
);
}
String _makeDescriptionText(String? born, String? died) {
if (born != null && died != null) {
return '$born - $died';
}
if (born != null) {
return 'born: $born';
}
if (died != null) {
return 'died: $died';
}
return '';
}