Skip to content

Commit

Permalink
Implement gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
ertrzyiks committed Jan 11, 2025
1 parent b30e0a6 commit 943d904
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- CreateTable
CREATE TABLE "RecipeGalleryImage" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"recipeId" INTEGER NOT NULL,
"imageUrl" TEXT NOT NULL,
"blurDataUrl" TEXT,
"position" INTEGER NOT NULL,
CONSTRAINT "RecipeGalleryImage_recipeId_fkey" FOREIGN KEY ("recipeId") REFERENCES "Recipe" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
2 changes: 1 addition & 1 deletion prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
# It should be added in your version-control system (e.g., Git)
provider = "sqlite"
10 changes: 10 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ model Recipe {
coverImage String
coverImageBlurDataUrl String?
title String
galleryImages RecipeGalleryImage[]
headline String
preparationTime Int
category Category @relation(fields: [categoryId], references: [id])
Expand All @@ -53,3 +54,12 @@ model Recipe {
instructions RecipeInstructionBlock[]
publishedAt DateTime
}

model RecipeGalleryImage {
id Int @id @default(autoincrement())
recipeId Int
recipe Recipe @relation(fields: [recipeId], references: [id])
imageUrl String
blurDataUrl String?
position Int
}
35 changes: 35 additions & 0 deletions scripts/import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,34 @@ async function createRecipeBlocks({ recipeId, ingredients, instructions }) {
});
}
}

async function createRecipeGalleryImages({ recipeId, gallery }) {
let position = 0;

for (const image of gallery) {
await prisma.recipeGalleryImage.create({
data: {
imageUrl: image.url,
blurDataUrl: image.placeholderBlurDataUrl,
recipe: {
connect: {
id: recipeId,
},
},
position: position++,
},
});
}
}

async function createOrUpdateRecipes({
title,
slug,
category,
coverImage,
coverImageBlurDataUrl,
headline,
gallery,
preparationTime,
publishedAt,
ingredients,
Expand All @@ -125,6 +146,12 @@ async function createOrUpdateRecipes({
},
});

await prisma.recipeGalleryImage.deleteMany({
where: {
recipeId: recipe.id,
},
});

await prisma.recipe.update({
where: {
id: recipe.id,
Expand Down Expand Up @@ -153,6 +180,7 @@ async function createOrUpdateRecipes({
ingredients,
instructions,
});
await createRecipeGalleryImages({ recipeId: recipe.id, gallery });
return;
}

Expand All @@ -177,6 +205,7 @@ async function createOrUpdateRecipes({
});

await createRecipeBlocks({ recipeId: result.id, ingredients, instructions });
await createRecipeGalleryImages({ recipeId: result.id, gallery });
}

function loadCategories() {
Expand Down Expand Up @@ -223,6 +252,11 @@ function loadRecipes(after = null, limit) {
id
content
}
gallery {
id
url
placeholderBlurDataUrl
}
}
}
`,
Expand Down Expand Up @@ -283,6 +317,7 @@ async function main() {
category: { id: categoryMap[recipe.category.slug].id },
ingredients: recipe.ingredients,
instructions: recipe.instructions,
gallery: recipe.gallery,
publishedAt: recipe.publishedAt,
tags: dbTags,
});
Expand Down

0 comments on commit 943d904

Please sign in to comment.