public async Task <bool> Update(Customers customer) { try { // We only need to update the parts of the customer // that are different from the customer in the database // Get the Customer from the database using (var context = new WarehouseDataAccess.WarehouseDBContext()) { Customers customerDatabase = context.Customers.Find(customer.Id); if (customerDatabase == null) { return(false); } context.Entry(customerDatabase).CurrentValues.SetValues(customer); await context.SaveChangesAsync(); return(true); } } catch (Exception e) { return(false); } }
/// <summary> /// Add a new customer asynchronously and check for errors. Display those errors /// To the user /// </summary> /// <param name="customerParas"></param> /// <returns></returns> public async Task <bool> Add(string[] customerParas) { Customers customers = new Customers(); try { using (var context = new WarehouseDataAccess.WarehouseDBContext()) { string[] name = customerParas[0].Trim().Split(' '); var firstname = NameWithACapitalLetter(name[0]); var lastname = NameWithACapitalLetter(name[1]); //Check if there are no duplicates var CustomerExist = context.Customers.FirstOrDefault(p => p.FirstName == firstname && p.LastName == lastname); if (CustomerExist == null) { customers.FirstName = firstname; customers.LastName = lastname; customers.Email = customerParas[1]; customers.Phone = customerParas[2]; customers.Country = customerParas[3]; customers.City = customerParas[4]; customers.Street = customerParas[5]; customers.Number = "0"; context.Customers.Add(customers); await context.SaveChangesAsync(); Console.WriteLine("Save Complete"); return(true); } return(false); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } return(false); } }
public async Task <bool> Delete(Customers Customer) { try { using (var context = new WarehouseDataAccess.WarehouseDBContext()) { Customers DeleteCustomer = context.Customers.Find(Customer.Id); context.Customers.Remove(DeleteCustomer); //Update the Id's in the database await context.SaveChangesAsync(); return(true); } } catch (Exception e) { Console.WriteLine($"{e.InnerException} => {e.Message}: {e.Data}"); return(false); } }
/// <summary> /// Need to get the password /// Need the username /// No duplicates => First Check if user not in database /// Password hashing sha256 /// </summary> /// <param name="FirstName"></param> /// <param name="LastName"></param> /// <param name="Username"></param> /// <param name="Password"></param> public List <string> Register(newEmployeeParams newEmployee) { //Check first of the user doesn't exist already bool userExist = false; try { using (var context = new WarehouseDataAccess.WarehouseDBContext()) { // if empty than false userExist = context.Employees.FirstOrDefault(p => p.FirstName == newEmployee.FirstName && p.LastName == newEmployee.LastName) != null ? true : false; if (userExist == false) { //Generate an 128-bit random number salt to make crypto collitions unlikely //First hash password var supervisor = context.Supervisor.First(); string Salt = GenerateSalt(); password = CalculateHashPassword(newEmployee.Password, Salt); Console.WriteLine($"{newEmployee.FirstName} {newEmployee.Gender} {newEmployee.Salaries} {newEmployee.Password}"); Employee Employee = new Employee { FirstName = newEmployee.FirstName, LastName = newEmployee.LastName, Email = newEmployee.Email, PassWord = password, PassWordSalt = Salt, JobTitle = newEmployee.JobTitle, FailedPasswordAttemptCount = 0, IsActive = true, UserName = newEmployee.Username, Salary = newEmployee.Salaries, Gender = newEmployee.Gender, BirthDate = newEmployee.BirthDate, Supervisor = supervisor }; context.Employees.Add(Employee); context.SaveChangesAsync(); } else { Errors.Add($"{newEmployee.FirstName} {newEmployee.LastName} is already in the database"); // Raise an Error when the User return(Errors); } } return(Errors); //Check firstName and Lastname and Email in datbase zit // Als dat zo is dan User already exist } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); Errors.Add(ve.ErrorMessage); } } return(Errors); } }