feat: bulk tag editing, bulk export, and move recipes between collections
Extends the existing bulk-select infra: recipes list gets tag add/remove and multi-recipe Markdown export; collection pages get a select mode to move recipes to another collection or remove them from the current one. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -37,6 +37,7 @@ export async function PUT(req: NextRequest, { params }: Params) {
|
||||
addRecipeId: z.string().optional(),
|
||||
addRecipeIds: z.array(z.string()).max(200).optional(),
|
||||
removeRecipeId: z.string().optional(),
|
||||
removeRecipeIds: z.array(z.string()).max(200).optional(),
|
||||
}).safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
||||
|
||||
@@ -66,9 +67,10 @@ export async function PUT(req: NextRequest, { params }: Params) {
|
||||
}
|
||||
}
|
||||
|
||||
if (data.removeRecipeId) {
|
||||
const idsToRemove = [...(data.removeRecipeId ? [data.removeRecipeId] : []), ...(data.removeRecipeIds ?? [])];
|
||||
if (idsToRemove.length > 0) {
|
||||
await db.delete(collectionRecipes).where(
|
||||
and(eq(collectionRecipes.collectionId, id), eq(collectionRecipes.recipeId, data.removeRecipeId))
|
||||
and(eq(collectionRecipes.collectionId, id), inArray(collectionRecipes.recipeId, idsToRemove))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { db, recipes, eq, and, inArray } from "@epicure/db";
|
||||
import { recipeToMarkdown } from "@/lib/markdown/recipe";
|
||||
|
||||
const ExportSchema = z.object({
|
||||
ids: z.array(z.string()).min(1).max(100),
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
|
||||
const body = ExportSchema.safeParse(await req.json());
|
||||
if (!body.success) return NextResponse.json({ error: "Invalid request" }, { status: 400 });
|
||||
|
||||
const owned = await db.query.recipes.findMany({
|
||||
where: and(inArray(recipes.id, body.data.ids), eq(recipes.authorId, session!.user.id)),
|
||||
with: {
|
||||
ingredients: { orderBy: (t, { asc }) => asc(t.order) },
|
||||
steps: { orderBy: (t, { asc }) => asc(t.order) },
|
||||
batchDishes: { orderBy: (t, { asc }) => asc(t.order) },
|
||||
},
|
||||
});
|
||||
|
||||
if (owned.length === 0) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
const byId = new Map(owned.map((r) => [r.id, r]));
|
||||
const ordered = body.data.ids.flatMap((id) => (byId.has(id) ? [byId.get(id)!] : []));
|
||||
|
||||
const markdown = ordered
|
||||
.map((recipe) =>
|
||||
recipeToMarkdown({
|
||||
title: recipe.title,
|
||||
description: recipe.description,
|
||||
baseServings: recipe.baseServings,
|
||||
prepMins: recipe.prepMins,
|
||||
cookMins: recipe.cookMins,
|
||||
difficulty: recipe.difficulty,
|
||||
sourceUrl: recipe.sourceUrl,
|
||||
ingredients: recipe.ingredients,
|
||||
steps: recipe.steps,
|
||||
isBatchCook: recipe.isBatchCook,
|
||||
batchDishes: recipe.batchDishes,
|
||||
})
|
||||
)
|
||||
.join("\n---\n\n");
|
||||
|
||||
return NextResponse.json({ markdown });
|
||||
}
|
||||
@@ -10,6 +10,8 @@ const BulkDeleteSchema = z.object({
|
||||
const BulkUpdateSchema = z.object({
|
||||
ids: z.array(z.string()).min(1).max(100),
|
||||
visibility: z.enum(["private", "unlisted", "public"]).optional(),
|
||||
addTags: z.array(z.string().min(1).max(40)).max(20).optional(),
|
||||
removeTags: z.array(z.string().min(1).max(40)).max(20).optional(),
|
||||
});
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
@@ -33,13 +35,33 @@ export async function PATCH(req: NextRequest) {
|
||||
const body = BulkUpdateSchema.safeParse(await req.json());
|
||||
if (!body.success) return NextResponse.json({ error: "Invalid request" }, { status: 400 });
|
||||
|
||||
const { ids, visibility } = body.data;
|
||||
if (!visibility) return NextResponse.json({ error: "Nothing to update" }, { status: 400 });
|
||||
const { ids, visibility, addTags, removeTags } = body.data;
|
||||
if (!visibility && !addTags?.length && !removeTags?.length) {
|
||||
return NextResponse.json({ error: "Nothing to update" }, { status: 400 });
|
||||
}
|
||||
|
||||
await db
|
||||
.update(recipes)
|
||||
.set({ visibility, updatedAt: new Date() })
|
||||
.where(and(inArray(recipes.id, ids), eq(recipes.authorId, session!.user.id)));
|
||||
if (visibility) {
|
||||
await db
|
||||
.update(recipes)
|
||||
.set({ visibility, updatedAt: new Date() })
|
||||
.where(and(inArray(recipes.id, ids), eq(recipes.authorId, session!.user.id)));
|
||||
}
|
||||
|
||||
if (addTags?.length || removeTags?.length) {
|
||||
const owned = await db.query.recipes.findMany({
|
||||
where: and(inArray(recipes.id, ids), eq(recipes.authorId, session!.user.id)),
|
||||
columns: { id: true, tags: true },
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
owned.map((r) => {
|
||||
let nextTags = r.tags;
|
||||
if (addTags?.length) nextTags = [...new Set([...nextTags, ...addTags])];
|
||||
if (removeTags?.length) nextTags = nextTags.filter((tag) => !removeTags.includes(tag));
|
||||
return db.update(recipes).set({ tags: nextTags, updatedAt: new Date() }).where(eq(recipes.id, r.id));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user