private async Task <Response> ModifyBidPhase(string bidId, BidPhase newPhase) { BidEntity bid = await _context.Bids.FindAsync(bidId).ConfigureAwait(false); if (bid == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } bid.Phase = newPhase; try { _context.Bids.Update(bid); await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = ex.Message }); } return(new Response() { IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage() }); }
public async Task <Response <BidPhase> > TryUpdatePhase(string bidId) { BidEntity bid_ent = await _context.Bids.Where(b => b.Id == bidId).Include(b => b.CurrentProposals).Include(b => b.CurrentParticipancies).FirstOrDefaultAsync().ConfigureAwait(false); if (bid_ent == null) { return(new Response <BidPhase>() { IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } BidPhase currentPhase = bid_ent.Phase; BidPhase newPhase = currentPhase; switch (currentPhase) { case BidPhase.Join: if (bid_ent.ExpirationDate <= DateTime.UtcNow.AddHours(2)) { List <SupplierProposalEntity> relevantProposals = bid_ent.CurrentProposals.Where(proposal => proposal.MinimumUnits <= bid_ent.UnitsCounter && proposal.ProposedPrice <= bid_ent.MaxPrice).ToList(); int numOfProposals = relevantProposals.Count(); if (numOfProposals == 0) { newPhase = BidPhase.CancelledSupplierNotFound; } else if (numOfProposals == 1) { newPhase = BidPhase.Payment; } else { newPhase = BidPhase.Vote; } } break; case BidPhase.Vote: if (bid_ent.ExpirationDate.AddHours(48) <= DateTime.UtcNow.AddHours(2)) { newPhase = BidPhase.Payment; } break; case BidPhase.Payment: if (bid_ent.ExpirationDate.AddDays(5) <= DateTime.UtcNow.AddHours(2)) { if (bid_ent.CurrentParticipancies.Any(p => p.HasPaid == false)) { newPhase = BidPhase.CancelledNotEnoughBuyersPayed; } else { newPhase = BidPhase.Completed; } } break; // All others should update synchronously (with events) default: break; } if (newPhase != currentPhase) { bid_ent.Phase = newPhase; _context.Bids.Update(bid_ent); await _context.SaveChangesAsync().ConfigureAwait(false); } return(new Response <BidPhase>() { IsOperationSucceeded = newPhase != currentPhase, SuccessOrFailureMessage = this.getSuccessMessage(), DTOObject = newPhase }); }