api-erstellen #1
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Repositories;
|
||||
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
|
||||
|
||||
/**
|
||||
* @implements RepositoryReaderInterface<WorkingHours>
|
||||
* @implements RepositoryWriterInterface<WorkingHours>
|
||||
*/
|
||||
class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWriterInterface
|
||||
{
|
||||
|
||||
protected PDO $database;
|
||||
|
||||
/**
|
||||
* WorkingHoursRepository constructor.
|
||||
*
|
||||
* @param PDO $database
|
||||
*/
|
||||
public function __construct(PDO $database)
|
||||
{
|
||||
$this->database = $database;
|
||||
$this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gibt alle vorhandenen Einträge zurück
|
||||
*
|
||||
* @return WorkingHours[]
|
||||
* @throws Exception
|
||||
* @throws PDOException
|
||||
*/
|
||||
public function findAll(): array
|
||||
{
|
||||
$models = [];
|
||||
$stmt = $this->database->prepare('SELECT "Datum" as workingDay, "Arbeitszeit" as workingTime FROM public."Arbeitszeiten"');
|
||||
$stmt->execute();
|
||||
while ($row = $stmt->fetch()) {
|
||||
$model = new WorkingHours();
|
||||
$model
|
||||
->setWorkingDay(new DateTime($row['workingday']))
|
||||
->setWorkingTime(new DateInterval('P0000-00-00T'.$row['workingtime']));
|
||||
$models[] = $model;
|
||||
}
|
||||
return $models;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Eintrag zurück, der durch die ID
|
||||
*
|
||||
* @param mixed $primary_key
|
||||
*
|
||||
* @return WorkingHours
|
||||
* @throws RepositoryRecordNotFoundException
|
||||
* @throws Exception
|
||||
* @throws PDOException
|
||||
*/
|
||||
public function findByKey(mixed $primary_key): WorkingHours
|
||||
{
|
||||
$stmt = $this->database->prepare('SELECT "Datum" as workingDay, "Arbeitszeit" as workingTime FROM public."Arbeitszeiten" WHERE "Datum" = ?');
|
||||
$stmt->bindParam(1, $primary_key);
|
||||
$stmt->execute();
|
||||
$row = $stmt->fetch();
|
||||
if ($row === false) {
|
||||
throw new RepositoryRecordNotFoundException();
|
||||
}
|
||||
$model = new WorkingHours();
|
||||
$model
|
||||
->setWorkingDay(new DateTime($row['workingday']))
|
||||
->setWorkingTime(new DateInterval('P0000-00-00T'.$row['workingtime']));
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingHours|ModelInterface $model
|
||||
*/
|
||||
public function insert(mixed $model): void
|
||||
{
|
||||
// TODO: Implement insert() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingHours|ModelInterface $model
|
||||
*/
|
||||
public function update(mixed $model): void
|
||||
{
|
||||
// TODO: Implement update() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingHours|ModelInterface $model
|
||||
*/
|
||||
public function delete(mixed $model): void
|
||||
{
|
||||
// TODO: Implement delete() method.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user