Beispiel #1
0
        public void Store(StoreMoneyRequestModel request)
        {
            var currency = _currencyRepo.GetAll(s => s.Name.Equals(request.Currency)).FirstOrDefault();

            if (currency == null)
            {
                throw new Exception($"Selected currency is not yet available.");
            }

            var user = _userRepo.Get(request.UserId);

            if (user == null)
            {
                throw new Exception("User does not exist.");
            }

            UserMoney money = new UserMoney {
                Currency = currency
            };

            money.CashIn(request.Amount);

            user.StoreMoney(money);

            _userRepo.Update(user);
        }
Beispiel #2
0
        public void Send(SendMoneyRequestModel request)
        {
            var userFrom = _userRepo.Get(request.FromUserId);
            var userTo   = _userRepo.Get(request.ToUserId);

            if (userFrom == null || userTo == null)
            {
                throw new Exception("The user/s you specified not found.");
            }

            var userFrom_money = userFrom.Money.Where(s => s.Currency.Name.Equals(request.Currency)).FirstOrDefault();

            if (userFrom_money == null)
            {
                throw new Exception($"The sender dont have a {request.Currency} balance.");
            }
            if (!userFrom_money.CanSend(request.Amount))
            {
                throw new Exception($"The sender cannot send the specified amount.");
            }

            var currency = _currencyRepo.GetAll(s => s.Name.Equals(request.Currency)).FirstOrDefault();

            if (currency == null)
            {
                throw new Exception($"Selected currency is not yet available.");
            }

            var userTo_money = userTo.Money.Where(s => s.Currency.Name.Equals(request.Currency)).FirstOrDefault();

            if (userTo_money == null)
            {
                userTo_money = new UserMoney
                {
                    Currency = currency
                };
                userTo_money.CashIn(request.Amount);


                userTo.StoreMoney(userTo_money);
            }

            userFrom_money.CashOut(request.Amount);

            _userRepo.Update(userFrom);
            _userRepo.Update(userTo);
        }