fixed category in posts

This commit is contained in:
SlimusMinus
2026-06-11 00:52:56 +03:00
parent 5692d8425e
commit e4b07fcbff
5 changed files with 45 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ 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;
@@ -12,10 +13,12 @@ import java.util.List;
@Mapper(componentModel = "spring")
public interface PostMapper {
@Mapping(source = "category", target = "category", qualifiedByName = "stringToPostCategory")
Post toEntity(PostRequest request);
@Mapping(source = "user.login", target = "userEmail")
@Mapping(source = "images", target = "images", qualifiedByName = "imagesToUrls")
@Mapping(target = "category", expression = "java(post.getCategory().getDisplayName())")
PostResponse toResponse(Post post);
@Named("imagesToUrls")
@@ -25,4 +28,10 @@ public interface PostMapper {
.map(Image::getUrl)
.toList();
}
@Named("stringToPostCategory")
default PostCategory stringToPostCategory(String displayName) {
if (displayName == null) return null;
return PostCategory.fromDisplayName(displayName);
}
}