feat(api): add inventory API (GET /api/inventory)
This commit is contained in:
71
apps/api/src/inventory/inventory.service.ts
Normal file
71
apps/api/src/inventory/inventory.service.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CharacterEquipment } from '../equipment/entities/character-equipment.entity';
|
||||
import { CharacterItem } from '../items/entities/character-item.entity';
|
||||
import { EquipmentSlot } from '../items/equipment-slot.enum';
|
||||
import { ItemRarity } from '../items/item-rarity.enum';
|
||||
|
||||
export interface InventoryItemDto {
|
||||
id: string;
|
||||
quantity: number;
|
||||
equipped: boolean;
|
||||
item: {
|
||||
key: string;
|
||||
name: string;
|
||||
rarity: ItemRarity;
|
||||
equipmentSlot: EquipmentSlot | null;
|
||||
requiredLevel: number;
|
||||
weaponDamage: number;
|
||||
bonusAttack: number;
|
||||
bonusHp: number;
|
||||
bonusArmor: number;
|
||||
iconPath: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface InventoryResponseDto {
|
||||
items: InventoryItemDto[];
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class InventoryService {
|
||||
constructor(
|
||||
@InjectRepository(CharacterItem)
|
||||
private readonly characterItems: Repository<CharacterItem>,
|
||||
@InjectRepository(CharacterEquipment)
|
||||
private readonly equipment: Repository<CharacterEquipment>,
|
||||
) {}
|
||||
|
||||
async getInventory(characterId: string): Promise<InventoryResponseDto> {
|
||||
const [items, equipped] = await Promise.all([
|
||||
this.characterItems.find({
|
||||
where: { characterId },
|
||||
relations: { itemDefinition: true },
|
||||
order: { createdAt: 'ASC' },
|
||||
}),
|
||||
this.equipment.find({ where: { characterId } }),
|
||||
]);
|
||||
const equippedIds = new Set(equipped.map((row) => row.characterItemId));
|
||||
|
||||
return {
|
||||
items: items.map((characterItem) => ({
|
||||
id: characterItem.id,
|
||||
quantity: characterItem.quantity,
|
||||
equipped: equippedIds.has(characterItem.id),
|
||||
item: {
|
||||
key: characterItem.itemDefinition.key,
|
||||
name: characterItem.itemDefinition.name,
|
||||
rarity: characterItem.itemDefinition.rarity,
|
||||
equipmentSlot: characterItem.itemDefinition.equipmentSlot,
|
||||
requiredLevel: characterItem.itemDefinition.requiredLevel,
|
||||
weaponDamage: characterItem.itemDefinition.weaponDamage,
|
||||
bonusAttack: characterItem.itemDefinition.bonusAttack,
|
||||
bonusHp: characterItem.itemDefinition.bonusHp,
|
||||
bonusArmor: characterItem.itemDefinition.bonusArmor,
|
||||
iconPath: characterItem.itemDefinition.iconPath,
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user