Task IInsumoRepository.DeleteById(int id) { try { using (var dbContext = new InsumosDbContext(_conectionString)) { var field = dbContext.Insumos.Find(id); dbContext.Insumos.Remove(field); //dbContext.Entry(field).State = System.Data.Entity.EntityState.Deleted; dbContext.SaveChanges(); } return(Task.CompletedTask); } catch (Exception ex) { if (ex.InnerException.GetType() == typeof(SqlException)) { SqlException innerException = ex.InnerException as SqlException; if (innerException.Number == 2627) { throw new DuplicateItemException(); } } throw; } }
Task IInsumoRepository.Update(Insumo updatedObj) { try { using (var dbContext = new InsumosDbContext(_conectionString)) { var field = dbContext.Insumos.Find(updatedObj.Id); field.nombre = updatedObj.Nombre; field.descripcion = updatedObj.Descripcion; dbContext.Entry(field).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); } return(Task.CompletedTask); } catch (Exception ex) { if (ex.InnerException.GetType() == typeof(SqlException)) { SqlException innerException = ex.InnerException as SqlException; if (innerException.Number == 2627) { throw new DuplicateItemException(); } } throw; } }
Task IInsumoRepository.Create(Insumo newObj) { try { using (var dbContext = new InsumosDbContext(_conectionString)) { if (!dbContext.Insumos.Any(i => i.nombre == newObj.Nombre)) { InsumosDao newField = new InsumosDao() { nombre = newObj.Nombre, descripcion = newObj.Descripcion }; dbContext.Insumos.Add(newField); dbContext.SaveChanges(); } else { throw new DuplicateItemException(); } } return(Task.CompletedTask); } catch (Exception ex) { if (ex.InnerException.GetType() == typeof(SqlException)) { SqlException innexException = ex.InnerException as SqlException; if (innexException.Number == 2627) { throw new DuplicateItemException(); } } throw; } }