Exemplo n.º 1
0
        public NaturezaModel RecuperarPeloId(int id)
        {
            NaturezaModel ret = null;

            Connection();

            using (SqlCommand command = new SqlCommand("SELECT *            " +
                                                       "  FROM Natureza " +
                                                       " WHERE Id=@Id", con))
            {
                con.Open();

                command.Parameters.AddWithValue("@Id", SqlDbType.Int).Value = id;

                var reader = command.ExecuteReader();

                if (reader.Read())
                {
                    ret = new NaturezaModel()
                    {
                        Id     = (int)reader["Id"],
                        Codigo = (string)reader["Codigo"],
                        Nome   = (string)reader["Nome"]
                    };
                }
            }
            return(ret);
        }
Exemplo n.º 2
0
        public int Salvar(NaturezaModel naturezaModel)
        {
            var ret = 0;

            var model = RecuperarPeloId(naturezaModel.Id);

            if (model == null)
            {
                Connection();

                using (SqlCommand command = new SqlCommand("    INSERT INTO Natureza ( Nome,        " +
                                                           "                           Codigo       " +
                                                           "                         )              " +
                                                           "                  VALUES ( @Nome,       " +
                                                           "                           @Codigo      " +
                                                           "                         );             " +
                                                           " select convert(int, scope_identity())  ", con))
                {
                    con.Open();

                    command.Parameters.AddWithValue("@Nome", SqlDbType.VarChar).Value   = naturezaModel.Nome;
                    command.Parameters.AddWithValue("@Codigo", SqlDbType.VarChar).Value = naturezaModel.Codigo;

                    ret = (int)command.ExecuteScalar();
                }
            }
            else
            {
                Connection();

                using (SqlCommand command = new SqlCommand("UPDATE Natureza       " +
                                                           "   SET Nome=@Nome,    " +
                                                           "       Codigo=@Codigo " +
                                                           " WHERE Id=@Id         ", con))
                {
                    con.Open();

                    command.Parameters.AddWithValue("@Nome", SqlDbType.VarChar).Value = naturezaModel.Nome;
                    command.Parameters.AddWithValue("@Codigo", SqlDbType.Int).Value   = naturezaModel.Codigo;
                    command.Parameters.AddWithValue("@Id", SqlDbType.Int).Value       = naturezaModel.Id;

                    if (command.ExecuteNonQuery() > 0)
                    {
                        ret = naturezaModel.Id;
                    }
                }
            }
            return(ret);
        }
Exemplo n.º 3
0
        public JsonResult SalvarNatureza(NaturezaModel naturezaModel)
        {
            var resultado = "OK";
            var mensagens = new List <string>();
            var idSalvo   = string.Empty;

            if (!ModelState.IsValid)
            {
                resultado = "AVISO";
                mensagens = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage).ToList();
            }
            else
            {
                try
                {
                    naturezaRepositorio = new NaturezaRepositorio();

                    var id = naturezaRepositorio.Salvar(naturezaModel);

                    if (id > 0)
                    {
                        idSalvo = id.ToString();
                    }
                    else
                    {
                        resultado = "ERRO";
                    }
                }
                catch (Exception ex)
                {
                    resultado = "ERRO";
                    throw new Exception(ex.Source);
                }
            }
            return(Json(new { Resultado = resultado, Mensagens = mensagens, IdSalvo = idSalvo }));
        }
 public void Delete(NaturezaModel naturezaModel)
 {
     _context.InfracaoNatureza.Remove(naturezaModel);
     _context.SaveChanges();
 }
 public void Update(NaturezaModel naturezaModel)
 {
     _context.Entry(naturezaModel).State = EntityState.Modified;
     _context.SaveChanges();
 }
 public void Insert(NaturezaModel naturezaModel)
 {
     _context.InfracaoNatureza.Add(naturezaModel);
     _context.SaveChanges();
 }
        public GenericResult Exec(InfracaoInsertCommand command)
        {
            GrupoModel grupoModel = _repositoryGrupo.GetById(command.GrupoId);

            if (grupoModel == null)
            {
                command.AddNotification("GrupoId", "Grupo Inexistente");
            }

            NaturezaModel naturezaModel = _repositoryNatureza.GetById(command.NaturezaId);

            if (naturezaModel == null)
            {
                command.AddNotification("Natureza", "Natureza Inexistente");
            }

            InfracaoModel model = _repository.GetByCodigoEValidadeInicio(command.Codigo, command.ValidadeInicio);

            if (model != null)
            {
                command.AddNotification("Codigo e ValidadeInicio", "Par (Código e Validade) já existente para o id:" + model.Id.ToString());
            }



            command.Validate();
            if (command.Invalid)
            {
                return(new GenericResult(400, "Recurso Inválido", command.Notifications));
            }


            model = new InfracaoModel(
                command.Codigo, command.Descricao, command.AmparoLegal, command.MedidaAdm, command.Crime, command.ObsObrigatoria, command.GrupoId, command.NaturezaId,
                command.CompetenciaMunicipal, command.CompetenciaEstadual,
                command.CompetenciaRodoviaria, command.InfratorCondutor, command.InfratorProprietario,
                command.ValidadeInicio
                );



            // inicio Enquadrando
            InfracaoModel infracaoMenor = _repository.GetByCodigoValidadeInicioMenor(0, command.Codigo, command.ValidadeInicio);
            InfracaoModel infracaoMaior = _repository.GetByCodigoValidadeInicioMaior(0, command.Codigo, command.ValidadeInicio);


            if (infracaoMenor != null)
            {
                infracaoMenor.SetDataFim(command.ValidadeInicio.AddDays(-1));
            }

            if (infracaoMaior != null)
            {
                model.SetDataFim(infracaoMaior.ValidadeInicio.AddDays(-1));
            }
            // fim Enquadrando


            try
            {
                if (infracaoMenor != null)
                {
                    _repository.Update(infracaoMenor);
                }

                _repository.Insert(model);

                model = _repository.GetById(model.Id);

                return(new GenericResult(201, "Recurso Inserido", model));
            }
            catch (System.Exception ex)
            {
                return(new GenericResult(503, ex.GetBaseException().Message, null));
            }
        }