Beispiel #1
0
        public IActionResult Transfer([FromBody] TransferCredentials request)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(new
                {
                    Errors = ModelState.Values.SelectMany(x => x.Errors.Select(xx => xx.ErrorMessage))
                }));
            }

            if (request.Value <= 0)
            {
                return(new BadRequestObjectResult(new
                {
                    Errors = "not valid value"
                }));
            }


            var accountFrom = accountRepository.FindByAccountId(request.FromId);
            var accountTo   = accountRepository.FindByAccountId(request.ToId);

            if (accountFrom == null || accountTo == null || accountFrom.Status == "closed" || accountTo.Status == "closed")
            {
                return(new BadRequestObjectResult(new
                {
                    Errors = "not valid Account Id"
                }));
            }

            if (HttpContext.GetUser() != accountFrom.Email)
            {
                return(new BadRequestObjectResult(new
                {
                    Errors = "No access"
                }));
            }

            if (accountFrom.Money < request.Value)
            {
                return(new BadRequestObjectResult(new
                {
                    Errors = "Invalid balance"
                }));
            }
            accountRepository.UpdateMoney(accountFrom.AccountId, -request.Value);
            accountRepository.UpdateMoney(accountTo.AccountId, request.Value);

            return(new OkObjectResult(new
            {
                Message = "success"
            }));
        }
Beispiel #2
0
        public async Task <IActionResult> Transfer([FromBody] TransferCredentials transfer)
        {
            var userEmail = User.FindFirst(ClaimTypes.Email)?.Value;

            if (string.Equals(transfer.From, transfer.To))
            {
                ModelState.AddModelError("Same", "Вы не можете перевести деньги сами себе.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            return(Ok(await _accountsRequestHandler.TransferMoney(transfer.From, userEmail, transfer.To, transfer.Amount)));
        }