public async Task <Election> UpdateElectionAsync(UpdateElection election) { Election old = _commonDbContext.Elections .Include(e => e.Candidates) .SingleOrDefault(e => e.Id == election.Id); if (old == null) { throw new NotFoundException("انتخابات"); } List <ElectionCandidate> candidates = _mapper.Map <List <ElectionCandidate> >(election.Candidates); ValidateElectionCandidate(candidates); _commonDbContext.RemoveRange(old.Candidates); await _commonDbContext.ElectionCandidates.AddRangeAsync(candidates); old.Name = election.Name; _commonDbContext.Elections.Update(old); await _commonDbContext.SaveChangesAsync(); return(old); }
public async Task <Election> UpdateElectionAsync(UpdateElection election) { Election old = _commonDbContext.Elections .Include(e => e.Candidates) .SingleOrDefault(e => e.Id == election.Id); if (old == null) { throw new NotFoundException("election"); } bool usedInBlockchain = _dbContext.Blocks .ToList() .SelectMany(b => JsonConvert.DeserializeObject <List <Transaction> >(b.Data)) .Any(t => t.Outputs.Any(o => o.ElectionAddress == old.Address)); bool usedInTransactions = await _dbContext.Transactions.AnyAsync(t => t.Outputs.Any(o => o.ElectionAddress == old.Address)); if (usedInBlockchain || usedInTransactions) { throw new ValidationException("Voting has not taken place in this election and cannot be edited."); } List <ElectionCandidate> candidates = _mapper.Map <List <ElectionCandidate> >(election.Candidates); ValidateElectionCandidate(candidates); _commonDbContext.RemoveRange(old.Candidates); await _commonDbContext.ElectionCandidates.AddRangeAsync(candidates); old.Name = election.Name; _commonDbContext.Elections.Update(old); await _commonDbContext.SaveChangesAsync(); return(old); }
public async Task <IActionResult> CloseElection([FromBody] UpdateElection election) { await _electionService.CloseElectionAsync(election.Id); return(Ok()); }