feat(db): recordView/listViews fuer recipe_view
INSERT OR REPLACE fuer idempotenten Bump des last_viewed_at Timestamps. listViews-Helper nur fuer Tests; Sort-Query laeuft direkt in listAllRecipesPaginated via LEFT JOIN. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
35
src/lib/server/recipes/views.ts
Normal file
35
src/lib/server/recipes/views.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import type Database from 'better-sqlite3';
|
||||
|
||||
export function recordView(
|
||||
db: Database.Database,
|
||||
profileId: number,
|
||||
recipeId: number
|
||||
): void {
|
||||
// INSERT OR REPLACE re-fires the DEFAULT (CURRENT_TIMESTAMP) on conflict,
|
||||
// so subsequent views of the same recipe by the same profile bump the
|
||||
// timestamp without breaking the composite PK.
|
||||
db.prepare(
|
||||
`INSERT OR REPLACE INTO recipe_view (profile_id, recipe_id)
|
||||
VALUES (?, ?)`
|
||||
).run(profileId, recipeId);
|
||||
}
|
||||
|
||||
export type ViewRow = {
|
||||
profile_id: number;
|
||||
recipe_id: number;
|
||||
last_viewed_at: string;
|
||||
};
|
||||
|
||||
export function listViews(
|
||||
db: Database.Database,
|
||||
profileId: number
|
||||
): ViewRow[] {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT profile_id, recipe_id, last_viewed_at
|
||||
FROM recipe_view
|
||||
WHERE profile_id = ?
|
||||
ORDER BY last_viewed_at DESC`
|
||||
)
|
||||
.all(profileId) as ViewRow[];
|
||||
}
|
||||
Reference in New Issue
Block a user