Added basic dev-data and loading mechanism.

This commit is contained in:
Jesse Brault 2024-07-31 17:39:31 -05:00
parent c89524a989
commit 81424eb2e6
5 changed files with 92 additions and 41 deletions

View File

@ -60,6 +60,7 @@ dependencies {
implementation 'org.commonmark:commonmark:0.22.0'
implementation 'org.jsoup:jsoup:1.17.2'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.17.2'
implementation 'io.minio:minio:8.5.11'

BIN
dev-data/images/Obazda.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -0,0 +1,21 @@
---
title: German (Bavarian) Obazda
slug: obazda
isPublic: true
mainImage:
src: images/Obazda.jpg
title: Obazda
alt: Obazda.
caption: German Obazda.
isPublic: true
---
## Ingredients
- Butter
- Camembert
- Scallions
- A few slices of red onion
- Salt and pepper
## Instructions
Mix all together until coarsely blended.
Serve cold with pretzels or other lye-type bread!

View File

@ -1,7 +1,6 @@
package app.mealsmadeeasy.api;
import app.mealsmadeeasy.api.image.Image;
import app.mealsmadeeasy.api.image.ImageException;
import app.mealsmadeeasy.api.image.ImageService;
import app.mealsmadeeasy.api.image.spec.ImageCreateInfoSpec;
import app.mealsmadeeasy.api.recipe.Recipe;
@ -9,6 +8,8 @@ import app.mealsmadeeasy.api.recipe.RecipeService;
import app.mealsmadeeasy.api.recipe.spec.RecipeCreateSpec;
import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
@ -16,9 +17,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.stream.Stream;
@Configuration
@Profile("dev")
@ -30,58 +34,82 @@ public class DevConfiguration {
private final RecipeService recipeService;
private final ImageService imageService;
private final ObjectMapper yamlObjectMapper = new ObjectMapper(new YAMLFactory());
public DevConfiguration(UserService userService, RecipeService recipeService, ImageService imageService) {
this.userService = userService;
this.recipeService = recipeService;
this.imageService = imageService;
}
private static final class ImageInfo {
public String src;
public String title;
public String alt;
public String caption;
public boolean isPublic;
}
private static final class RecipeFrontMatter {
public String title;
public String slug;
public boolean isPublic;
public ImageInfo mainImage;
}
@Bean
public CommandLineRunner addTestData() {
return args -> {
final User testUser = this.userService.createUser(
"test", "test@test.com", "test", Set.of()
"test-user", "test-user@test.com", "test", Set.of()
);
logger.info("Created {}", testUser);
logger.info("Created testUser: {}", testUser);
final ImageCreateInfoSpec obazdaCreateSpec = new ImageCreateInfoSpec();
obazdaCreateSpec.setAlt("Obazda");
obazdaCreateSpec.setCaption("German Obazda.");
obazdaCreateSpec.setPublic(true);
final Image obazdaImage;
try (final InputStream obazdaStream = DevConfiguration.class.getResourceAsStream("Obazda.jpg")) {
obazdaImage = this.imageService.create(
testUser,
"Obazda.jpg",
obazdaStream,
48654L,
obazdaCreateSpec
);
}
logger.info("Created {}", obazdaImage);
try (final Stream<Path> recipePaths = Files.walk(Path.of("dev-data/recipes"))) {
for (final Path recipePath : recipePaths.toList()) {
if (Files.isDirectory(recipePath)) {
continue;
}
final RecipeCreateSpec recipeCreateSpec = new RecipeCreateSpec();
recipeCreateSpec.setSlug("test-recipe");
recipeCreateSpec.setTitle("Test Recipe");
recipeCreateSpec.setRawText("Hello, World!");
recipeCreateSpec.setPublic(true);
recipeCreateSpec.setMainImage(obazdaImage);
final Recipe recipe = this.recipeService.create(testUser, recipeCreateSpec);
logger.info("Created {}", recipe);
final String fullText = Files.readString(recipePath);
final String[] parts = fullText.split("---");
if (parts.length != 3) {
throw new IllegalArgumentException("Invalid recipe file: " + recipePath);
}
final String rawFrontMatter = parts[1];
final String rawRecipeText = parts[2];
final RecipeFrontMatter frontMatter = this.yamlObjectMapper.readValue(
rawFrontMatter, RecipeFrontMatter.class
);
try (final InputStream inputStream = DevConfiguration.class.getResourceAsStream("HAL9000.svg")) {
final ImageCreateInfoSpec imageCreateSpec = new ImageCreateInfoSpec();
imageCreateSpec.setPublic(true);
final Image image = this.imageService.create(
testUser,
"HAL9000.svg",
inputStream,
27881L,
imageCreateSpec
);
logger.info("Created {}", image);
} catch (IOException | ImageException e) {
logger.error("Failed to load and/or create HAL9000.svg", e);
final ImageCreateInfoSpec imageCreateSpec = new ImageCreateInfoSpec();
imageCreateSpec.setAlt(frontMatter.mainImage.alt);
imageCreateSpec.setCaption(frontMatter.mainImage.caption);
imageCreateSpec.setPublic(frontMatter.mainImage.isPublic);
final Path givenPath = Path.of(frontMatter.mainImage.src);
final Path resolvedPath = Path.of("dev-data").resolve(givenPath);
final Image mainImage;
try (final InputStream inputStream = new FileInputStream(resolvedPath.toFile())) {
final long size = Files.size(resolvedPath);
mainImage = this.imageService.create(
testUser,
givenPath.getName(givenPath.getNameCount() - 1).toString(),
inputStream,
size,
imageCreateSpec
);
logger.info("Created mainImage {} for {}", mainImage, recipePath);
}
final RecipeCreateSpec recipeCreateSpec = new RecipeCreateSpec();
recipeCreateSpec.setSlug(frontMatter.slug);
recipeCreateSpec.setTitle(frontMatter.title);
recipeCreateSpec.setRawText(rawRecipeText);
recipeCreateSpec.setPublic(frontMatter.isPublic);
recipeCreateSpec.setMainImage(mainImage);
final Recipe recipe = this.recipeService.create(testUser, recipeCreateSpec);
logger.info("Created recipe {}", recipe);
}
}
};
}

View File

@ -33,12 +33,13 @@ public final class RecipeEntity implements Recipe {
@Column(nullable = false)
private String title;
@Column(nullable = false)
@Lob
@Column(name = "raw_text", columnDefinition = "TEXT", nullable = false)
@Basic(fetch = FetchType.LAZY)
private String rawText;
@Lob
@Column(name = "cached_rendered_text", columnDefinition = "TEXT")
@Basic(fetch = FetchType.LAZY)
private String cachedRenderedText;