Dnevni recept: tabela daily_recipes, API /api/recipes, prikaz na /rezepte
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { db } from "./db";
|
||||
import { dailyRecipes } from "@shared/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
function todayStr(): string {
|
||||
const d = new Date();
|
||||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
export async function getRecipeForToday(): Promise<any | null> {
|
||||
const rows = await db.select().from(dailyRecipes).where(eq(dailyRecipes.dateStr, todayStr()));
|
||||
return rows.length > 0 ? rows[0] : null;
|
||||
}
|
||||
|
||||
export async function upsertRecipe(data: {
|
||||
dateStr?: string;
|
||||
name: string;
|
||||
intro?: string;
|
||||
recipe?: string;
|
||||
seasonNote?: string | null;
|
||||
imageUrl?: string | null;
|
||||
}): Promise<any> {
|
||||
const dateStr = data.dateStr || todayStr();
|
||||
const values = {
|
||||
dateStr,
|
||||
name: data.name,
|
||||
intro: data.intro || "",
|
||||
recipe: data.recipe || "",
|
||||
seasonNote: data.seasonNote || null,
|
||||
imageUrl: data.imageUrl || null,
|
||||
};
|
||||
const existing = await db.select().from(dailyRecipes).where(eq(dailyRecipes.dateStr, dateStr));
|
||||
if (existing.length > 0) {
|
||||
const [row] = await db.update(dailyRecipes).set(values).where(eq(dailyRecipes.id, existing[0].id)).returning();
|
||||
return row;
|
||||
}
|
||||
const [row] = await db.insert(dailyRecipes).values(values).returning();
|
||||
return row;
|
||||
}
|
||||
Reference in New Issue
Block a user