예제 #1
0
        public ClassificacaoFiscalModel RecuperarPeloId(int id)
        {
            ClassificacaoFiscalModel ret = null;

            Connection();

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

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

                var reader = command.ExecuteReader();

                if (reader.Read())
                {
                    ret = new ClassificacaoFiscalModel()
                    {
                        Id    = (int)reader["Id"],
                        Nome  = (string)reader["Nome"],
                        Ativo = (bool)reader["Ativo"]
                    };
                }
            }
            return(ret);
        }
예제 #2
0
        public int Salvar(ClassificacaoFiscalModel classificacaoFiscalModel)
        {
            var ret = 0;

            var modal = RecuperarPeloId(classificacaoFiscalModel.Id);

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

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

                    command.Parameters.AddWithValue("@Nome", SqlDbType.VarChar).Value = classificacaoFiscalModel.Nome;
                    command.Parameters.AddWithValue("@Ativo", SqlDbType.Int).Value    = classificacaoFiscalModel.Ativo;

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

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

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

                    if (command.ExecuteNonQuery() > 0)
                    {
                        ret = classificacaoFiscalModel.Id;
                    }
                }
            }
            return(ret);
        }
        public JsonResult SalvarClassificacaoFiscal(ClassificacaoFiscalModel classificacaoFiscalModel)
        {
            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
                {
                    classificacaoFiscalRepositorio = new ClassificacaoFiscalRepositorio();

                    var id = classificacaoFiscalRepositorio.Salvar(classificacaoFiscalModel);

                    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 }));
        }