Esempio n. 1
0
 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;
     }
 }
Esempio n. 2
0
 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;
     }
 }
Esempio n. 3
0
 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;
     }
 }
Esempio n. 4
0
        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;
            }
        }
Esempio n. 5
0
        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;
            }
        }
Esempio n. 6
0
        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;
            }
        }
Esempio n. 7
0
        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;
            }
        }
Esempio n. 8
0
        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;
            }
        }
Esempio n. 9
0
        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;
            }
        }