public async Task Handle(
            DateTimeOffset date, UserId user, BrokerId brokerId, AssetId assetId, int count)
        {
            if (count <= 0)
            {
                throw new InvalidCountException();
            }
            var state  = _stateManager.ReadState(date, user);
            var broker = state.Brokers.FirstOrDefault(b => b.Id == brokerId);

            if (broker == null)
            {
                throw new InvalidBrokerException();
            }
            if (string.IsNullOrWhiteSpace(assetId))
            {
                throw new InvalidAssetException();
            }
            var asset = broker.Inventory.FirstOrDefault(a => a.Id == assetId);

            if (asset == null)
            {
                throw new AssetNotFoundException();
            }
            var remainingCount = asset.Count - count;

            if (remainingCount < 0)
            {
                throw new InvalidCountException();
            }
            await _stateManager.AddCommand(new ReduceAssetCommand(date, user, brokerId, assetId, count));
        }
Exemplo n.º 2
0
        public async Task Handle(
            DateTimeOffset date, UserId user, BrokerId broker, AccountId account,
            decimal amount, ExpenseCategory category, AssetId?asset)
        {
            if (amount == 0)
            {
                throw new InvalidPriceException();
            }
            if (string.IsNullOrWhiteSpace(category.ToString()))
            {
                throw new InvalidCategoryException();
            }
            var state       = _stateManager.ReadState(date, user);
            var brokerState = state.Brokers.FirstOrDefault(b => b.Id == broker);

            if (brokerState == null)
            {
                throw new BrokerNotFoundException();
            }
            if (brokerState.Accounts.All(a => a.Id != account))
            {
                throw new AccountNotFoundException();
            }
            if ((asset != null) && brokerState.Inventory.All(a => a.Id != asset))
            {
                throw new AssetNotFoundException();
            }
            var id = new OperationId(_idGenerator.GenerateNewId());
            await _stateManager.AddCommand(new AddExpenseCommand(
                                               date, user, broker, account, id, amount, category, asset));
        }
Exemplo n.º 3
0
        public async Task Handle(
            DateTimeOffset date, UserId user, BrokerId broker, CurrencyCode currency, string displayName)
        {
            if (string.IsNullOrWhiteSpace(displayName))
            {
                throw new InvalidAccountException();
            }
            var state = _stateManager.ReadState(date, user);

            if (!_currencyConfig.HasCurrency(currency))
            {
                throw new CurrencyNotFoundException();
            }
            var brokerState = state.Brokers.FirstOrDefault(b => b.Id == broker);

            if (brokerState == null)
            {
                throw new BrokerNotFoundException();
            }
            if (brokerState.Accounts.Any(a => a.DisplayName == displayName))
            {
                throw new DuplicateAccountException();
            }
            var id = new AccountId(_idGenerator.GenerateNewId());
            await _stateManager.AddCommand(new CreateAccountCommand(date, user, broker, id, currency, displayName));
        }
        public async Task Handle(DateTimeOffset date, UserId user, string displayName)
        {
            if (string.IsNullOrWhiteSpace(displayName))
            {
                throw new InvalidBrokerException();
            }
            var state = _stateManager.ReadState(date, user);

            if (state.Brokers.Any(b => b.DisplayName == displayName))
            {
                throw new DuplicateBrokerException();
            }
            var id = new BrokerId(_idGenerator.GenerateNewId());
            await _stateManager.AddCommand(new CreateBrokerCommand(date, user, id, displayName));
        }
Exemplo n.º 5
0
        public async Task <AssetId> Handle(
            DateTimeOffset date, UserId user, BrokerId brokerId, AccountId payAccountId, AccountId feeAccountId,
            AssetISIN isin, decimal price, decimal fee, string name, int count)
        {
            if (string.IsNullOrWhiteSpace(isin.Value))
            {
                throw new InvalidAssetException();
            }
            if (count <= 0)
            {
                throw new InvalidCountException();
            }
            var state  = _stateManager.ReadState(date, user);
            var broker = state.Brokers.FirstOrDefault(b => b.Id == brokerId);

            if (broker == null)
            {
                throw new InvalidBrokerException();
            }
            var payAccount = broker.Accounts.FirstOrDefault(a => a.Id == payAccountId);

            if (payAccount == null)
            {
                throw new InvalidAccountException();
            }
            var feeAccount = broker.Accounts.FirstOrDefault(a => a.Id == feeAccountId);

            if (feeAccount == null)
            {
                throw new InvalidAccountException();
            }
            var     asset = broker.Inventory.FirstOrDefault(a => a.Isin == isin);
            AssetId id;

            if (asset != null)
            {
                id = asset.Id;
                await _stateManager.AddCommand(new IncreaseAssetCommand(date, user, brokerId, asset.Id, count));
            }
            else
            {
                id = new AssetId(_idGenerator.GenerateNewId());
                var currency = payAccount.Currency;
                await _stateManager.AddCommand(new AddAssetCommand(date, user, brokerId, id, isin, currency, name, count));
            }
            switch (price)
            {
            case < 0:
                throw new InvalidPriceException();

            case 0:
                break;

            default:
                await _addExpense.Handle(
                    date, user, brokerId, payAccountId, price, ExpenseCategory.BuyAsset, id);

                break;
            }
            switch (fee)
            {
            case < 0:
                throw new InvalidPriceException();

            case 0:
                break;

            default:
                await _addExpense.Handle(
                    date, user, brokerId, feeAccountId, fee, ExpenseCategory.BuyAssetFee, id);

                break;
            }
            return(id);
        }
Exemplo n.º 6
0
        public async Task Handle(
            DateTimeOffset date, UserId user, BrokerId brokerId, AccountId payAccountId, AccountId feeAccountId,
            AssetId assetId, decimal price, decimal fee, int count)
        {
            if (count <= 0)
            {
                throw new InvalidCountException();
            }
            var state  = _stateManager.ReadState(date, user);
            var broker = state.Brokers.FirstOrDefault(b => b.Id == brokerId);

            if (broker == null)
            {
                throw new InvalidBrokerException();
            }
            if (string.IsNullOrWhiteSpace(assetId))
            {
                throw new InvalidAssetException();
            }
            var asset = broker.Inventory.FirstOrDefault(a => a.Id == assetId);

            if (asset == null)
            {
                throw new AssetNotFoundException();
            }
            var remainingCount = asset.Count - count;

            if (remainingCount < 0)
            {
                throw new InvalidCountException();
            }
            var payAccount = broker.Accounts.FirstOrDefault(a => a.Id == payAccountId);

            if (payAccount == null)
            {
                throw new InvalidAccountException();
            }
            var feeAccount = broker.Accounts.FirstOrDefault(a => a.Id == feeAccountId);

            if (feeAccount == null)
            {
                throw new InvalidAccountException();
            }
            switch (price)
            {
            case < 0:
                throw new InvalidPriceException();

            case 0:
                break;

            default:
                await _addIncome.Handle(
                    date, user, brokerId, payAccountId, price, IncomeCategory.SellAsset, assetId);

                break;
            }
            switch (fee)
            {
            case < 0:
                throw new InvalidPriceException();

            case 0:
                break;

            default:
                await _addExpense.Handle(
                    date, user, brokerId, feeAccountId, fee, ExpenseCategory.SellAssetFee, assetId);

                break;
            }
            await _stateManager.AddCommand(new ReduceAssetCommand(date, user, brokerId, assetId, count));
        }