From 63a7186464175c2e35a2ef40ee4c8340193d1412 Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Mon, 6 Aug 2018 03:15:28 -0600 Subject: [PATCH 1/3] [add] Update to the person table --- .../src/application/EmployeeApplication.php | 121 +++++++++++++++++- api-payroll/src/routes.php | 9 ++ 2 files changed, 127 insertions(+), 3 deletions(-) diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index 206e2b3..6ad0e03 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -1,6 +1,8 @@ pdo->prepare("SELECT + COALESCE((SELECT + idPerson + FROM + employees + WHERE + id = :idEmployee), + 0) AS id"); + + $stmt->execute(array(':idEmployee' => $idEmployee)); + $results = $stmt->fetchAll(); + if(!$results){ + exit($this->databaseSelectQueryErrorMessage); + } + $stmt = null; + + return $results[0]['id']; + } + /** * @param $code * @return mixed @@ -241,7 +267,96 @@ class EmployeeApplication{ ); return $response; + } + /** + * @param $idPerson integer + * @param $firstName binary + * @param $middleName binary + * @param $lastName binary + * @param $birthDate date + * @param $email binary + * @param $phone string + */ + function updatePerson($idPerson, $firstName, $middleName, $lastName, $birthDate, $email, $phone){ + try { + $stmt = $this->pdo->prepare("UPDATE persons + SET + firstName = :firstName, + middleName = :middleName, + lastName = :lastName, + birthDate = :birthDate, + email = :email, + phone = :phone + WHERE + id = :idPerson"); + $this->pdo->beginTransaction(); + $stmt->execute(array(':firstName' => $firstName, ':middleName' => $middleName, ':lastName' => $lastName, + ':birthDate' => $birthDate, ':email' => $email, ':phone' => $phone, ':idPerson' => $idPerson)); + $this->pdo->commit(); + + $stmt = null; + } catch( PDOExecption $e ) { + $this->pdo->rollback(); + } + } + + /** + * @param $requestData object + * @return array + */ + function updateEmployee($requestData){ + // Getting and validating the data + $idEmployee = $requestData['idEmployee']; + $idPerson = $this->getIdPersonByIdEmployee($idEmployee); + + $firstName = $requestData['firstName']; + $this->asserts->firstName($firstName); + + $middleName = $requestData['middleName']; + $this->asserts->middleName($middleName); + + $lastName = isset($requestData['lastName']) ? $requestData['lastName'] : null; + + $birthDate = $requestData['birthDate']; + $this->asserts->birthDate($birthDate); + + $email = $requestData['email']; + $this->asserts->email($email); + + $phone = $requestData['phone']; + $this->asserts->phone($phone); + + $idEmployeeType = $requestData{'idEmployeeType'}; + $contractType = $requestData{'contractType'}; + + // Encrypting the sensitive data + $securedFirstName = $this->cryptographyService->encryptString($firstName); + $securedMiddleName = $this->cryptographyService->encryptString($middleName); + + if (isset($lastName)) { + $securedLastName = $this->cryptographyService->encryptString($lastName); + } else { + $securedLastName = null; + } + + $securedEmail = $this->cryptographyService->encryptString($email); + + // Update process + $this->updatePerson($idPerson, $securedFirstName, $securedMiddleName, $securedLastName, + $birthDate, $securedEmail, $phone); + + $response = array( + "fullName" => "$firstName $middleName $lastName", + "idEmployee" => $idEmployee, + "email" => $email, + "phone" => $phone, + "birthDate" => $birthDate, + "idEmployeeType" => $idEmployeeType, + "contractType" => $contractType + ); + + return $response; } } ?> \ No newline at end of file diff --git a/api-payroll/src/routes.php b/api-payroll/src/routes.php index da1eda3..820efe7 100644 --- a/api-payroll/src/routes.php +++ b/api-payroll/src/routes.php @@ -49,6 +49,15 @@ $app->post('/api/employee', function ($request, $response) { ->write(json_encode($this->employeeApplication->saveNewEmployee($requestData))); }); +$app->put('/api/employee', function ($request, $response) { + $requestData = $request->getParsedBody(); + + return $response->withStatus(200) + ->withHeader('Content-Type', 'application/json') + ->write(json_encode($this->employeeApplication->updateEmployee($requestData))); +}); + + $app->get('/api/employee/type/{code}', function (Request $request, Response $response, array $args) { $code = $args['code']; From 57ee1fbd72fb71ec6917e58c25166f0af3a0fc2f Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Mon, 6 Aug 2018 03:27:22 -0600 Subject: [PATCH 2/3] [add] Updating the employee table --- .../src/application/EmployeeApplication.php | 31 ++++++++++++++++++- api-payroll/src/routes.php | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index 6ad0e03..7ff7d12 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -301,14 +301,41 @@ class EmployeeApplication{ } } + /** + * @param $idEmployee integer + * @param $code string + * @param $idEmployeeType integer + * @param $contractType string + */ + function updateEmployee($idEmployee, $code, $idEmployeeType, $contractType){ + try { + $stmt = $this->pdo->prepare("UPDATE employees + SET + idEmployeeType = :idEmployeeType, + code = :code, + contractType = :contractType + WHERE + id = :idEmployee"); + $this->pdo->beginTransaction(); + $stmt->execute(array(':idEmployeeType' => $idEmployeeType, ':code' => $code, ':contractType' => $contractType, + ':idEmployee' => $idEmployee)); + $this->pdo->commit(); + + $stmt = null; + } catch( PDOExecption $e ) { + $this->pdo->rollback(); + } + } + /** * @param $requestData object * @return array */ - function updateEmployee($requestData){ + function updateEmployeeData($requestData){ // Getting and validating the data $idEmployee = $requestData['idEmployee']; $idPerson = $this->getIdPersonByIdEmployee($idEmployee); + $code = $requestData['code']; $firstName = $requestData['firstName']; $this->asserts->firstName($firstName); @@ -346,6 +373,8 @@ class EmployeeApplication{ $this->updatePerson($idPerson, $securedFirstName, $securedMiddleName, $securedLastName, $birthDate, $securedEmail, $phone); + $this->updateEmployee($idEmployee, $code, $idEmployeeType, $contractType); + $response = array( "fullName" => "$firstName $middleName $lastName", "idEmployee" => $idEmployee, diff --git a/api-payroll/src/routes.php b/api-payroll/src/routes.php index 820efe7..def91f3 100644 --- a/api-payroll/src/routes.php +++ b/api-payroll/src/routes.php @@ -54,7 +54,7 @@ $app->put('/api/employee', function ($request, $response) { return $response->withStatus(200) ->withHeader('Content-Type', 'application/json') - ->write(json_encode($this->employeeApplication->updateEmployee($requestData))); + ->write(json_encode($this->employeeApplication->updateEmployeeData($requestData))); }); From ba307555f08f0a2807d799e4e9b704e4400a436a Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Mon, 6 Aug 2018 03:33:18 -0600 Subject: [PATCH 3/3] [add] Delete employee --- .../src/application/EmployeeApplication.php | 17 +++++++++++++++++ api-payroll/src/routes.php | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index 7ff7d12..6cf237a 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -387,5 +387,22 @@ class EmployeeApplication{ return $response; } + + function disableEmployeeRecord($idEmployee){ + try { + $stmt = $this->pdo->prepare("UPDATE employees + SET + status = 'INACTIVE' + WHERE + id = :idEmployee"); + $this->pdo->beginTransaction(); + $stmt->execute(array(':idEmployee' => $idEmployee)); + $this->pdo->commit(); + + $stmt = null; + } catch( PDOExecption $e ) { + $this->pdo->rollback(); + } + } } ?> \ No newline at end of file diff --git a/api-payroll/src/routes.php b/api-payroll/src/routes.php index def91f3..7c3337e 100644 --- a/api-payroll/src/routes.php +++ b/api-payroll/src/routes.php @@ -57,6 +57,13 @@ $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']; + + return $response->withStatus(200) + ->withHeader('Content-Type', 'application/json') + ->write(json_encode($this->employeeApplication->disableEmployeeRecord($idEmployee))); +}); $app->get('/api/employee/type/{code}', function (Request $request, Response $response, array $args) { $code = $args['code'];