public static Product GetProductById(NorthwindContext dbContext, int id) { try { return(GetAllProducts(dbContext).First(product => product.ProductId == id)); } catch (ArgumentNullException ex) { ex.SetMessage(DbExceptionMessages.FieldIsRequired("id")); throw; } catch (InvalidOperationException ex) { ex.SetMessage(DbExceptionMessages.InstanceNotFound(InstanceName, id)); throw; } }
public static Customer GetCustomerById(NorthwindContext dbContext, string id) { try { return(GetAllCustomers(dbContext).First(customer => customer.CustomerId == id)); } catch (ArgumentNullException ex) { ex.SetMessage(DbExceptionMessages.FieldIsRequired("id")); throw; } catch (InvalidOperationException ex) { ex.SetMessage(DbExceptionMessages.InstanceNotFound(InstanceName, id)); throw; } }
public static Employee GetEmployeeById(NorthwindContext dbContext, int id) { try { return(GetAllEmployees(dbContext).First(employee => employee.EmployeeId == id)); } catch (ArgumentNullException ex) { ex.SetMessage(DbExceptionMessages.FieldIsRequired("id")); throw; } catch (InvalidOperationException ex) { ex.SetMessage(DbExceptionMessages.InstanceNotFound(InstanceName, id)); throw; } }
public static void DeleteCustomer(NorthwindContext dbContext, string id) { try { Customer dataBaseCustomer = GetCustomerById(dbContext, id); dbContext.Customers.Remove(dataBaseCustomer); dbContext.SaveChanges(); } catch (Exception ex) when(ExceptionTypes.IsSqlException(ex)) { ex.SetMessage(DbExceptionMessages.FailedToDelete(InstanceName, id, ex.InnerException)); throw; } catch (Exception ex) when(ExceptionTypes.IsDbException(ex)) { ex.SetMessage(DbExceptionMessages.UnexpectedFailure(ex)); throw; } }
public static void DeleteProduct(NorthwindContext dbContext, int id) { try { Product dataBaseProduct = GetProductById(dbContext, id); dbContext.Products.Remove(dataBaseProduct); dbContext.SaveChanges(); } catch (Exception ex) when(ExceptionTypes.IsSqlException(ex)) { ex.SetMessage(DbExceptionMessages.FailedToDelete(InstanceName, id, ex.InnerException)); throw; } catch (Exception ex) when(ExceptionTypes.IsDbException(ex)) { ex.SetMessage(DbExceptionMessages.UnexpectedFailure(ex)); throw; } }
public static void UpdateEmployee(NorthwindContext dbContext, int id, EmployeeDTO modifiedEmployee) { try { Employee dataBaseEmployee = GetEmployeeById(dbContext, id); modifiedEmployee.ModifyDataBaseEmployee(dataBaseEmployee); dbContext.SaveChanges(); } catch (Exception ex) when(ExceptionTypes.IsSqlException(ex)) { ex.SetMessage(DbExceptionMessages.FailedToUpdate(InstanceName, id, ex.InnerException)); throw; } catch (Exception ex) when(ExceptionTypes.IsDbException(ex)) { ex.SetMessage(DbExceptionMessages.UnexpectedFailure(ex)); throw; } }
public static string AddNewCustomer(NorthwindContext dbContext, CustomerDTO newCustomer) { try { Customer dataBaseCustomer = newCustomer.GetDataBaseCustomerObject(); dbContext.Customers.Add(dataBaseCustomer); dbContext.SaveChanges(); return(dataBaseCustomer.CustomerId); } catch (Exception ex) when(ExceptionTypes.IsSqlException(ex)) { ex.SetMessage(DbExceptionMessages.FailedToAdd(InstanceName, ex.InnerException)); throw; } catch (Exception ex) when(ExceptionTypes.IsDbException(ex)) { ex.SetMessage(DbExceptionMessages.UnexpectedFailure(ex)); throw; } }
public static int AddNewProduct(NorthwindContext dbContext, ProductDTO newProduct) { try { Product dataBaseProduct = newProduct.GetDataBaseProductObject(); dbContext.Products.Add(dataBaseProduct); dbContext.SaveChanges(); return(dataBaseProduct.ProductId); } catch (Exception ex) when(ExceptionTypes.IsSqlException(ex)) { ex.SetMessage(DbExceptionMessages.FailedToAdd(InstanceName, ex.InnerException)); throw; } catch (Exception ex) when(ExceptionTypes.IsDbException(ex)) { ex.SetMessage(DbExceptionMessages.UnexpectedFailure(ex)); throw; } }
public static int AddNewEmployee(NorthwindContext dbContext, EmployeeDTO newEmployee) { try { Employee dataBaseEmployee = newEmployee.GetDataBaseEmployeeObject(); dbContext.Employees.Add(dataBaseEmployee); dbContext.SaveChanges(); return(dataBaseEmployee.EmployeeId); } catch (Exception ex) when(ExceptionTypes.IsSqlException(ex)) { ex.SetMessage(DbExceptionMessages.FailedToAdd(InstanceName, ex.InnerException)); throw; } catch (Exception ex) when(ExceptionTypes.IsDbException(ex)) { ex.SetMessage(DbExceptionMessages.UnexpectedFailure(ex)); throw; } }