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>
        /// Check User for psw and username
        ///
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User Login(string username, string password)
        {
            string usernameInDb;

            using (var ctx = new WarehouseDataAccess.WarehouseDBContext())
            {
                ctx.Database.Log = (message) => Debug.WriteLine(message);
                Employee employee = ctx.Employees.Where(p => p.UserName == username).FirstOrDefault();
                if (employee == null)
                {
                    ErrorUser.InvalidLogin = false;
                    return(ErrorUser);
                }
                else
                {
                    usernameInDb = employee.UserName;

                    if (usernameInDb != null)
                    {
                        //Check the  password
                        string Salt = employee.PassWordSalt;
                        string pswd = CalculateHashPassword(Salt, password);

                        if (pswd == employee.PassWord)
                        {
                            //Create a User from Employee

                            User user = new User();
                            user.Email           = employee.Email;
                            user.Name            = employee.FullName();
                            user.Password        = employee.PassWord;
                            user.Role            = employee.JobTitle;
                            user.Username        = employee.UserName;
                            user.IsAuthenticated = true;
                            return(user);
                        }

                        else
                        {
                            // Should raise error  => capture that error and display that message to User

                            ErrorUser.InvalidLogin = false;
                            // Find the Username => then
                            if (employee.FailedPasswordAttemptCount == 3)
                            {
                                employee.IsLockedOut      = true;
                                ErrorUser.IsAuthenticated = false;
                                return(ErrorUser);
                            }
                            employee.FailedPasswordAttemptCount++;
                            ErrorUser.IsAuthenticated = false;
                            return(ErrorUser);
                        }
                    }
                }
            }
            ErrorUser.IsAuthenticated = false;

            return(ErrorUser);
        }
 public List <Employee> GiveAllEmployees()
 {
     // Get the data from the DATABASE
     using (WarehouseDataAccess.WarehouseDBContext ctx = new WarehouseDataAccess.WarehouseDBContext())
     {
         //Get all the Data out of the database
         foreach (var empl in ctx.Employees)
         {
             employees.Add(empl);
         }
     }
     return(employees);
 }
        public IEnumerable <Customers> GetAllCustomers()
        {
            List <Customers> customers = new List <Customers>();

            using (var context = new WarehouseDataAccess.WarehouseDBContext())
            {
                foreach (var customer in context.Customers)
                {
                    customers.Add(customer);
                }

                return(customers);
            }
        }
        /// <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);
            }
        }
        public List <CategoryWithProducts> getCategoriesWithProducts()
        {
            using (var context = new WarehouseDataAccess.WarehouseDBContext())
            {
                var data = context.Categories.Join(context.Products,
                                                   c => c.Id,
                                                   p => p.CategoryId,
                                                   (c, p) => new CategoryWithProducts
                {
                    CategoryName        = c.CategoryName,
                    CategoryDescription = c.Description,
                    ProductName         = p.ProductName,
                    MadeIn       = p.MadeIn,
                    ModelYear    = p.ModelYear,
                    Price        = p.UnitPrice,
                    UnitsOnOrder = p.UnitsOnOrder,
                }).ToList();

                return(data);
            }
        }
        /// <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);
            }
        }