public async Task <IActionResult> Add([FromBody] RequestAnticipationModel model) { var anticipation = await _dbContext.Anticipations .WhereInProgress() .SingleOrDefaultAsync(); if (anticipation != null) { return(new AnticipationAlreadyInProgress(anticipation)); } var anticipationProcessing = new AnticipationProcessing(_dbContext); if (!await anticipationProcessing.Process(model)) { return(BadRequest()); } return(new AnticipationJson(anticipationProcessing.Anticipation)); }
public async Task <bool> Process(RequestAnticipationModel model) { Anticipation = model.Map(); var transferAmount = (decimal)0.0; foreach (var item in model.Transactions) { transferAmount += item.TransactionAmount.CalculateTransactionFees(item.Installments); Anticipation.AnticipationItems.Add(new AnticipationItem { AnticipationId = Anticipation.Id, TransactionId = item.Id }); } Anticipation.TransferAmount = transferAmount - AmountTransactionFee; _dbContext.Add(Anticipation); return(await _dbContext.SaveChangesAsync() > 0); }