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;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { storage } from "./storage";
|
||||
import { insertArticleSchema } from "@shared/schema";
|
||||
import { seedDatabase } from "./seed";
|
||||
import { generateDailyHoroscopes, getHoroscopesForToday, getOrGenerateHoroscope, setHoroscopeImages } from "./horoscope-generator";
|
||||
import { getRecipeForToday, upsertRecipe } from "./daily-recipe";
|
||||
import { startDailyScheduler } from "./scheduler";
|
||||
import { analyzeAllArticleImages, getCachedFocalPoints } from "./focal-point";
|
||||
import { optimizeImage } from "./image-optimizer";
|
||||
@@ -754,6 +755,26 @@ export async function registerRoutes(
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/api/recipes/today", async (_req, res) => {
|
||||
try {
|
||||
const recipe = await getRecipeForToday();
|
||||
res.json(recipe);
|
||||
} catch (err: any) {
|
||||
res.status(500).json({ message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/api/recipes", requireApiKey, async (req, res) => {
|
||||
try {
|
||||
const { name } = req.body || {};
|
||||
if (!name) return res.status(400).json({ message: "name ist erforderlich" });
|
||||
const row = await upsertRecipe(req.body);
|
||||
res.json(row);
|
||||
} catch (err: any) {
|
||||
res.status(500).json({ message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/api/horoscopes/today", async (_req, res) => {
|
||||
try {
|
||||
const horoscopes = await getHoroscopesForToday();
|
||||
|
||||
Reference in New Issue
Block a user