Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Phoenix Hospitality Management System
- * Phoenix REST API - API Department Repository file
- * Written in PHP using the Slim micro-framework
- *
- * @author Troy L. Marker
- * @version 1.0.0
- * @since 0.5.0
- * @copyright Copyright (c) 2020-2021 By Troy Marker Enterprises
- *
- * Declare name space and Import needed functions/classes
- */
- namespace App\Domain\Repository;
- use PDO;
- /**
- * This class provides a repository to the departments Database table
- */
- class Department {
- protected $connection;
- /**
- * Class constructor
- * @param PDO $connection - The database connection
- */
- public function __construct(PDO $connection) {
- $this->connection = $connection;
- }
- /**
- * This method inserts a new department into the database
- * @param array $Params - Method Parameters
- * @return array $AddRetval - Method Return value
- */
- public function add(array $Params): string {
- $SQLVars = ['department' => $Params['department']];
- $SQLStatement = "SELECT id FROM " . strtolower($Params['db']) . "_departments WHERE department=:department";
- $SQLResult = $this->connection->prepare($SQLStatement);
- $SQLResult->execute($SQLVars);
- if ($SQLResult->rowCount() > 0) {
- return false;
- }
- $SQLStatement = "INSERT INTO " . strtolower($Params['db']) . "_departments SET department=:department;";
- $this->connection->prepare($SQLStatement)->execute($SQLVars);
- return $Params['department'];
- }
- /**
- * This method returns a list of departments
- * @param array $Params - Method Parameters
- * @return array $ListsRetval - Method Return value
- */
- public function lists(array $Params): array {
- if (isset($Params['id'])) {
- $SQLVars = ['id' => $Params['id']];
- $SQLStatement = "SELECT department FROM " . strtolower($Params['db']) . "_departments WHERE id=:id";
- $SQLResult = $this->connection->prepare($SQLStatement);
- $SQLResult->execute($SQLVars);
- return $SQLResult->fetch(PDO::FETCH_ASSOC);
- }
- $SQLStatement = "SELECT * FROM " . strtolower($Params['db']) . "_departments ORDER BY id";
- $SQLResult = $this->connection->prepare($SQLStatement);
- $SQLResult->execute();
- return $SQLResult->fetchAll(PDO::FETCH_ASSOC);
- }
- /**
- * This method will remove a department from the database
- * @param array $Params - Method parameters
- * @return string $RemoveRetval - The name of the department removed.
- */
- public function remove(array $Params): string {
- $SQLVars = ['id' => $Params['id']];
- $SQLStatement = "SELECT department FROM " . strtolower($Params['db']) . "_departments WHERE id=:id";
- $SQLResult = $this->connection->prepare($SQLStatement);
- $SQLResult->execute($SQLVars);
- $SQLReturn = $SQLResult->fetch(PDO::FETCH_ASSOC);
- $SQLStatement = "DELETE FROM " . strtolower($Params['db']) . "_departments WHERE id=:id";
- $SQLResult = $this->connection->prepare($SQLStatement);
- $SQLResult->execute($SQLVars);
- return $SQLReturn['department'];
- }
- /**
- * The method update all fields in a department record
- * @param array $Params - Method parameters
- * @return array $ReplaceRetval - The method results
- */
- public function replace(array $Params): array {
- $SQLVars = ['id' => $Params['id'], 'department' => $Params['department']];
- $SQLStatement = "UPDATE " . strtolower($Params['db']) . "_departments SET department=:department WHERE id=:id";
- $SQLResult = $this->connection->prepare($SQLStatement);
- $SQLResult->execute($SQLVars);
- return $this->lists($Params);
- }
- /**
- * This method will change the name of a department in the database
- * @param array $Params - Method parameters
- * @return array - Call success status with message
- */
- public function update(array $Params): array {
- $SQLVars = ['id' => $Params['id'], 'department' => $Params['department']];
- $SQLStatement = "UPDATE " . strtolower($Params['db']) . "_departments SET department=:department WHERE id=:id";
- $SQLResult = $this->connection->prepare($SQLStatement);
- $SQLResult->execute($SQLVars);
- return $this->lists($Params);
- }
- /**
- * This method will check if a department is in the database
- * @param array $Params - Method parameters
- * @return boolean Method result
- */
- public function check(array $Params): bool {
- $SQLVars = ['department' => $Params['department']];
- $SQLStatement = "SELECT * FROM " . strtolower($Params['db']) . "_departments WHERE department=:department";
- $SQLResult = $this->connection->prepare($SQLStatement);
- $SQLResult->execute($SQLVars);
- if ($SQLResult->rowCount() != 0) {
- return true;
- } else {
- return false;
- }
- }
- }
RAW Paste Data