feat: extend character creation endpoint to accept full character data

This commit is contained in:
Aaron Wood 2026-04-11 11:51:33 -04:00
parent 9399fc2186
commit d8dbbabf7e

View file

@ -94,7 +94,18 @@ router.get<CampaignParams>("/", async (req, res) => {
router.post<CampaignParams>("/", requireAuth, async (req, res) => {
try {
const { campaignId } = req.params;
const { name, class: charClass, ancestry, hp_max } = req.body;
const {
name,
class: charClass,
ancestry,
hp_max,
alignment,
background,
deity,
title,
gp,
stats,
} = req.body;
if (!name?.trim()) {
res.status(400).json({ error: "Character name is required" });
@ -105,26 +116,36 @@ router.post<CampaignParams>("/", requireAuth, async (req, res) => {
const [result] = await db.execute<ResultSetHeader>(
`INSERT INTO characters
(campaign_id, user_id, name, class, ancestry, hp_current, hp_max, color)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
(campaign_id, user_id, name, class, ancestry, hp_current, hp_max,
alignment, background, deity, title, gp, color)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
campaignId,
userId,
name.trim(),
charClass ?? "Fighter",
ancestry ?? "Human",
hp_max ?? 0,
hp_max ?? 0,
hp_max ?? 1,
hp_max ?? 1,
alignment ?? "Neutral",
background ?? "",
deity ?? "",
title ?? "",
gp ?? 0,
generateCharacterColor(),
]
);
const characterId = result.insertId;
const statNames = ["STR", "DEX", "CON", "INT", "WIS", "CHA"];
const providedStats: Record<string, number> =
stats && typeof stats === "object" ? (stats as Record<string, number>) : {};
await Promise.all(
DEFAULT_STATS.map((stat) =>
statNames.map((stat) =>
db.execute(
"INSERT INTO character_stats (character_id, stat_name, value) VALUES (?, ?, 10)",
[characterId, stat]
"INSERT INTO character_stats (character_id, stat_name, value) VALUES (?, ?, ?)",
[characterId, stat, providedStats[stat] ?? 10]
)
)
);
@ -136,7 +157,7 @@ router.post<CampaignParams>("/", requireAuth, async (req, res) => {
const enriched = {
...charRows[0],
overrides: {},
stats: DEFAULT_STATS.map((s) => ({ stat_name: s, value: 10 })),
stats: statNames.map((s) => ({ stat_name: s, value: providedStats[s] ?? 10 })),
gear: [],
talents: [],
};