private async Task FillInternalEntities(AnticipationEntity entity) { entity.SolicitedPayments = await _dbContext.Set <PaymentEntity>() .Where(x => x.AnticipationId == entity.Id) .ToListAsync(); entity.Analysis = await _dbContext.Set <AnticipationAnalysis>() .Where(x => x.AnticipationId == entity.Id) .FirstOrDefaultAsync(); }
public async Task <AnticipationResult> Create(List <int> paymentIds, DateTime solicitationDate) { // Check if there's a open solicitation var openAnalysis = await _dbContext.Set <AnticipationAnalysis>() .Where(x => x.EndDate == null) .FirstOrDefaultAsync(); if (openAnalysis != null) { return(_resultService.GenerateFailedResult("Cannot open solicitation without finilizing the current one.", true)); } var payments = await _dbContext.Set <PaymentEntity>() .Where(x => paymentIds.Contains(x.Id) && x.AnticipationId == null && x.Approved == true) .ToListAsync(); if (payments.Count() == 0) { return(_resultService.GenerateFailedResult("None of the solicited payments are available for anticipation.", true)); } var entity = new AnticipationEntity { SolicitationDate = solicitationDate, SolicitedValue = 0, }; await _dbContext.AddAsync(entity); await _dbContext.SaveChangesAsync(); // sets the payment.solicitationId fields to the current anticipation id // needs to be done after the SaveChangesAsync so the id is set. foreach (var payment in payments) { payment.AnticipationId = entity.Id; entity.SolicitedValue += payment.LiquidValue * TaxRate; } var analysis = new AnticipationAnalysis { AnticipationId = entity.Id }; await _dbContext.AddAsync(analysis); await _dbContext.SaveChangesAsync(); await FillInternalEntities(entity); return(_resultService.GenerateResult(entity)); }
public AnticipationResult GenerateResult(AnticipationEntity anticipation) => new AnticipationResult(anticipation);