step 5.2: done
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// Inventory routes: list, equip, unequip.
|
||||
const zod = require('zod');
|
||||
const { AppError } = require('../utils/errors');
|
||||
const { requireAuth } = require('../middleware/session');
|
||||
const { getCharacterIdForUser } = require('../services/characterService');
|
||||
const { listInventory, equipItem, unequipItem } = require('../services/inventoryService');
|
||||
|
||||
// Validation schemas
|
||||
const slotSchema = zod.enum(['weapon', 'armor', 'trinket']);
|
||||
|
||||
function wrap(fn) {
|
||||
return (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/inventory — list all items, equipped gear, and derived stats.
|
||||
*/
|
||||
async function handleListInventory(req, _res) {
|
||||
const characterId = getCharacterIdForUser(req.session.userId);
|
||||
const result = listInventory(characterId);
|
||||
res.json({ ok: true, data: result });
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/inventory/equip — equip an item into a slot.
|
||||
*/
|
||||
async function handleEquip(req, res) {
|
||||
const parsed = zod.strictObject({
|
||||
itemId: zod.number().int().positive(),
|
||||
slot: slotSchema,
|
||||
}).safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
throw new AppError('VALIDATION_ERROR', 'Request validation failed.', 400);
|
||||
}
|
||||
|
||||
const { itemId, slot } = parsed.data;
|
||||
const characterId = getCharacterIdForUser(req.session.userId);
|
||||
const result = equipItem(characterId, itemId, slot);
|
||||
res.json({ ok: true, data: result });
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/inventory/unequip — unequip an item from a slot.
|
||||
*/
|
||||
async function handleUnequip(req, res) {
|
||||
const parsed = zod.strictObject({
|
||||
slot: slotSchema,
|
||||
}).safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
throw new AppError('VALIDATION_ERROR', 'Request validation failed.', 400);
|
||||
}
|
||||
|
||||
const { slot } = parsed.data;
|
||||
const characterId = getCharacterIdForUser(req.session.userId);
|
||||
const result = unequipItem(characterId, slot);
|
||||
res.json({ ok: true, data: result });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
routes: {
|
||||
getInventory: wrap(handleListInventory),
|
||||
postEquip: wrap(handleEquip),
|
||||
postUnequip: wrap(handleUnequip),
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user