/// <summary> /// Deletes a given employee if it is found in the database. It will leave all the tasks given to it as ABANDONED. /// </summary> /// <param name="pEmployee">Receives the employee that is meant to be deleted</param> /// <returns></returns> public static pojo.Employee deleteEmployee(pojo.Employee pEmployee) { using (var context = new synupEntities()) { pojo.Employee _oEmployee = readEmployee(pEmployee.nif, context); //Finds the received employee in the database. if (_oEmployee != null) //If the employee has been found - meaning that it exists: { if (checkRelations(_oEmployee)) //Checks the employee foreign keys and performs an update. If the update is succesfull it will step in the next code block. { tryAttach(context, _oEmployee); context.Employees.Remove(_oEmployee); //Will be deleted. if (commitChanges(context)) { return(_oEmployee); //If the changes are commited succesfully it will return the deleted Employee. } else { return(null); } } } } return(null); }
/// <summary> /// Attachs employees to the current session so it can be updated. /// </summary> /// <param name="pContext">The session the operation will be performed under.</param> /// <param name="pEmployee">The object which were attachs</param> private static void tryAttach(synupEntities pContext, pojo.Employee pEmployee) { var entry = pContext.Entry(pEmployee); if (entry.State == System.Data.Entity.EntityState.Detached) { pContext.Employees.Attach(pEmployee); } }
/// <summary> /// Inserts in the database the given employee by parameter. Checks if the employee already exists. /// </summary> /// <param name="pEmployee">Receives the object Employee that will be inserted in the database.</param> /// <returns>Returns a boolean depending in the outcome of the insert - true if it is successfull</returns> public static bool createEmployee(pojo.Employee pEmployee) { pojo.Employee _oEmployee = readEmployee(pEmployee.nif); //Finds the received employee in the database. if (_oEmployee == null) { using (var context = new synupEntities()) { pEmployee.password = MD5Hash(pEmployee.password); context.Employees.Add(pEmployee); //If the employee doesn't exist already in the database, it will be inserted. return(commitChanges(context)); } } return(false); }
/// <summary> /// Receives the employee that is meant to be updated and updates it. /// </summary> /// <param name="pEmployee">Receives the employee that will be updated</param> /// <returns>Returns a boolean whether the employee has been updated succesfully or not.</returns> public static bool updateEmployee(pojo.Employee pEmployee) { using (var context = new synupEntities()) { pojo.Employee _oEmployee = readEmployee(pEmployee.nif, context); if (_oEmployee != null) { _oEmployee.name = pEmployee.name; _oEmployee.surname = pEmployee.surname; _oEmployee.phone = pEmployee.phone; _oEmployee.email = pEmployee.email; _oEmployee.adress = pEmployee.adress; _oEmployee.username = pEmployee.username; _oEmployee.password = _oEmployee.password; return(commitChanges(context)); } } return(false); }