public async Task <BodyResponse <MoneyMqResponse> > Handle(AddMoneyCommand request, CancellationToken cancellationToken) { using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, MoneyInfo.ClassName))) { await locker.LockAsync(); Log.Debug($"AddMoneyCommand add begin:{request.AddCoins},{request.AddCarry} {request.AggregateId}"); var moneyInfo = await _redis.GetMoney(request.Id); if (moneyInfo == null) { moneyInfo = await _moneyRepository.FindAndAdd(request.Id, new MoneyInfo(request.Id, 0, 0, 0, 0, 0)); } if (request.AddCoins < 0 && System.Math.Abs(request.AddCoins) > moneyInfo.CurCoins) { Log.Debug($"AddMoneyCommand add end:{request.AddCoins},{request.AddCarry} {request.AggregateId}--1"); return(new BodyResponse <MoneyMqResponse> (StatusCodeDefines.NoEnoughMoney, null, null)); } if (request.AddCarry < 0 && System.Math.Abs(request.AddCarry) > moneyInfo.Carry) { Log.Debug($"AddMoneyCommand add end:{request.AddCoins},{request.AddCarry} {request.AggregateId}--2"); return(new BodyResponse <MoneyMqResponse> (StatusCodeDefines.NoEnoughMoney, null, null)); } moneyInfo.AddCoins(request.AddCoins); moneyInfo.AddCarry(request.AddCarry); moneyInfo.UpdateMaxCoins(); long coinsChangedCount = request.AddCoins + request.AddCarry; var moneyevent = new MoneyChangedMqEvent (moneyInfo.Id, moneyInfo.CurCoins, moneyInfo.CurDiamonds, moneyInfo.MaxCoins, moneyInfo.MaxDiamonds, coinsChangedCount, 0); _busCtl.PublishExt(moneyevent); _busCtl.PublishServerReqExt(moneyInfo.Id, moneyevent); await Task.WhenAll(_redis.SetMoney(request.Id, moneyInfo), _moneyRepository.ReplaceAsync(moneyInfo)); Log.Debug($"AddMoneyCommand add end:{request.AddCoins},{request.AddCarry} {request.AggregateId} curCoins:{moneyInfo.CurCoins} curCarry:{moneyInfo.Carry}--3"); return(new BodyResponse <MoneyMqResponse>(StatusCodeDefines.Success, null, new MoneyMqResponse(request.Id, moneyInfo.CurCoins, moneyInfo.CurDiamonds, moneyInfo.MaxCoins, moneyInfo.MaxDiamonds, moneyInfo.Carry))); } }
private void InitializeCommands() { // Ambivalent about newing these commands up here. // On the one hand I feel like I should be using DI // and on the other hand they're so closely linked // they'll never be in a separate repo ICommand acceptCommand = new AcceptCurrentWorkingCoffeeCommand(); ICommand addMoneyCommand = new AddMoneyCommand(); ICommand showOrderCommand = new ShowOrderCommand(); ICommand setSizeCommand = new SetSizeCommand(); ICommand setCreamCommand = new SetCreamCommand(); ICommand setSugarCommand = new SetSugarCommand(); ICommand incrementCreamCommand = new IncrementCreamCommand(); ICommand incrementSugarCommand = new IncrementSugarCommand(); ICommand decrementCreamCommand = new DecrementCreamCommand(); ICommand decrementSugarCommand = new DecrementSugarCommand(); ICommand selectSmallCommand = new SelectSmallCommand(); ICommand selectMediumCommand = new SelectMediumCommand(); ICommand selectLargeCommand = new SelectLargeCommand(); ICommand showCurrentWorkingCoffee = new ShowCurrentWorkingCoffeeCommand(); ICommand completeTransactionCommand = new CompleteTransactionCommand(); commands.Add(ADD_PAYMENT, addMoneyCommand); commands.Add(ACCEPT_CURRENT_WORKING_COFFEE, acceptCommand); commands.Add(SHOW_CURRENT_ORDER_AND_TOTAL, showOrderCommand); commands.Add(SET_SIZE, setSizeCommand); commands.Add(SET_CREAM, setCreamCommand); commands.Add(SET_SUGAR, setSugarCommand); commands.Add(SELECT_SMALL_COFFEE, selectSmallCommand); commands.Add(SELECT_MEDIUM_COFFEE, selectMediumCommand); commands.Add(SELECT_LARGE_COFFEE, selectLargeCommand); commands.Add(INCREMENT_CREAM, incrementCreamCommand); commands.Add(INCREMENT_SUGAR, incrementSugarCommand); commands.Add(DECREMENT_CREAM, decrementCreamCommand); commands.Add(DECREMENT_SUGAR, decrementSugarCommand); commands.Add(SHOW_CURRENT_WORKING_COFFEE, showCurrentWorkingCoffee); commands.Add(DISPENSE_ORDER_AND_CHANGE, completeTransactionCommand); }
public IMoney Add(IBank bank, IMoney augend, IMoney addend) { var newAdd = new AddMoneyCommand(augend); return(this.Do(newAdd, bank, addend)); }
public ICommand BuildCommand() { ICommand result = new AddMoneyCommand(); return(result); }