folx-tv/server/daily-recipe.ts
2026-08-15 17:28:45 +02:00

44 lines
1.4 KiB
TypeScript

import { db } from "./db";
import { dailyRecipes } from "@shared/schema";
import { eq, desc } 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;
}
export async function listRecipes(limit = 200): Promise<any[]> {
return await db.select().from(dailyRecipes).orderBy(desc(dailyRecipes.dateStr)).limit(limit);
}