Ejemplo n.º 1
0
        public async Task <Signature> Create([FromBody] BlockChain electionChain)
        {
            Signature result = null;

            try
            {
                UOW.BeginTransaction();
                Signature signature = this.GetSignatureFromBlockChain(electionChain);
                // now check to make sure the nonce matches the expected nonce
                (int expectedNonce, Guid ballotRequestId) = await this.signatureRepository.GetExpectedNonce(UOW, signature.BallotId);

                if (ballotRequestId != Guid.Empty)
                {
                    var br = await ballotRepository.BallotRequestGetById(Context, ballotRequestId);

                    // make sure the nonce and device id are correct
                    if (signature == null || expectedNonce != electionChain.GetLatestBlock().Nonce ||
                        (br != null && signature.DeviceId != br.DeviceId))
                    {
                        return(null);
                    }

                    Signature existingSignature = await this.GetByBallotId(signature.BallotId);

                    if (existingSignature != null)
                    {
                        result = await signatureRepository.UpdateBallotVotes(UOW, existingSignature, signature);
                    }
                    else
                    {
                        result = await this.InsertNewSignatureBallot(UOW, signature);
                    }
                    UOW.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                string err = ex.Message;
            }
            finally
            {
                UOW.CloseTransaction();
            }
            return(result);
        }