fix: add DELETE and batch PUT endpoints for decks
This commit is contained in:
@@ -40,3 +40,28 @@ route('player/:steamId/decks/:index', ['PUT'], (ctx: HandlerContext) => {
|
||||
`).run(ctx.params.steamId, idx, name || 'My Deck', JSON.stringify(cards || []), name || 'My Deck', JSON.stringify(cards || []));
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
route('player/:steamId/decks/:index', ['DELETE'], (ctx: HandlerContext) => {
|
||||
const db = getDb();
|
||||
const idx = parseInt(ctx.params.index);
|
||||
db.prepare('DELETE FROM decks WHERE steam_id = ? AND deck_index = ?').run(ctx.params.steamId, idx);
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
route('player/:steamId/decks', ['PUT'], (ctx: HandlerContext) => {
|
||||
const { decks: decksToSave } = ctx.body as any;
|
||||
if (!decksToSave) throw new HttpError(400, 'decks required');
|
||||
const db = getDb();
|
||||
const upsert = db.prepare(`
|
||||
INSERT INTO decks (steam_id, deck_index, name, cards, updated_at) VALUES (?, ?, ?, ?, datetime('now'))
|
||||
ON CONFLICT(steam_id, deck_index) DO UPDATE SET name = ?, cards = ?, updated_at = datetime('now')
|
||||
`);
|
||||
const tx = db.transaction(() => {
|
||||
for (const [indexStr, deck] of Object.entries(decksToSave)) {
|
||||
const d = deck as any;
|
||||
upsert.run(ctx.params.steamId, parseInt(indexStr), d.name || 'My Deck', JSON.stringify(d.cards || []), d.name || 'My Deck', JSON.stringify(d.cards || []));
|
||||
}
|
||||
});
|
||||
tx();
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user