示例#1
0
        public async Task <IActionResult> Send([FromBody] SendMoneyRequest request)
        {
            try
            {
                var currency = await _currencyService.GetCurrencyAsync(request.CurrencyId);

                var money = new Money()
                {
                    Currency = currency, Amount = request.Amount
                };
                var fromUser = await _userService.GetUserAsync(request.FromUsername);

                var toUser = await _userService.GetUserAsync(request.ToUsername);

                fromUser.Balance.ChargeMoney(money);
                toUser.Balance.AddMoney(money);

                await _userService.UpdateUserBalance(fromUser);

                await _userService.UpdateUserBalance(toUser);

                return(Ok("Send money successful!"));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
示例#2
0
        public async Task WhenSendingMoneyAndReturnIsNullOrError_ThenThrowArgumentException()
        {
            //Arrange
            var fixture  = new Fixture();
            var currency = new Currency()
            {
                Id = 1, Name = "USD", Ratio = 1
            };
            var money = new Money()
            {
                Currency = currency, Amount = 100
            };
            var fromUser = fixture.Create <User>();
            var toUser   = fixture.Create <User>();

            var request = new SendMoneyRequest()
            {
                FromUsername = fromUser.Username,
                CurrencyId   = currency.Id,
                Amount       = 10,
                ToUsername   = toUser.Username
            };

            fromUser.Balance.AddMoney(money);

            _currencyServiceMock.Setup(x => x.GetCurrencyAsync(currency.Id)).ReturnsAsync(currency);
            _userServiceMock.Setup(x => x.GetUserAsync(fromUser.Username)).ReturnsAsync(fromUser);
            _userServiceMock.Setup(x => x.GetUserAsync(toUser.Username)).ReturnsAsync(toUser);
            _userServiceMock.Setup(x => x.UpdateUserBalance(fromUser)).ReturnsAsync(fromUser);
            _userServiceMock.Setup(x => x.UpdateUserBalance(toUser)).ReturnsAsync(toUser);

            //Action
            var target = new MoneyController(_currencyServiceMock.Object, _userServiceMock.Object);
            var result = await target.Send(request);

            //Assert
            var objectResult = result.As <ObjectResult>();

            objectResult.Should().NotBeNull();
            objectResult.StatusCode.Should().Be(StatusCodes.Status200OK);
        }