meals-made-easy-api/src/main/java/app/mealsmadeeasy/api/recipe/converter/RecipeDraftToViewConverter.java
2026-01-28 14:00:48 -06:00

55 lines
2.4 KiB
Java

package app.mealsmadeeasy.api.recipe.converter;
import app.mealsmadeeasy.api.image.converter.ImageToViewConverter;
import app.mealsmadeeasy.api.image.view.ImageView;
import app.mealsmadeeasy.api.recipe.RecipeDraft;
import app.mealsmadeeasy.api.recipe.view.RecipeDraftView;
import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.view.UserInfoView;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;
import java.util.Comparator;
@Component
@RequiredArgsConstructor
public class RecipeDraftToViewConverter {
private final ImageToViewConverter imageToViewConverter;
public RecipeDraftView convert(RecipeDraft recipeDraft, User viewer) {
final @Nullable ImageView mainImageView = recipeDraft.getMainImage() != null
? this.imageToViewConverter.convert(recipeDraft.getMainImage(), viewer, false)
: null;
final @Nullable RecipeDraftView.RecipeDraftInferenceView lastInference = recipeDraft.getInferences() != null
? recipeDraft.getInferences().stream()
.max(Comparator.comparing(RecipeDraft.RecipeDraftInference::getInferredAt))
.map(inference -> RecipeDraftView.RecipeDraftInferenceView.builder()
.inferredAt(inference.getInferredAt())
.title(inference.getTitle())
.rawText(inference.getRawText())
.build()
)
.orElse(null)
: null;
return RecipeDraftView.builder()
.id(recipeDraft.getId())
.created(recipeDraft.getCreated())
.modified(recipeDraft.getModified())
.state(recipeDraft.getState())
.title(recipeDraft.getTitle())
.slug(recipeDraft.getSlug())
.preparationTime(recipeDraft.getPreparationTime())
.cookingTime(recipeDraft.getCookingTime())
.totalTime(recipeDraft.getTotalTime())
.rawText(recipeDraft.getRawText())
.ingredients(recipeDraft.getIngredients())
.owner(UserInfoView.from(recipeDraft.getOwner()))
.mainImage(mainImageView)
.lastInference(lastInference)
.build();
}
}