PMU/lib/data/mappers/cats_mapper.dart
2024-12-11 02:50:15 +04:00

54 lines
1.8 KiB
Dart

import 'package:pmu/data/dtos/cats_dto.dart';
import 'package:pmu/domain/models/card.dart';
import '../../domain/models/home.dart';
const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension CatsDtoToModel on CatsDto {
HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(),
nextPage: data != null && data!.isNotEmpty ? 20 : null,
);
}
extension CatDataDtoToModel on CatDataDto {
CardData toDomain() => CardData(
name ?? 'UNKNOWN',
imageUrl: imageLink ?? _imagePlaceholder,
descriptionText: _makeDescriptionText(origin, length, minWeight,
maxWeight, minLifeExpectancy, maxLifeExpectancy),
id: name,
);
String _makeDescriptionText(String? origin, String? length, int? minWeight,
int? maxWeight, int? minLifeExpectancy, int? maxLifeExpectancy) {
String description = '';
if (origin != null) {
description += '\nOrigin: $origin';
}
if (length != null) {
description += '\nLength: $length';
}
minWeight != null && maxWeight != null
? description += '\nWeight: $minWeight - $maxWeight pounds'
: minWeight != null
? description += '\nMin weight: $minWeight pounds'
: maxWeight != null
? description += '\nMax weight: $maxWeight pounds'
: '';
minLifeExpectancy != null && maxLifeExpectancy != null
? description += '\nLife expectancy: $minLifeExpectancy - $maxLifeExpectancy years'
: minLifeExpectancy != null
? description += '\nMin life expectancy: $minLifeExpectancy years'
: maxLifeExpectancy != null
? description += '\nMax life expectancy: $maxLifeExpectancy years'
: '';
return description;
}
}