[add} Saving new person

This commit is contained in:
Jose Pabl Domingo Aramburo Sanchez 2018-08-05 23:44:46 -06:00
parent 692f52b533
commit 304e3045c7
3 changed files with 95 additions and 5 deletions

View File

@ -14,6 +14,9 @@ class EmployeeApplication{
$this->databaseSelectQueryErrorMessage = 'There was an error inserting the record.'; $this->databaseSelectQueryErrorMessage = 'There was an error inserting the record.';
} }
/**
* @return array
*/
function listEmployeeTypes(){ function listEmployeeTypes(){
$stmt = $this->pdo->prepare("SELECT id, name FROM employeeType WHERE status = 'ACTIVE'"); $stmt = $this->pdo->prepare("SELECT id, name FROM employeeType WHERE status = 'ACTIVE'");
$stmt->execute(); $stmt->execute();
@ -27,5 +30,72 @@ class EmployeeApplication{
return $results; return $results;
} }
function saveNewPerson($firstName, $middleName, $lastName, $birthDate, $email, $phone){
$this->asserts->firstName($firstName);
$this->asserts->middleName($middleName);
$this->asserts->birthDate($birthDate);
$this->asserts->email($email);
$this->asserts->phone($phone);
try {
$stmt = $this->pdo->prepare("INSERT INTO persons (firstName, middleName, lastName, birthDate, email, phone)
VALUES (:firstName, :middleName, :lastName, :birthDate, :email, :phone)");
$this->pdo->beginTransaction();
$stmt->execute(array(':firstName' => $firstName, ':middleName' => $middleName, ':lastName' => $lastName,
':birthDate' => $birthDate, ':email' => $email, ':phone' => $phone));
$id = $this->pdo->lastInsertId();
$this->pdo->commit();
return $id;
$stmt = null;
} catch( PDOExecption $e ) {
$this->pdo->rollback();
throw new Exception('There was an error while trying to save a new person.');
$this->logger->warning("There was an error in the EmployeeApplication->saveNewPerson caused by: $e ");
}
}
function saveNewEmployee($requestData){
// Getting and validating the data
$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);
$employeeType = $requestData{'employeeType'};
$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);
$idNewperson = $this->saveNewPerson($securedFirstName, $securedMiddleName, $securedLastName,
$birthDate, $securedEmail, $phone);
return $idNewperson;
}
} }
?> ?>

View File

@ -20,9 +20,9 @@ $app->get('/api/session', function (Request $request, Response $response, array
}); });
$app->post('/api/session/login', function ($request, $response) { $app->post('/api/session/login', function ($request, $response) {
$RequestData = $request->getParsedBody(); $requestData = $request->getParsedBody();
$data = $this->sessionApplication->newSession($RequestData['userName'], $RequestData['password']); $data = $this->sessionApplication->newSession($requestData['userName'], $requestData['password']);
return $response->withStatus(200) return $response->withStatus(200)
->withHeader('Content-Type', 'application/json') ->withHeader('Content-Type', 'application/json')
@ -40,3 +40,11 @@ $app->get('/api/employee/types', function (Request $request, Response $response,
->withHeader('Content-Type', 'application/json') ->withHeader('Content-Type', 'application/json')
->write(json_encode($this->employeeApplication->listEmployeeTypes())); ->write(json_encode($this->employeeApplication->listEmployeeTypes()));
}); });
$app->post('/api/employee', function ($request, $response) {
$requestData = $request->getParsedBody();
return $response->withStatus(200)
->withHeader('Content-Type', 'application/json')
->write(json_encode($this->employeeApplication->saveNewEmployee($requestData)));
});

View File

@ -16,8 +16,7 @@ CREATE TABLE IF NOT EXISTS `persons` (
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment 'The date on which the registry was created', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment 'The date on which the registry was created',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment 'The date of the last time the row was modified', `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment 'The date of the last time the row was modified',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE (`phone`), UNIQUE (`phone`)
UNIQUE (`firstName`,`middleName`,`lastName`,`birthDate`)
); );
INSERT INTO persons (firstName, middleName, lastName, birthDate, email, phone) INSERT INTO persons (firstName, middleName, lastName, birthDate, email, phone)
@ -60,3 +59,16 @@ CREATE TABLE IF NOT EXISTS `employeeType` (
INSERT INTO employeeType (name) VALUES ('Chofer'), INSERT INTO employeeType (name) VALUES ('Chofer'),
('Cargador'), ('Cargador'),
('Auxiliar'); ('Auxiliar');
DROP TABLE IF EXISTS employees;
CREATE TABLE IF NOT EXISTS `employees` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`idEmployeeType` INT UNSIGNED NOT NULL comment 'Defines the rol within the company',
`numero` VARCHAR(100) NOT NULL comment 'A code to reference the employee',
`contractType` ENUM('INTERNO', 'EXTERNO') NOT NULL comment 'The type of contract',
`status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment 'The date on which the registry was created',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment 'The date of the last time the row was modified',
PRIMARY KEY (`id`),
UNIQUE (`name`)
);