2026-08-24 22:01:04 -04:00
|
|
|
import { sql } from '../../../lib/sql.js';
|
|
|
|
|
import { getUserFromRequest } from '../../../lib/permission-middleware';
|
|
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
|
try {
|
|
|
|
|
const user = await getUserFromRequest(req);
|
|
|
|
|
if (!user) {
|
|
|
|
|
return res.status(401).json({ error: 'Authentication required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const gameId = parseInt(req.query.id, 10);
|
|
|
|
|
if (!Number.isInteger(gameId)) {
|
|
|
|
|
return res.status(400).json({ error: 'Invalid game id' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const found = await sql`
|
|
|
|
|
SELECT * FROM custom_games
|
|
|
|
|
WHERE id = ${gameId} AND user_id = ${user.userId}
|
|
|
|
|
`;
|
|
|
|
|
if (found.rows.length === 0) {
|
|
|
|
|
return res.status(404).json({ error: 'Game not found' });
|
|
|
|
|
}
|
|
|
|
|
const game = found.rows[0];
|
|
|
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
|
const cards = await sql`
|
feat(designer): inline symbol icons, frame textures, community sharing, print sheets
- {CODE} tokens in description/actions/flavor render as inline symbol
icons (RichText), including inside cost pips
- custom frames gain an optional background texture (upload/replace/
remove via /api/custom-frames/[id]/texture); renders behind panels
- custom games can be shared to the community (is_public): toggle in the
game space, public listing at /community/games, read-only game view,
/api/public/games endpoints (no auth, public rows only)
- /designer/print: multi-card print sheets on US Letter at 300dpi
(63x88mm cards, 3x3 or 2x2, dashed cut guides, full-sheet PNG export)
- migration 1787711511000
2026-08-24 22:48:52 -04:00
|
|
|
SELECT c.id, c.card_id, c.name, c.mana_cost, c.card_type, c.rarity,
|
|
|
|
|
c.rules_text, c.actions, c.flavor_quote, c.power, c.toughness,
|
|
|
|
|
c.frame_id, c.artwork_url, c.art_mode,
|
|
|
|
|
f.id AS frame_pk, f.name AS frame_name, f.palette AS frame_palette,
|
|
|
|
|
f.texture_url AS frame_texture,
|
2026-09-01 10:23:16 -04:00
|
|
|
f.frame_image_url AS frame_image,
|
|
|
|
|
f.layout AS frame_layout,
|
feat(designer): inline symbol icons, frame textures, community sharing, print sheets
- {CODE} tokens in description/actions/flavor render as inline symbol
icons (RichText), including inside cost pips
- custom frames gain an optional background texture (upload/replace/
remove via /api/custom-frames/[id]/texture); renders behind panels
- custom games can be shared to the community (is_public): toggle in the
game space, public listing at /community/games, read-only game view,
/api/public/games endpoints (no auth, public rows only)
- /designer/print: multi-card print sheets on US Letter at 300dpi
(63x88mm cards, 3x3 or 2x2, dashed cut guides, full-sheet PNG export)
- migration 1787711511000
2026-08-24 22:48:52 -04:00
|
|
|
c.created_at, c.updated_at
|
|
|
|
|
FROM custom_cards c
|
|
|
|
|
LEFT JOIN custom_frames f ON f.id = c.custom_frame_id
|
|
|
|
|
WHERE c.custom_game_id = ${gameId}
|
|
|
|
|
ORDER BY c.updated_at DESC
|
2026-08-24 22:01:04 -04:00
|
|
|
`;
|
feat(designer): inline symbol icons, frame textures, community sharing, print sheets
- {CODE} tokens in description/actions/flavor render as inline symbol
icons (RichText), including inside cost pips
- custom frames gain an optional background texture (upload/replace/
remove via /api/custom-frames/[id]/texture); renders behind panels
- custom games can be shared to the community (is_public): toggle in the
game space, public listing at /community/games, read-only game view,
/api/public/games endpoints (no auth, public rows only)
- /designer/print: multi-card print sheets on US Letter at 300dpi
(63x88mm cards, 3x3 or 2x2, dashed cut guides, full-sheet PNG export)
- migration 1787711511000
2026-08-24 22:48:52 -04:00
|
|
|
const designs = cards.rows.map((row) => {
|
2026-09-01 10:23:16 -04:00
|
|
|
const { frame_pk, frame_name, frame_palette, frame_texture, frame_image, frame_layout, ...rest } = row;
|
feat(designer): inline symbol icons, frame textures, community sharing, print sheets
- {CODE} tokens in description/actions/flavor render as inline symbol
icons (RichText), including inside cost pips
- custom frames gain an optional background texture (upload/replace/
remove via /api/custom-frames/[id]/texture); renders behind panels
- custom games can be shared to the community (is_public): toggle in the
game space, public listing at /community/games, read-only game view,
/api/public/games endpoints (no auth, public rows only)
- /designer/print: multi-card print sheets on US Letter at 300dpi
(63x88mm cards, 3x3 or 2x2, dashed cut guides, full-sheet PNG export)
- migration 1787711511000
2026-08-24 22:48:52 -04:00
|
|
|
return {
|
|
|
|
|
...rest,
|
|
|
|
|
custom_frame:
|
|
|
|
|
frame_pk != null
|
|
|
|
|
? {
|
|
|
|
|
id: frame_pk,
|
|
|
|
|
name: frame_name,
|
|
|
|
|
palette: frame_palette,
|
|
|
|
|
texture_url: frame_texture,
|
2026-09-01 10:23:16 -04:00
|
|
|
frame_image_url: frame_image,
|
|
|
|
|
layout: frame_layout,
|
feat(designer): inline symbol icons, frame textures, community sharing, print sheets
- {CODE} tokens in description/actions/flavor render as inline symbol
icons (RichText), including inside cost pips
- custom frames gain an optional background texture (upload/replace/
remove via /api/custom-frames/[id]/texture); renders behind panels
- custom games can be shared to the community (is_public): toggle in the
game space, public listing at /community/games, read-only game view,
/api/public/games endpoints (no auth, public rows only)
- /designer/print: multi-card print sheets on US Letter at 300dpi
(63x88mm cards, 3x3 or 2x2, dashed cut guides, full-sheet PNG export)
- migration 1787711511000
2026-08-24 22:48:52 -04:00
|
|
|
}
|
|
|
|
|
: null,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
return res.status(200).json({ game, designs });
|
2026-08-24 22:01:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.method === 'PUT') {
|
|
|
|
|
const name = typeof req.body?.name === 'string' ? req.body.name.trim() : '';
|
|
|
|
|
const description =
|
|
|
|
|
typeof req.body?.description === 'string' ? req.body.description.trim() : null;
|
feat(designer): inline symbol icons, frame textures, community sharing, print sheets
- {CODE} tokens in description/actions/flavor render as inline symbol
icons (RichText), including inside cost pips
- custom frames gain an optional background texture (upload/replace/
remove via /api/custom-frames/[id]/texture); renders behind panels
- custom games can be shared to the community (is_public): toggle in the
game space, public listing at /community/games, read-only game view,
/api/public/games endpoints (no auth, public rows only)
- /designer/print: multi-card print sheets on US Letter at 300dpi
(63x88mm cards, 3x3 or 2x2, dashed cut guides, full-sheet PNG export)
- migration 1787711511000
2026-08-24 22:48:52 -04:00
|
|
|
const isPublic =
|
|
|
|
|
typeof req.body?.is_public === 'boolean' ? req.body.is_public : game.is_public;
|
2026-08-24 22:01:04 -04:00
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
|
return res.status(400).json({ error: 'Game name is required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const clash = await sql`
|
|
|
|
|
SELECT id FROM custom_games
|
|
|
|
|
WHERE user_id = ${user.userId}
|
|
|
|
|
AND lower(name) = ${name.toLowerCase()}
|
|
|
|
|
AND id <> ${gameId}
|
|
|
|
|
`;
|
|
|
|
|
if (clash.rows.length > 0) {
|
|
|
|
|
return res.status(409).json({ error: 'You already have a game with that name' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updated = await sql`
|
|
|
|
|
UPDATE custom_games SET
|
feat(designer): inline symbol icons, frame textures, community sharing, print sheets
- {CODE} tokens in description/actions/flavor render as inline symbol
icons (RichText), including inside cost pips
- custom frames gain an optional background texture (upload/replace/
remove via /api/custom-frames/[id]/texture); renders behind panels
- custom games can be shared to the community (is_public): toggle in the
game space, public listing at /community/games, read-only game view,
/api/public/games endpoints (no auth, public rows only)
- /designer/print: multi-card print sheets on US Letter at 300dpi
(63x88mm cards, 3x3 or 2x2, dashed cut guides, full-sheet PNG export)
- migration 1787711511000
2026-08-24 22:48:52 -04:00
|
|
|
name = ${name}, description = ${description}, is_public = ${isPublic},
|
2026-08-24 22:01:04 -04:00
|
|
|
updated_at = CURRENT_TIMESTAMP
|
|
|
|
|
WHERE id = ${gameId}
|
|
|
|
|
RETURNING *
|
|
|
|
|
`;
|
|
|
|
|
return res.status(200).json({ game: updated.rows[0] });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.method === 'DELETE') {
|
|
|
|
|
// Designs keep existing (custom_game_id drops to NULL via FK);
|
|
|
|
|
// their catalog twins stay in the collection untouched.
|
|
|
|
|
await sql`DELETE FROM custom_games WHERE id = ${gameId}`;
|
|
|
|
|
return res.status(200).json({ message: 'Game deleted' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Custom game API error:', error);
|
|
|
|
|
return res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
}
|
|
|
|
|
}
|