private void btnSaveEmployee_Click(object sender, RoutedEventArgs e) { if (tbFirstname.Text.Equals("") || tbSurname.Text.Equals("")) { MessageBox.Show("Please enter Surname and Firstname"); return; } int gender = 1; if (rbGenderFemale.IsChecked == true) { gender = 0; } Department result = null; if (cbDepartment.SelectedIndex > -1) { result = (Department)cbDepartment.Items.GetItemAt(cbDepartment.SelectedIndex); } else { MessageBox.Show("Please enter Department"); return; } Employee emp = new Employee(tbFirstname.Text, tbSurname.Text, (EmployeeGender)gender, result); service.insertEmployee(emp); this.loadList(); }
public bool deleteDepartmentById(int id) { List<Department> departments = getAllDepartments(); foreach (XmlNode employee in employeesRoot.ChildNodes) { if (Convert.ToInt32(employee.Attributes["Department"].InnerText) == id) { Employee emp = new Employee(employee.Attributes["Firstname"].InnerText, employee.Attributes["Lastname"].InnerText, (EmployeeGender)Convert.ToInt32(employee.Attributes["Gender"].InnerText), getDepartmentById(Convert.ToInt32(employee.Attributes["Id"].InnerText))); emp.Department = null; updateEmployee(emp); /* employee.Attributes["Department"].InnerText = null; employeesDoc.Save(@"..\\..\\data\\xml\\employees.xml"); */ } } foreach (XmlNode department in departmentsRoot.ChildNodes) { if (Convert.ToInt32(department.Attributes["Id"].InnerText) == id) { departmentsRoot.RemoveChild(department); departmentsDoc.Save(@"..\\..\\data\\xml\\departments.xml"); return true; } } return false; }
public void InsertEmpl() { int selectionIntGender; Console.WriteLine("Input firstname:"); string firstname = Console.ReadLine(); if (firstname.Trim().Equals("") || !Regex.IsMatch(firstname, "^\\w[a-zA-Z\\s]+$")) { Console.WriteLine("Error, please enter a valid firstname"); InsertEmpl(); return; } bool sacksession = true; string lastname = ""; while (sacksession) { Console.WriteLine("Input lastname:"); lastname = Console.ReadLine(); if (lastname.Trim().Equals("") || !Regex.IsMatch(lastname, "^\\w[a-zA-Z\\s]+$")) { Console.WriteLine("Error, please enter a valid lastname"); } else { sacksession = false; } } bool successGender = false; do { Console.WriteLine("Choose gender: {0} = Male | {1} = Female", (int)EmployeeGender.Male, (int)EmployeeGender.Female); string selectionGender = Console.ReadLine(); successGender = int.TryParse(selectionGender, out selectionIntGender); switch (selectionIntGender) { case ((int) EmployeeGender.Male): case ((int) EmployeeGender.Female): successGender = successGender && true; break; default: successGender = false; break; } if (!successGender) { Console.WriteLine("Invalid Input - try again"); Console.WriteLine("Choose gender: {0} = Male | {1} = Female", (int)EmployeeGender.Male, (int)EmployeeGender.Female); } } while (!successGender); Console.WriteLine("Input department or create a new one:"); List<Department> listDeps = service.getDepartments(); foreach (Department d_temp in listDeps) Console.WriteLine(d_temp.Id + " -> " + d_temp.Name); Department result = null; if (listDeps.Count == 0) { Console.WriteLine("No Departmens found - Create a new one!"); string selDepID = ""; do { Console.WriteLine("Department name"); selDepID = Console.ReadLine(); if (selDepID.Trim().Equals("") || !Regex.IsMatch(selDepID, "^\\w[a-zA-Z\\s]+$")) { Console.WriteLine("Please enter a valid department name"); selDepID = null; } } while (selDepID == null); result = new Department(selDepID); service.insertDepartment(result); result = service.getDepartment(result.Id); Console.WriteLine("Department inserted"); } else { int selDepID; sacksession = true; while (sacksession) { Console.WriteLine("Input ID."); string selectionEmplIDString = Console.ReadLine(); //try to parse string to int bool successEmplID = int.TryParse(selectionEmplIDString, out selDepID); result = service.getDepartment(selDepID); if (result == null) { Console.WriteLine("Error, please enter a valid id"); } else { sacksession = false; } } } Employee emp = new Employee(0, firstname, lastname, (EmployeeGender)selectionIntGender, result); service.insertEmployee(emp); Console.WriteLine("Employee inserted"); Console.WriteLine("Press Enter to go back to mainmenu"); Console.ReadLine(); Console.Clear(); }
public bool updateEmployee(Employee employee) { if (employee.Id == 0) throw new ArgumentException(); SQLiteConnection connection = DbConnection; try { connection.Open(); SQLiteCommand command = new SQLiteCommand(connection); command.CommandText = "UPDATE `employees` SET firstname = @firstname, lastname = @lastname, gender = @gender, fk_department_nr = @department WHERE id = @id"; command.Prepare(); command.Parameters.AddWithValue("@firstname", employee.Firstname); command.Parameters.AddWithValue("@lastname", employee.Lastname); command.Parameters.AddWithValue("@gender", employee.Gender); command.Parameters.AddWithValue("@department", employee.Department.Id); command.Parameters.AddWithValue("@id", employee.Id); command.ExecuteNonQuery(); } catch (Exception e) { throw e; } finally { connection.Close(); } return true; }
public bool insertEmployee(Employee employee) { SQLiteConnection connection = DbConnection; try { connection.Open(); SQLiteCommand command = new SQLiteCommand(connection); command.CommandText = "INSERT INTO `employees` (firstname, lastname, gender, fk_department_nr) VALUES (@firstname, @lastname, @gender, @department)"; command.Prepare(); command.Parameters.AddWithValue("@firstname", employee.Firstname); command.Parameters.AddWithValue("@lastname", employee.Lastname); command.Parameters.AddWithValue("@gender", employee.Gender); command.Parameters.AddWithValue("@department", employee.Department.Id); command.ExecuteNonQuery(); // update the new id command.CommandText = @"SELECT last_insert_rowid()"; SQLiteDataReader reader = command.ExecuteReader(); reader.Read(); employee.Id = reader.GetInt32(0); } catch (Exception e) { throw e; } finally { connection.Close(); } return true; }
public bool updateEmployee(Employee employee) { deleteEmployeeById(employee.Id); insertEmployee(employee); return true; }
public bool insertEmployee(Employee employee) { int Id = 0; foreach (XmlNode emp2 in employeesRoot.ChildNodes) { if (Convert.ToInt32(emp2.Attributes["Id"].InnerText) > Id) Id = Convert.ToInt32(emp2.Attributes["Id"].InnerText); } XmlNode emp = employeesDoc.CreateElement("employee"); XmlAttribute empId = employeesDoc.CreateAttribute("Id"); empId.Value = Convert.ToString(Id + 1); XmlAttribute empFirstname = employeesDoc.CreateAttribute("Firstname"); empFirstname.Value = employee.Firstname; XmlAttribute empLastname = employeesDoc.CreateAttribute("Lastname"); empLastname.Value = employee.Lastname; XmlAttribute empGender = employeesDoc.CreateAttribute("Gender"); empGender.Value = ((int)employee.Gender).ToString(); int departmentId = employee.Department != null ? employee.Department.Id : 0; XmlAttribute empDepartment = employeesDoc.CreateAttribute("Department"); empDepartment.Value = Convert.ToString(departmentId); emp.Attributes.Append(empId); emp.Attributes.Append(empFirstname); emp.Attributes.Append(empLastname); emp.Attributes.Append(empGender); emp.Attributes.Append(empDepartment); employeesRoot.AppendChild(emp); employeesDoc.Save(@"..\\..\\data\\xml\\employees.xml"); return true; }
public Employee getEmployeeById(int id) { Employee emp = new Employee(1, "a", "f", new EmployeeGender(), new Department("test")); List<Employee> employees = getAllEmployees(); foreach (Employee employee in employees) { if (employee.Id == id) emp = employee; } return emp; }
public bool updateEmployee(Employee employee) { return store.updateEmployee(employee); }
public bool insertEmployee(Employee employee) { return store.insertEmployee(employee); }