42 lines
1.4 KiB
Java
42 lines
1.4 KiB
Java
package com.krylov.refound.mapper;
|
|
|
|
import com.krylov.refound.dto.PostRequest;
|
|
import com.krylov.refound.dto.PostResponse;
|
|
import com.krylov.refound.entity.Post;
|
|
import com.krylov.refound.enums.PostCategory;
|
|
import org.mapstruct.Mapper;
|
|
import org.mapstruct.Mapping;
|
|
import com.krylov.refound.entity.Image;
|
|
import org.mapstruct.Named;
|
|
import java.util.List;
|
|
|
|
@Mapper(componentModel = "spring")
|
|
public interface PostMapper {
|
|
|
|
@Mapping(source = "category", target = "category", qualifiedByName = "stringToPostCategory")
|
|
@Mapping(source = "rewardText", target = "reward")
|
|
Post toEntity(PostRequest request);
|
|
|
|
@Mapping(source = "user.id", target = "userId")
|
|
@Mapping(source = "user.login", target = "userEmail")
|
|
@Mapping(source = "images", target = "images", qualifiedByName = "imagesToUrls")
|
|
@Mapping(target = "category", expression = "java(post.getCategory().getDisplayName())")
|
|
@Mapping(source = "isReward", target = "reward")
|
|
@Mapping(source = "reward", target = "rewardText")
|
|
PostResponse toResponse(Post post);
|
|
|
|
@Named("imagesToUrls")
|
|
default List<String> imagesToUrls(List<Image> images) {
|
|
if (images == null) return List.of();
|
|
return images.stream()
|
|
.map(Image::getUrl)
|
|
.toList();
|
|
}
|
|
|
|
@Named("stringToPostCategory")
|
|
default PostCategory stringToPostCategory(String displayName) {
|
|
if (displayName == null) return null;
|
|
return PostCategory.fromDisplayName(displayName);
|
|
}
|
|
}
|