public async Task HandleAsync(StockGroupQuantityChangedEvent @event) { await _eventStore.AppendToStream(@event.AggregateId, @event); var entity = await _stockGroupRepository.GetByIdAsync(@event.AggregateId); entity.UpdateQuantity(@event.Quantity); await _stockGroupRepository.UpdateAsync(entity); }
public async Task HandleAsync(BuyStocksCommand command) { var user = await _eventStore.Load <Domain.User>(command.UserId); var stock = await _eventStore.Load <Domain.Stock>(command.StockId); var stockExchange = await _eventStore.Load <Domain.StockExchange>(stock.StockExchangeId); var stockExchangeStockGroup = await _stockGroupService.GetStockGroupFromStockExchange(stockExchange.Id, stock.Id); if (stockExchangeStockGroup.Quantity < command.Quantity) { throw new StockExchangeDoesNotHaveEnoughtStocksException(); } stockExchangeStockGroup.DecreaseQuantity(command.Quantity); await _eventBus.Publish(new StockGroupQuantityChangedEvent(stockExchangeStockGroup.Id, stockExchangeStockGroup.Quantity)); var stockPrice = await _stockExchangeService.GetStockPrice(stock.Code); await BurdenUserWallet(user.WalletId, stockPrice, stock.Unit, command.Quantity); var userStockGroup = await _stockGroupService.GetUserStockGroup(command.UserId, command.StockId); if (userStockGroup == null) { await CreateStockGroup(user.Id, stock.Id, stockPrice, command.Quantity); await _stockGroupRepository.UpdateAsync(stockExchangeStockGroup); } else { //Usunac _stockGroupRepository poniewaz z event handlera zmieniana jest wartosc w read modelu //przemyslec w jaki sposob roznica kwot powinna byc zapisana w evencie //tj. czy powinnismy zapisywać jedynie wartość ktora odejmujemy, czy wartosc koncowa userStockGroup.IncreaseQuantity(command.Quantity); await _eventBus.Publish(new StockGroupQuantityChangedEvent(userStockGroup.Id, userStockGroup.Quantity)); await _stockGroupRepository.UpdateAsync(stockExchangeStockGroup, userStockGroup); } }