Files
SerpentRace/SerpentRace_Backend/src/Application/Contact/commands/CreateContactCommandHandler.ts
T

27 lines
1013 B
TypeScript

import { IContactRepository } from '../../../Domain/IRepository/IContactRepository';
import { CreateContactCommand } from './CreateContactCommand';
import { ShortContactDto } from '../../DTOs/ContactDto';
import { ContactAggregate, ContactState } from '../../../Domain/Contact/ContactAggregate';
import { ContactMapper } from '../../DTOs/Mappers/ContactMapper';
export class CreateContactCommandHandler {
constructor(private readonly contactRepo: IContactRepository) {}
async execute(cmd: CreateContactCommand): Promise<ShortContactDto> {
try {
const contact = new ContactAggregate();
contact.name = cmd.name;
contact.email = cmd.email;
contact.userid = cmd.userid || null;
contact.type = cmd.type;
contact.txt = cmd.txt;
contact.state = ContactState.ACTIVE;
const created = await this.contactRepo.create(contact);
return ContactMapper.toShortDto(created);
} catch (error) {
throw new Error('Failed to create contact');
}
}
}