Esempio n. 1
0
        public async Task <IActionResult> Create(AccountAddModel account)
        {
            ResultLog resultLog = new ResultLog();

            if (ModelState.IsValid)
            {
                if (!await _redisService.IsExistKey(accountTableKey, account.AccountNumber))
                {
                    //ToDo: Use AutoMapper and DTO
                    resultLog.IsError = !await _redisService.CreateHashSetDataAsync(accountTableKey, account.AccountNumber,
                                                                                    new Account
                    {
                        AccountNumber = account.AccountNumber,
                        Balance       = Math.Round(account.Balance, 2), //limited 2 digits
                        CurrencyCode  = account.CurrencyCode.ToString()
                    });

                    if (resultLog.IsError)
                    {
                        return(StatusCode((int)Enums.Enums.StatusCode.ISSUER_OR_SWITCH_INOPERATIVE, resultLog));
                    }
                    return(StatusCode((int)Enums.Enums.StatusCode.CREATED, resultLog));
                }
                else
                {
                    return(StatusCode((int)Enums.Enums.StatusCode.CONFLICT,
                                      ActionResultHelper.CreateActionResultJson(Enums.Enums.StatusCode.CONFLICT, "AccountNumber should be unique")));
                }
            }
            return(ValidationProblem());
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(TransferAddModel transfer)
        {
            ResultLog resultLog = new ResultLog();

            if (ModelState.IsValid)
            {
                var isSenderExist = await _redisService.IsExistKey(accountTableKey, transfer.SenderAccountNumber);

                var isReceiverExist = await _redisService.IsExistKey(accountTableKey, transfer.ReceiverAccountNumber);

                //Check accounts
                if (!isSenderExist || !isReceiverExist)
                {
                    return(StatusCode((int)Enums.Enums.StatusCode.INVALID_CARD_NUMBER,
                                      ActionResultHelper.CreateActionResultJson(
                                          Enums.Enums.StatusCode.INVALID_CARD_NUMBER,
                                          "Please check accounts again")));
                }
                var sender = await _accountService.GetHashOneAsync(accountTableKey, transfer.SenderAccountNumber);

                var receiver = await _accountService.GetHashOneAsync(accountTableKey, transfer.ReceiverAccountNumber);

                //Check Currency Type is same
                if (!sender.CurrencyCode.Equals(receiver.CurrencyCode))
                {
                    return(StatusCode((int)Enums.Enums.StatusCode.INVALID_TRANSACTION,
                                      ActionResultHelper.CreateActionResultJson(
                                          Enums.Enums.StatusCode.INVALID_TRANSACTION,
                                          "CurrencyCode of the accounts are not same")));
                }

                //Check sender balance
                if (sender.Balance < transfer.Amount)
                {
                    return(StatusCode((int)Enums.Enums.StatusCode.NOT_SUFFICIENT_FUNDS,
                                      ActionResultHelper.CreateActionResultJson(
                                          Enums.Enums.StatusCode.NOT_SUFFICIENT_FUNDS,
                                          "There is not enough money in your account")));
                }

                Transfer tr = new Transfer
                {
                    SenderAccountNumber   = transfer.SenderAccountNumber,
                    ReceiverAccountNumber = transfer.ReceiverAccountNumber,
                    Amount = transfer.Amount
                };

                var operationResult = await _accountService.MakeTransfer(tr);


                if (operationResult)
                {
                    var r = await _redisService.CreateHashSetDataAsync(transferTableKey, new Random().Next(int.MinValue, int.MaxValue), tr);

                    resultLog.IsError = !r;
                    return(Ok(resultLog));
                }
                resultLog.IsError = true;
                return(BadRequest(resultLog));
            }
            return(ValidationProblem());
        }