api-erstellen #1
@@ -7,6 +7,7 @@ namespace TorstenHettstedt\TimekeepingApi\Repositories;
|
|||||||
use DateInterval;
|
use DateInterval;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use InvalidArgumentException;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
|
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
|
||||||
@@ -83,25 +84,69 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param WorkingHours|ModelInterface $model
|
* @param WorkingHours|ModelInterface $model
|
||||||
|
*
|
||||||
|
* @throws RepositoryModelAlreadyExists
|
||||||
*/
|
*/
|
||||||
public function insert(mixed $model): void
|
public function insert(mixed $model): void
|
||||||
{
|
{
|
||||||
// TODO: Implement insert() method.
|
if ($model instanceof WorkingHours) {
|
||||||
|
$workingDay = $model->getWorkingDay()->format('Y-m-d');
|
||||||
|
$workingTime = $model->getWorkingTime()->format('%H:%I:%S');
|
||||||
|
} else {
|
||||||
|
throw new InvalidArgumentException('Es wird ein Modell der Klasse "' . WorkingHours::class . '" verlangt.');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->findByKey($workingDay);
|
||||||
|
throw new RepositoryModelAlreadyExists();
|
||||||
|
} /** @noinspection PhpUnusedLocalVariableInspection */
|
||||||
|
catch (RepositoryRecordNotFoundException $exception) {
|
||||||
|
$stmt = $this->database->prepare('INSERT INTO public."Arbeitszeiten" ("Datum", "Arbeitszeit") values (?, ?) ON CONFLICT DO NOTHING');
|
||||||
|
$stmt->bindParam(1, $workingDay);
|
||||||
|
$stmt->bindParam(2, $workingTime);
|
||||||
|
$stmt->execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param WorkingHours|ModelInterface $model
|
* @param WorkingHours|ModelInterface $model
|
||||||
|
*
|
||||||
|
* @throws RepositoryRecordNotFoundException
|
||||||
*/
|
*/
|
||||||
public function update(mixed $model): void
|
public function update(mixed $model): void
|
||||||
{
|
{
|
||||||
// TODO: Implement update() method.
|
if ($model instanceof WorkingHours) {
|
||||||
|
$workingDay = $model->getWorkingDay()->format('Y-m-d');
|
||||||
|
$workingTime = $model->getWorkingTime()->format('%H:%I:%S');
|
||||||
|
} else {
|
||||||
|
throw new InvalidArgumentException('Es wird ein Modell der Klasse "' . WorkingHours::class . '" verlangt.');
|
||||||
|
}
|
||||||
|
$stmt = $this->database->prepare('UPDATE public."Arbeitszeiten" SET "Arbeitszeit" = ? WHERE "Datum" = ?');
|
||||||
|
$stmt->bindParam(1, $workingTime);
|
||||||
|
$stmt->bindParam(2, $workingDay);
|
||||||
|
$stmt->execute();
|
||||||
|
if ($stmt->rowCount() < 1) {
|
||||||
|
throw new RepositoryRecordNotFoundException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param WorkingHours|ModelInterface $model
|
* @param WorkingHours|ModelInterface $model
|
||||||
|
*
|
||||||
|
* @throws RepositoryRecordNotFoundException
|
||||||
*/
|
*/
|
||||||
public function delete(mixed $model): void
|
public function delete(mixed $model): void
|
||||||
{
|
{
|
||||||
// TODO: Implement delete() method.
|
if ($model instanceof WorkingHours) {
|
||||||
|
$workingDay = $model->getWorkingDay()->format('Y-m-d');
|
||||||
|
} else {
|
||||||
|
throw new InvalidArgumentException('Es wird ein Modell der Klasse "' . WorkingHours::class . '" verlangt.');
|
||||||
|
}
|
||||||
|
$stmt = $this->database->prepare('DELETE FROM public."Arbeitszeiten" WHERE "Datum" = ?');
|
||||||
|
$stmt->bindParam(1, $workingDay);
|
||||||
|
$stmt->execute();
|
||||||
|
if ($stmt->rowCount() < 1) {
|
||||||
|
throw new RepositoryRecordNotFoundException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user