Код IT
← Каталог

Паттерны проектирования — TypeScript и NestJS

Фрагмент из «Паттерны проектирования»: TypeScript и NestJS.

typescript projectencyclopedia7-06-proektirovanie-i-arhitektura-115 embed URL статья в энциклопедии
TypeScript main.ts
// Команда
export class CreateOrderCommand {
  constructor(public readonly userId: string, public readonly items: Item[]) {}
}

// Обработчик команды
@CommandHandler(CreateOrderCommand)
export class CreateOrderHandler 
  implements ICommandHandler<CreateOrderCommand, OrderId> 
{
  constructor(private repo: OrderRepository) {}

  async execute(command: CreateOrderCommand): Promise<OrderId> {
    const order = Order.create(command.userId, command.items);
    await this.repo.save(order);
    return order.id;
  }
}

// Использование
const id = await this.commandBus.execute(
  new CreateOrderCommand(userId, items)
);
// Команда
export class CreateOrderCommand {
  constructor(public readonly userId: string, public readonly items: Item[]) {}
}

// Обработчик команды
@CommandHandler(CreateOrderCommand)
export class CreateOrderHandler 
  implements ICommandHandler<CreateOrderCommand, OrderId> 
{
  constructor(private repo: OrderRepository) {}

  async execute(command: CreateOrderCommand): Promise<OrderId> {
    const order = Order.create(command.userId, command.items);
    await this.repo.save(order);
    return order.id;
  }
}

// Использование
const id = await this.commandBus.execute(
  new CreateOrderCommand(userId, items)
);