|
3 | 3 | import org.devkor.apu.saerok_server.domain.dex.bird.api.dto.response.BirdDetailResponse; |
4 | 4 | import org.devkor.apu.saerok_server.domain.dex.bird.api.dto.response.BirdFullSyncResponse; |
5 | 5 | import org.devkor.apu.saerok_server.domain.dex.bird.query.view.BirdProfileView; |
| 6 | +import org.devkor.apu.saerok_server.global.util.ImageDomainService; |
6 | 7 | import org.mapstruct.*; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
7 | 9 |
|
8 | 10 | import java.util.List; |
9 | 11 |
|
10 | 12 | @Mapper( |
11 | 13 | componentModel = MappingConstants.ComponentModel.SPRING |
12 | 14 | ) |
13 | | -public interface BirdProfileViewMapper { |
| 15 | +public abstract class BirdProfileViewMapper { |
14 | 16 |
|
15 | | - BirdFullSyncResponse.BirdProfileItem toDto(BirdProfileView birdProfileView); |
| 17 | + @Autowired |
| 18 | + private ImageDomainService imageDomainService; |
16 | 19 |
|
17 | | - List<BirdFullSyncResponse.BirdProfileItem> toDtoList(List<BirdProfileView> birdProfileViews); |
| 20 | + @Mapping(target = "images.s3Url", ignore = true) |
| 21 | + public abstract BirdFullSyncResponse.BirdProfileItem toDto(BirdProfileView birdProfileView); |
| 22 | + |
| 23 | + @AfterMapping |
| 24 | + protected void fillS3Urls( |
| 25 | + BirdProfileView source, |
| 26 | + @MappingTarget BirdFullSyncResponse.BirdProfileItem target |
| 27 | + ) { |
| 28 | + |
| 29 | + for (int i = 0; i < target.getImages().size(); i++) { |
| 30 | + target.getImages().get(i).setS3Url( |
| 31 | + imageDomainService.toDexImageUrl(source.getImages().get(i).getObjectKey()) |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public abstract List<BirdFullSyncResponse.BirdProfileItem> toDtoList(List<BirdProfileView> birdProfileViews); |
18 | 37 |
|
19 | 38 | @Mapping(source = "name.koreanName", target = "koreanName") |
20 | 39 | @Mapping(source = "name.scientificName", target = "scientificName") |
21 | 40 | @Mapping(source = "description.description", target = "description") |
22 | | - @Mapping(source = "images", target = "imageUrls", qualifiedByName = "extractImageUrls") |
| 41 | + @Mapping(target = "imageUrls", ignore = true) |
23 | 42 | @Mapping(target = "sizeCategory", ignore = true) |
24 | | - BirdDetailResponse toBirdDetailResponse(BirdProfileView view); |
25 | | - |
26 | | - @Named("extractImageUrls") |
27 | | - default List<String> extractImageUrls(List<BirdProfileView.Image> images) { |
28 | | - if (images == null) return List.of(); |
29 | | - return images.stream() |
30 | | - .map(BirdProfileView.Image::getS3Url) |
| 43 | + public abstract BirdDetailResponse toBirdDetailResponse(BirdProfileView view); |
| 44 | + |
| 45 | + @AfterMapping |
| 46 | + protected void fillImageUrls( |
| 47 | + BirdProfileView source, |
| 48 | + @MappingTarget BirdDetailResponse target |
| 49 | + ) { |
| 50 | + target.imageUrls = source.getImages().stream() |
| 51 | + .map(img -> imageDomainService.toDexImageUrl(img.getObjectKey())) |
31 | 52 | .toList(); |
32 | 53 | } |
33 | 54 |
|
34 | | - default Long toId(BirdProfileView birdProfileView) { |
| 55 | + protected Long toId(BirdProfileView birdProfileView) { |
35 | 56 | return birdProfileView == null ? null : birdProfileView.getId(); |
36 | 57 | } |
37 | 58 |
|
38 | | - List<Long> toIdList(List<BirdProfileView> birdProfileViews); |
| 59 | + public abstract List<Long> toIdList(List<BirdProfileView> birdProfileViews); |
39 | 60 |
|
40 | 61 | } |
0 commit comments