[add] Delete emplployee

This commit is contained in:
Jose Pabl Domingo Aramburo Sanchez 2018-08-30 23:06:56 -06:00
parent 07e0fbbfab
commit ec64188737
4 changed files with 56 additions and 4 deletions

View File

@ -104,6 +104,7 @@
<div class="form-group">
<a href="#" class="btn btn-lg btn-success " onclick="updateEmployee();">Update</a>
<a href="#" class="btn btn-lg btn-primary " onclick="loadView(currentView);">Clear</a>
<a href="#" class="btn btn-lg btn-danger " onclick="deleteEmployee();">Delete</a>
</div>
</div>
</div>

View File

@ -134,6 +134,43 @@ function loadEmployeeData(code){
});
}
/**
* Will change the status of an employee to remove them from the
* active employee list
*/
function deleteEmployee(){
let baseUrl = getbaseUrl();
let code = $('#editEmployeeCode').val();
$.ajax({
url: baseUrl + '/api/employee/' + code,
type: 'DELETE',
dataType: 'json',
success:function(data){
$('#modalServerResponseSuccess').modal('show');
document.getElementById('serverResponseSuccess').innerHTML = 'The employee ' + data['firstName'] + ' ' + data['middleName'] + ' ' + data['lastName'] + ' has been deleted.';
},
error:function(x,e) {
let responseText = $.parseJSON(x["responseText"]);
if (x.status==0) {
$('#modalErrorInternetConnection').modal('show');
} else if(x.status==404) {
$('#modalError404').modal('show');
} else if(x.status==500) {
$('#modalServerResponseError').modal('show');
document.getElementById('modalResponseError').innerHTML = responseText['message'];
} else if(e=='parsererror') {
$('#modalErrorParsererror').modal('show');
} else if(e=='timeout'){
$('#modalErrorTimeout').modal('show');
} else {
$('#modalErrorOther').modal('show');
}
},
});
}
function updateEmployee(){
let baseUrl = getbaseUrl();

View File

@ -518,9 +518,19 @@ class EmployeeApplication{
return $response;
}
function disableEmployeeRecord($idEmployee){
/**
* @param $code string
* @return array
* @throws Exception
*/
function disableEmployeeRecord($code){
$this->asserts->isNotEmpty($code, "The code can't be empty.");
$idEmployee = $this->getIdEmployeeByCode($code);
$this->asserts->higherThanZero($idEmployee, "idEmployee must be higher than 0");
$employeeData = $this->proxyGetEmployeeDataById($idEmployee);
try {
$stmt = $this->pdo->prepare("UPDATE employees
SET
@ -532,8 +542,12 @@ class EmployeeApplication{
$this->pdo->commit();
$stmt = null;
return $employeeData;
} catch( PDOExecption $e ) {
$this->pdo->rollback();
throw new Exception("The employee you tried to delete could not be found.");
}
}

View File

@ -65,12 +65,12 @@ $app->put('/api/employee', function ($request, $response) {
->write(json_encode($this->employeeApplication->updateEmployeeData($requestData)));
});
$app->DELETE('/api/employee/{idEmployee}', function (Request $request, Response $response, array $args) {
$idEmployee = $args['idEmployee'];
$app->DELETE('/api/employee/{code}', function (Request $request, Response $response, array $args) {
$code = $args['code'];
return $response->withStatus(200)
->withHeader('Content-Type', 'application/json')
->write(json_encode($this->employeeApplication->disableEmployeeRecord($idEmployee)));
->write(json_encode($this->employeeApplication->disableEmployeeRecord($code)));
});
$app->get('/api/employee/type/{code}', function (Request $request, Response $response, array $args) {