meals-made-easy-api/src/main/java/app/mealsmadeeasy/api/recipe/spec/RecipeUpdateSpec.java
2026-01-15 22:18:08 -06:00

75 lines
2.5 KiB
Java

package app.mealsmadeeasy.api.recipe.spec;
import app.mealsmadeeasy.api.image.Image;
import app.mealsmadeeasy.api.recipe.Recipe;
import app.mealsmadeeasy.api.recipe.body.RecipeUpdateBody;
import lombok.Builder;
import lombok.Value;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
// For now, we cannot change slug after creation.
// In the future, we may be able to have redirects from
// old slugs to new slugs.
@Value
@Builder
public class RecipeUpdateSpec {
@Value
@Builder
public static class MainImageUpdateSpec {
String username;
String filename;
}
public static RecipeUpdateSpec from(RecipeUpdateBody body) {
final var b = RecipeUpdateSpec.builder()
.title(body.getTitle())
.preparationTime(body.getPreparationTime())
.cookingTime(body.getCookingTime())
.totalTime(body.getTotalTime())
.rawText(body.getRawText())
.isPublic(body.getIsPublic());
final @Nullable RecipeUpdateBody.MainImageUpdateBody mainImage = body.getMainImage();
if (mainImage != null) {
b.mainImage(
MainImageUpdateSpec.builder()
.username(mainImage.getUsername())
.filename(mainImage.getFilename())
.build()
);
}
return b.build();
}
// For testing convenience only.
@ApiStatus.Internal
public static RecipeUpdateSpec.RecipeUpdateSpecBuilder fromRecipeToBuilder(Recipe recipe) {
final var b = RecipeUpdateSpec.builder()
.title(recipe.getTitle())
.preparationTime(recipe.getPreparationTime())
.cookingTime(recipe.getCookingTime())
.totalTime(recipe.getTotalTime())
.rawText(recipe.getRawText())
.isPublic(recipe.getIsPublic());
final @Nullable Image mainImage = recipe.getMainImage();
if (recipe.getMainImage() != null) {
b.mainImage(MainImageUpdateSpec.builder()
.username(mainImage.getOwner().getUsername())
.filename(mainImage.getUserFilename())
.build()
);
}
return b;
}
String title;
@Nullable Integer preparationTime;
@Nullable Integer cookingTime;
@Nullable Integer totalTime;
String rawText;
Boolean isPublic;
@Nullable MainImageUpdateSpec mainImage;
}