Пример #1
0
        /// <summary>
        /// Permite insertar un teléfono que no exista en la base de datos. Como el teléfono está relacionado obligatoriamente con una empresa o un contacto uno de los dos parámetros será null.
        /// </summary>
        /// <param name="t">Teléfono a insertar.</param>
        /// <param name="empData">Empresa a la que pertenece el teléfono a insertar.</param>
        /// <param name="conData">Contacto al que pertenece el teléfono a insertar.</param>
        /// <returns>True si se ha insertado.</returns>
        public int AddTelefono(TelefonoData t, EmpresaData empData, ContactoData conData)
        {
            if (t == null) return -1;
            if (empData == null && conData == null) return -1;
            if (empData != null && conData != null) return -1;

            try
            {
                int id;
                using (GestionEmpresasEntities bd = new GestionEmpresasEntities())
                {
                    Telefono telefono = new Telefono()
                    {
                        idTelefono = t.idTelefono,
                        numero = t.numero,
                    };

                    if (empData != null)
                    {
                        var datos = from empresas in bd.Empresa
                                    where empresas.idEmpresa == empData.EmpresaID
                                    select empresas;
                        telefono.Empresa.Add(datos.First());
                    }
                    else
                    {
                        var datos = from contactos in bd.Contacto
                                    where contactos.idContacto == conData.idContacto
                                    select contactos;
                        telefono.Contacto.Add(datos.First());
                    }

                    bd.Telefono.Add(telefono);
                    bd.SaveChanges();
                    return telefono.idTelefono;
                }
            }
            catch (SqlException ex)
            {
                FaultException fault = new FaultException("ERROR SQL: " + ex.Message, new FaultCode("SQL"));
                throw fault;
            }
            catch (Exception ex)
            {
                FaultException fault = new FaultException("ERROR: " + ex.Message, new FaultCode("GENERAL"));
                throw fault;
            }
        }
Пример #2
0
        /// <summary>
        /// Permite editar un teléfono que exista en la base de datos. Como el teléfono está relacionado obligatoriamente con una empresa o un contacto uno de los dos parámetros será null.
        /// </summary>
        /// <param name="t">El teléfono a editar.</param>
        /// <returns>True si se ha editado.</returns>
        public int EditTelefono(TelefonoData t)
        {
            if (t == null) return -1;

            try
            {
                using (GestionEmpresasEntities bd = new GestionEmpresasEntities())
                {
                    var data = from telefonos in bd.Telefono
                               where telefonos.idTelefono == t.idTelefono
                               select telefonos;

                    Telefono telefono = data.First();
                    telefono.numero = t.numero;

                    bd.SaveChanges();
                    return telefono.idTelefono;
                }
            }
            catch (SqlException ex)
            {
                FaultException fault = new FaultException("ERROR SQL: " + ex.Message, new FaultCode("SQL"));
                throw fault;
            }
            catch (Exception ex)
            {
                FaultException fault = new FaultException("ERROR: " + ex.Message, new FaultCode("GENERAL"));
                throw fault;
            }
        }
Пример #3
0
        /// <summary>
        /// Permite obtener el listado de teléfonos existentes en la base de datos.
        /// </summary>
        /// <returns>La lista de teléfonos.</returns>
        public List<TelefonoData> GetAllTelefonos()
        {
            try
            {
                List<TelefonoData> listado = new List<TelefonoData>();

                using (GestionEmpresasEntities bd = new GestionEmpresasEntities())
                {
                    var datos = from telefonos in bd.Telefono
                                select telefonos;

                    foreach (Telefono tlf in datos)
                    {
                        TelefonoData tdata = new TelefonoData()
                        {
                            idTelefono = tlf.idTelefono,
                            numero = tlf.numero
                        };
                        listado.Add(tdata);
                    }
                }
                return listado;
            }
            catch (SqlException ex)
            {
                FaultException fault = new FaultException("ERROR SQL: " + ex.Message, new FaultCode("SQL"));
                throw fault;
            }
            catch (Exception ex)
            {
                FaultException fault = new FaultException("ERROR: " + ex.Message, new FaultCode("GENERAL"));
                throw fault;
            }
        }