public AccountEventEntity(AccountEventModel accountEventModel, int accountId)
 {
     this.AccountId = accountId;
     this.Type      = accountEventModel.Type;
     this.EventDate = accountEventModel.EventDate;
     this.Value     = accountEventModel.Value;
 }
Exemple #2
0
        public async Task <IActionResult> Post([FromRoute] int accountId, [FromBody] AccountEventModel p_AccountEventModel)
        {
            string error;

            if (!p_AccountEventModel.IsValidModel(out error))
            {
                return(BadRequest(error));
            }

            AccountEventEntity accountEvent = new AccountEventEntity(p_AccountEventModel, accountId);

            List <AccountEventEntity> accountEventEntities = m_AccountEventRepository.FindAllByAccount(accountId);
            double totalValue = (accountEventEntities.FindAll((a) => a.Type == eEventType.Credit).Sum((b) => b.Value) - accountEventEntities.FindAll((a) => a.Type == eEventType.Debt).Sum((b) => b.Value));

            if (p_AccountEventModel.Type == eEventType.Debt && p_AccountEventModel.Value > totalValue)
            {
                return(BadRequest("Valor informado para débito é maior que o saldo"));
            }
            else if (p_AccountEventModel.Type == eEventType.Debt)
            {
                totalValue = totalValue - p_AccountEventModel.Value;
            }
            else if (p_AccountEventModel.Type == eEventType.Credit)
            {
                totalValue = totalValue + p_AccountEventModel.Value;
            }

            if (m_AccountEventRepository.Insert(accountEvent))
            {
                return(Ok(new AccountEventResponsePostModel( )
                {
                    Type = accountEvent.Type,
                    Value = accountEvent.Value,
                    TotalValue = totalValue
                }));
            }
            else
            {
                return(BadRequest("Falha ao inserir movimentação da conta"));
            }
        }