package com.flexpoker.table.command.handlers; import java.util.List; import javax.inject.Inject; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import com.flexpoker.framework.command.CommandHandler; import com.flexpoker.framework.event.EventPublisher; import com.flexpoker.table.command.aggregate.Table; import com.flexpoker.table.command.commands.FoldCommand; import com.flexpoker.table.command.factory.TableFactory; import com.flexpoker.table.command.framework.TableEvent; import com.flexpoker.table.command.repository.TableEventRepository; @Component public class FoldCommandHandler implements CommandHandler<FoldCommand> { private final TableFactory tableFactory; private final EventPublisher<TableEvent> eventPublisher; private final TableEventRepository tableEventRepository; @Inject public FoldCommandHandler(TableFactory tableFactory, EventPublisher<TableEvent> eventPublisher, TableEventRepository tableEventRepository) { this.tableFactory = tableFactory; this.eventPublisher = eventPublisher; this.tableEventRepository = tableEventRepository; } @Async @Override public void handle(FoldCommand command) { List<TableEvent> tableEvents = tableEventRepository .fetchAll(command.getTableId()); Table table = tableFactory.createFrom(tableEvents); table.fold(command.getPlayerId()); table.fetchNewEvents().forEach(x -> tableEventRepository.save(x)); table.fetchNewEvents().forEach(x -> eventPublisher.publish(x)); } }