package com.flexpoker.table.query.handlers; import java.util.ArrayList; import java.util.List; import javax.inject.Inject; import org.springframework.stereotype.Component; import com.flexpoker.framework.event.EventHandler; import com.flexpoker.framework.pushnotifier.PushNotification; import com.flexpoker.framework.pushnotifier.PushNotificationPublisher; import com.flexpoker.model.card.TurnCard; import com.flexpoker.pushnotifications.TableUpdatedPushNotification; import com.flexpoker.table.command.events.TurnCardDealtEvent; import com.flexpoker.table.query.repository.CardsUsedInHandRepository; import com.flexpoker.table.query.repository.TableRepository; import com.flexpoker.web.dto.outgoing.CardDTO; import com.flexpoker.web.dto.outgoing.TableDTO; @Component public class TurnCardDealtEventHandler implements EventHandler<TurnCardDealtEvent> { private final TableRepository tableRepository; private final CardsUsedInHandRepository cardsUsedInHandRepository; private final PushNotificationPublisher pushNotificationPublisher; @Inject public TurnCardDealtEventHandler(TableRepository tableRepository, CardsUsedInHandRepository cardsUsedInHandRepository, PushNotificationPublisher pushNotificationPublisher) { this.tableRepository = tableRepository; this.cardsUsedInHandRepository = cardsUsedInHandRepository; this.pushNotificationPublisher = pushNotificationPublisher; } @Override public void handle(TurnCardDealtEvent event) { handleUpdatingTable(event); handlePushNotifications(event); } private void handleUpdatingTable(TurnCardDealtEvent event) { TableDTO currentTable = tableRepository.fetchById(event.getAggregateId()); TurnCard turnCard = cardsUsedInHandRepository.fetchTurnCard(event.getHandId()); List<CardDTO> visibleCommonCards = new ArrayList<>(currentTable.getVisibleCommonCards()); visibleCommonCards.add(new CardDTO(turnCard.getCard().getId())); TableDTO updatedTable = new TableDTO(currentTable.getId(), event.getVersion(), currentTable.getSeats(), currentTable.getTotalPot(), currentTable.getPots(), visibleCommonCards, currentTable.getCurrentHandMinRaiseToAmount()); tableRepository.save(updatedTable); } private void handlePushNotifications(TurnCardDealtEvent event) { PushNotification pushNotification = new TableUpdatedPushNotification( event.getGameId(), event.getAggregateId()); pushNotificationPublisher.publish(pushNotification); } }