Exemplo n.º 1
0
        public List <Anomalia> BuscarTodos()
        {
            var lstAnomalia = new List <Anomalia>();

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Db"].ConnectionString))
            {
                string strSQL = @"SELECT * FROM ANOMALIA;";

                using (SqlCommand cmd = new SqlCommand(strSQL))
                {
                    conn.Open();
                    cmd.Connection  = conn;
                    cmd.CommandText = strSQL;

                    var dataReader = cmd.ExecuteReader();
                    var dt         = new DataTable();
                    dt.Load(dataReader);
                    conn.Close();

                    foreach (DataRow row in dt.Rows)
                    {
                        var anomalia = new Anomalia()
                        {
                            Id        = Convert.ToInt32(row["ID"]),
                            Descricao = row["DESCRICAO"].ToString()
                        };

                        lstAnomalia.Add(anomalia);
                    }
                }
            }

            return(lstAnomalia);
        }
        public async Task <IActionResult> Crear([FromBody] CrearViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Anomalia anomalia = new Anomalia
            {
                nombre      = model.nombre,
                descripcion = model.descripcion,
                activo      = true,
                eliminado   = false
            };

            _context.Anomalias.Add(anomalia);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }

            return(Ok());
        }