コード例 #1
0
ファイル: StockCommandHandler.cs プロジェクト: mgce/stocqres
        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);
            }
        }
コード例 #2
0
        public async Task HandleAsync(ChargeWalletAmountCommand amountCommand)
        {
            var company = await _eventRepository.GetByIdAsync <Company>(amountCommand.CompanyId);

            if (company == null)
            {
                throw new StocqresException("Company doesn't exist");
            }

            if (company.Stock == null)
            {
                throw new StocqresException("Company doesn't have any stock");
            }

            var wallet = await GetWallet(amountCommand.WalletId);

            var stockPrice = await _stockExchangeService.GetStockPrice(company.Stock.Code);

            wallet.ChargeWallet(amountCommand.OrderId, stockPrice * amountCommand.Quantity);
            await _eventRepository.SaveAsync(wallet);
        }