build share count

This commit is contained in:
2026-03-28 22:02:37 -05:00
parent d1bfba89a8
commit d3fb42e8ed
6 changed files with 36 additions and 4 deletions

View File

@@ -66,6 +66,10 @@ if (!memesCols.find((c) => c.name === 'ocr_text')) {
db.exec('ALTER TABLE memes ADD COLUMN ocr_text TEXT');
}
if (!memesCols.find((c) => c.name === 'share_count')) {
db.exec('ALTER TABLE memes ADD COLUMN share_count INTEGER NOT NULL DEFAULT 0');
}
// Indexes that depend on migrated columns — created after columns are guaranteed to exist
db.exec(`
CREATE INDEX IF NOT EXISTS idx_memes_collection_id ON memes(collection_id);

View File

@@ -214,6 +214,14 @@ export async function memesRoutes(app: FastifyInstance) {
}
);
// Record a share — no auth required, public action
app.post<{ Params: { id: string } }>('/api/memes/:id/share', async (req, reply) => {
const meme = getMemeById(req.params.id);
if (!meme) return reply.status(404).send({ error: 'Not found' });
db.prepare('UPDATE memes SET share_count = share_count + 1 WHERE id = ?').run(meme.id);
return { share_count: (meme.share_count ?? 0) + 1 };
});
// Delete meme (children cascade)
app.delete<{ Params: { id: string } }>(
'/api/memes/:id',

View File

@@ -11,6 +11,7 @@ export interface Meme {
parent_id: string | null;
collection_id: number | null;
ocr_text: string | null;
share_count: number;
created_at: string;
tags: string[];
}