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

37 lines
1.0 KiB
Dart
Raw Permalink Normal View History

2024-10-11 19:12:12 +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',
imageUrl: attributes?.image ?? _imagePlaceholder,
descriptionText:
_makeDescriptionText(attributes?.born, attributes?.died),
);
}
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 '';
}