build share count
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user