// Create employee public int create(String firstname, String lastname, DateTime DOB, String contactNum, DateTime startDate, int dept, int depot, int role) { // Establishes model EmployeeModel employeeModel = new EmployeeModel(); // Holds the new employee Employee newEmployee = new Employee(); // Stored details for the employee newEmployee.Firstname = firstname; newEmployee.Lastname = lastname; newEmployee.DOB = DOB; newEmployee.ContactNumber = contactNum; newEmployee.Startdate = startDate; newEmployee.Dept = dept; newEmployee.Depot = depot; newEmployee.Role = role; // Adds the object to the database int employeeID = employeeModel.NewEmployee(newEmployee); // Return the employeeID return employeeID; }
public void EditEmployee(Employee em) { using (connect = new MySqlConnection(_connectionString)) { connect.Open(); using (MySqlTransaction transaction = connect.BeginTransaction()) { try { string query = "EditEmployee"; var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.AddWithValue("EmployeeID", em.Id); cmd.Parameters.AddWithValue("PForename", em.Firstname); cmd.Parameters.AddWithValue("PSurname", em.Lastname); cmd.Parameters.AddWithValue("PDOB", em.DOB); cmd.Parameters.AddWithValue("ContactNumber", em.ContactNumber); cmd.Parameters.AddWithValue("StartDate", em.Startdate); cmd.Parameters.AddWithValue("EndDate", em.EndDate); cmd.Parameters.AddWithValue("DepartmentID", em.Dept); cmd.Parameters.AddWithValue("DepotID", em.Depot); cmd.Parameters.AddWithValue("RoleID", em.Role); cmd.ExecuteNonQuery(); transaction.Commit(); connect.Close(); } catch (InvalidOperationException ioException) { transaction.Rollback(); connect.Close(); } } } }
// Controller for modification of an employee public ActionResult EditEmployee() { // Null handling if (Session["loggedInState"] == null) { return Redirect("/403.html"); } // Checks if logged in bool state = (bool)Session["loggedInState"]; if (state == true) { // Creates an employee placeholder var employee = new Employee(); // Setup employee edit employee.Id = int.Parse(Request.Form["id"]); employee.Firstname = Request.Form["firstname"]; employee.Lastname = Request.Form["lastname"]; employee.DOB = DateTime.Parse(Request.Form["DOB"]); employee.ContactNumber = Request.Form["contactNum"]; employee.Startdate = DateTime.Parse(Request.Form["startDate"]); employee.Dept = int.Parse(Request.Form["depart"]); employee.Depot = int.Parse(Request.Form["depot"]); employee.Role = int.Parse(Request.Form["role"]); // Establish an employee model var employeeModel = new EmployeeModel(); // Conduct edit employeeModel.EditEmployee(employee); // Passes back to the view return Redirect("Employee/Employee"); } else { // If not logged in return Redirect("/login.html"); } }
// GET: Employee public ActionResult Index() { // Null handling if (Session["loggedInState"] == null) { return Redirect("/403.html"); } // Checks if logged in bool state = (bool)Session["loggedInState"]; if (state == true) { // Establishes employee model EmployeeModel employeeModel = new EmployeeModel(); // Holds the new employee Employee newEmployee = new Employee(); // Stored details for the employee newEmployee.Firstname = Request.Form[0]; newEmployee.Lastname = Request.Form[1]; newEmployee.DOB = DateTime.Parse(Request.Form[2]); newEmployee.ContactNumber = Request.Form[3]; newEmployee.Startdate = DateTime.Parse(Request.Form[4]); newEmployee.Dept = int.Parse(Request.Form[5]); newEmployee.Depot = int.Parse(Request.Form[6]); newEmployee.Role = int.Parse(Request.Form[7]); // Adds the object to the database employeeModel.NewEmployee(newEmployee); // Return created employee to view return View(newEmployee); } else { // If not logged in return Redirect("/login.html"); } }
public Employee SearchEmployee(Employee em) { return SearchEmployee(em.Id); }
public Employee SearchEmployee(int ID) { var emp = new Employee(); using (connect = new MySqlConnection(_connectionString)) { try { string query = "GetEmployee"; var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.AddWithValue("EmployeeID", ID); connect.Open(); var reader = cmd.ExecuteReader(); while (reader.Read()) { emp.Id = (int)reader["Employee_ID"]; emp.Firstname = reader["Forenames"].ToString(); emp.Lastname = reader["Surname"].ToString(); emp.DOB = DateTime.Parse(reader["DOB"].ToString()); emp.ContactNumber = reader["Contact_Number"].ToString(); emp.Startdate = DateTime.Parse(reader["Start_Date"].ToString()); try { emp.EndDate = DateTime.Parse(reader["End_Date"].ToString()); }catch(Exception){} emp.Dept = (int)reader["Department_ID"]; try{ emp.Depot = (int)reader["Depot_ID"]; }catch(Exception){} emp.Role = (int)reader["Role_ID"]; } connect.Close(); } catch (InvalidOperationException ioException) { connect.Close(); } return emp; } }
public List<Employee> GetEmployeesList() { var empList = new List<Employee>(); using (connect = new MySqlConnection(_connectionString)) { try { string query = "ListEmployee"; var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure }; connect.Open(); var reader = cmd.ExecuteReader(); while (reader.Read()) { var e = new Employee(); e.Id = (int)reader["Employee_ID"]; e.Firstname = reader["Forenames"].ToString(); e.Lastname = reader["Surname"].ToString(); e.DOB = DateTime.Parse(reader["DOB"].ToString()); e.ContactNumber = reader["Contact_Number"].ToString(); e.Startdate = DateTime.Parse(reader["Start_Date"].ToString()); try { e.EndDate = DateTime.Parse(reader["End_Date"].ToString()); }catch(Exception){} try { e.Dept = (int)reader["Department_ID"]; } catch (Exception) { } try { e.Depot = (int)reader["Depot_ID"]; } catch (Exception) { } try { e.Role = (int)reader["Role_ID"]; } catch (Exception) { } empList.Add(e); } connect.Close(); } catch (InvalidOperationException ioException) { connect.Close(); } return empList; } }