본문 바로가기

Playlist/Content

ContentMapper

*/domain/content/mapper

 

import com.codeit.playlist.domain.content.dto.data.ContentDto;
import com.codeit.playlist.domain.content.entity.Content;
import com.codeit.playlist.domain.content.entity.Tag;
import com.codeit.playlist.global.constant.S3Properties;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.ArrayList;
import java.util.List;

@Mapper(componentModel = "spring")
public interface ContentMapper {
    @Mapping(target = "tags", expression = "java(changeTags(tag))")
    ContentDto toDto(Content content, List<Tag> tag);
    ContentDto toDto(Content content);

    @Mapping(target = "tags", expression = "java(changeTags(tag))")
    @Mapping(target = "thumbnailUrl", expression = "java(toS3Url(content.getThumbnailUrl(), s3Properties))")
    ContentDto toDtoUsingS3(Content content, List<Tag> tag, @Context S3Properties s3Properties);

    default List<String> changeTags(List<Tag> tags) {
        if(tags == null || tags.isEmpty()) {
            return new ArrayList<>();
        }

        return tags.stream().map(Tag::getName)
                .filter(name -> name != null && !name.isEmpty())
                .toList();
    }

    @Named("toS3Url")
    default String toS3Url(String key, @Context S3Properties s3Properties) {
        if(key == null || key.isBlank()) {
            return null;
        }
        if(key.startsWith("http://") || key.startsWith("https://")) {
            return key;
        }
        return "https://" + s3Properties.getContentBucket() + ".s3." + s3Properties.getRegion() + ".amazonaws.com/" + key;
    }
}

'Playlist > Content' 카테고리의 다른 글

TagRepository  (0) 2025.12.17
ContentRepository, Custom, Impl  (0) 2025.12.17
ContentException, ErrorCode  (0) 2025.12.17
Content, DTO  (0) 2025.12.17
ContentController  (0) 2025.12.17