PIbd-31_MasenkinMS_PMU/lib/data/mappers/characters_mapper.dart
2024-09-30 22:15:55 +04:00

34 lines
1.0 KiB
Dart

import 'package:pmu_labworks/data/dtos/characters_dto.dart';
import 'package:pmu_labworks/domain/models/comment.dart';
import 'package:pmu_labworks/domain/models/home.dart';
import 'package:pmu_labworks/domain/models/user.dart';
extension CharactersDtoToModel on CharactersDto {
HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(),
nextPage: meta?.pagination?.next,
);
}
extension CharacterDataDtoToModel on CharacterDataDto {
CommentData toDomain() => CommentData(
title: type ?? 'UNKNOWN',
text: _makeDescriptionText(attributes?.born, attributes?.died),
user: UserData(
nickname: attributes?.name ?? 'Noname',
avatarUrl: attributes?.image),
);
String _makeDescriptionText(String? born, String? died) {
if (born != null && died != null) {
return '$born - $died';
} else if (born != null) {
return 'born: $born';
} else if (died != null) {
return 'died: $died';
} else {
return '';
}
}
}