Example #1
0
 public ActionResult PostVoto(VotoWrapper entity)
 {
     try
     {
         this._logger.LogInformation("se va a registrar un voto " + JsonConvert.SerializeObject(entity));
         var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
         var rta    = this._rondaVotacionService.AddVoto(entity, Guid.Parse(userId));
         if (rta)
         {
             return(Ok(new { status = true, message = rta }));
         }
         else
         {
             return(BadRequest(new { status = false, message = "Error creando el objeto" }));
         }
     }
     catch (Exception ex)
     {
         this._logger.LogError("Error registrando un voto-> " + ex.Message);
         return(BadRequest(new { status = true, message = ex.Message }));
     }
 }
        public Boolean AddVoto(VotoWrapper entity, Guid userId)
        {
            var registro = false;

            try
            {
                using (var transaction = this._applicationDBContext.Database.BeginTransaction())
                {
                    try
                    {
                        var rondaVotacion = this._applicationDBContext.Set <RondaVotacionEntity>()
                                            .Where(i => i.Id.Equals(entity.RondaId))
                                            .FirstOrDefault();

                        var votante = (from x in this._applicationDBContext.Set <VotanteEntity>()
                                       join vv in this._applicationDBContext.Set <VotacionVotanteEntity>() on x.Id equals vv.IdVotante
                                       where vv.IdVotacion.Equals(rondaVotacion.IdVotacion) && x.UserId.ToLower().Equals(userId.ToString().ToLower())
                                       select vv
                                       ).FirstOrDefault();
                        if (votante != null)
                        {
                            var continuar =
                                this._applicationDBContext.Set <ControlVotoVotanteEntity>()
                                .Where(i => i.IdRondaVotacion.Equals(entity.RondaId) && i.IdVotacionVotante.Equals(votante.Id))
                                .Count();

                            if (continuar == 0)
                            {
                                var voto = new VotoRondaEntity()
                                {
                                    Id = Guid.NewGuid(),
                                    IdRondaVotacion  = entity.RondaId,
                                    idRondaCandidato = entity.CandidatoId,

                                    ///////////////////////////////////////
                                    EstadoRegistro = Data.Enums.HelpConstantes.EstadoRegistro.Activo,
                                    fechaCreacion  = DateTime.Now,
                                    fechaEdicion   = DateTime.Now,
                                };

                                var controlVoto = new ControlVotoVotanteEntity()
                                {
                                    Id = Guid.NewGuid(),
                                    IdRondaVotacion   = entity.RondaId,
                                    IdVotacionVotante = votante.Id,

                                    ///////////////////////////////////////
                                    EstadoRegistro = Data.Enums.HelpConstantes.EstadoRegistro.Activo,
                                    fechaCreacion  = DateTime.Now,
                                    fechaEdicion   = DateTime.Now
                                };



                                this._applicationDBContext.Add(voto);
                                this._applicationDBContext.Add(controlVoto);

                                this._applicationDBContext.SaveChanges();


                                transaction.Commit();
                                registro = true;
                            }
                            else
                            {
                                throw new Exception("Usted Ya completó su voto");
                            }
                        }
                        else
                        {
                            throw new Exception("No tiene permisos para votar");
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(registro);
        }