public IActionResult PostChildren(AccountChildrenDTO account, [FromServices] AccountApplication accountApplication)
 {
     try
     {
         return(Ok(accountApplication.CreatedChildren(account)));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex));
     }
 }
        public Account CreatedChildren(AccountChildrenDTO accountChildrenDTO)
        {
            _accountRepository.Begin();

            try
            {
                Account account = _accountRepository.Get(new Object[] { accountChildrenDTO.AccountParentId });

                if (account == null)
                {
                    throw new Exception("Conta pai informada nao existe");
                }

                Account accountChildren = new Account();
                accountChildren.Created  = DateTime.Now;
                accountChildren.Master   = false;
                accountChildren.Money    = accountChildrenDTO.Money;
                accountChildren.Name     = accountChildrenDTO.Name;
                accountChildren.PersonId = accountChildrenDTO.PersonId;
                accountChildren.StatusId = accountChildrenDTO.StatusId;

                accountChildren = _accountRepository.Save(accountChildren);

                ChildrenAccounts childrenAccounts = new ChildrenAccounts();
                childrenAccounts.ParentAccountId   = account.Id;
                childrenAccounts.ChildrenAccountId = accountChildren.Id;

                childrenAccounts = _accountChildrenRepository.Save(childrenAccounts);

                _accountRepository.Commit();

                return(accountChildren);
            }
            catch (Exception ex)
            {
                _accountRepository.RollBack();
                throw ex;
            }
        }
        public Account PostFull(AccountFullDTO accountFull)
        {
            int PersonId = _personApplication.PostPersonPhysicaORPersonLegal(accountFull.PersonLegal, accountFull.PersonPhysical);

            try
            {
                if (accountFull.AccountParentId != null && accountFull.AccountParentId > 0)
                {
                    AccountChildrenDTO accountChildrenDTO = new AccountChildrenDTO();
                    accountChildrenDTO.AccountParentId = accountFull.AccountParentId.Value;
                    accountChildrenDTO.Money           = accountFull.Money;
                    accountChildrenDTO.Name            = accountFull.Name;
                    accountChildrenDTO.PersonId        = PersonId;
                    accountChildrenDTO.StatusId        = accountFull.StatusId;

                    Account account = CreatedChildren(accountChildrenDTO);

                    return(account);
                }
                else
                {
                    AccountMasterDTO accountMaster = new AccountMasterDTO();
                    accountMaster.Money    = accountFull.Money;
                    accountMaster.Name     = accountFull.Name;
                    accountMaster.PersonId = PersonId;
                    accountMaster.StatusId = accountFull.StatusId;

                    Account account = Created(accountMaster);

                    return(account);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("O Cliente foi salvo com sucesso. Porem ocorreu um erro ao salvar a conta, selecione o cliente adicionado e cria a conta novamente!", ex.InnerException);
            }
        }