fel kesz game backend
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { n } from "framer-motion/dist/types.d-D0HXPxHm";
|
||||
|
||||
export interface UpdateDeckCommand {
|
||||
id: string;
|
||||
userstate?: number;
|
||||
name?: string;
|
||||
type?: number;
|
||||
userid?: string;
|
||||
|
||||
@@ -2,13 +2,49 @@ import { IDeckRepository } from '../../../Domain/IRepository/IDeckRepository';
|
||||
import { UpdateDeckCommand } from './UpdateDeckCommand';
|
||||
import { ShortDeckDto } from '../../DTOs/DeckDto';
|
||||
import { DeckMapper } from '../../DTOs/Mappers/DeckMapper';
|
||||
import { DeckAggregate } from '../../../Domain/Deck/DeckAggregate';
|
||||
import { logError } from '../../Services/Logger';
|
||||
|
||||
export class UpdateDeckCommandHandler {
|
||||
constructor(private readonly deckRepo: IDeckRepository) {}
|
||||
|
||||
async execute(cmd: UpdateDeckCommand): Promise<ShortDeckDto | null> {
|
||||
const updated = await this.deckRepo.update(cmd.id, { ...cmd });
|
||||
if (!updated) return null;
|
||||
return DeckMapper.toShortDto(updated);
|
||||
if(cmd.state !== undefined && cmd.userstate!==1) {
|
||||
throw new Error('Only admin users can change deck state');
|
||||
}
|
||||
try {
|
||||
let existingDeck: DeckAggregate | null = null;
|
||||
if (cmd.userstate === 1) {
|
||||
existingDeck = await this.deckRepo.findByIdIncludingDeleted(cmd.id);
|
||||
} else {
|
||||
existingDeck = await this.deckRepo.findById(cmd.id);
|
||||
}
|
||||
if (!existingDeck) {
|
||||
logError(`Deck not found with ID: ${cmd.id}`);
|
||||
throw new Error('Deck not found');
|
||||
}
|
||||
|
||||
const for_update: Partial<DeckAggregate> = {};
|
||||
if(cmd.name !== undefined) for_update.name = cmd.name;
|
||||
if(cmd.type !== undefined) for_update.type = cmd.type;
|
||||
if(cmd.cards !== undefined) for_update.cards = cmd.cards;
|
||||
if(cmd.ctype !== undefined) for_update.ctype = cmd.ctype;
|
||||
if(cmd.state !== undefined) for_update.state = cmd.state;
|
||||
|
||||
// Ensure we have something to update
|
||||
if (Object.keys(for_update).length === 0) {
|
||||
throw new Error('No fields provided for update');
|
||||
}
|
||||
|
||||
const deck = await this.deckRepo.update(cmd.id, { ...for_update });
|
||||
if(!deck) {
|
||||
logError(`Deck update failed for ID: ${cmd.id}. Update returned null.`);
|
||||
throw new Error('Failed to update deck');
|
||||
}
|
||||
return DeckMapper.toShortDto(deck);
|
||||
} catch (error: any) {
|
||||
logError(`Error updating deck: ${cmd.id}`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user