From d0799eaae20acf42c01bcf671ba108ca3a50c169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Thu, 1 Apr 2021 14:00:41 +0200 Subject: [PATCH] =?UTF-8?q?Die=20Tests=20f=C3=BCr=20das=20Schreiben=20der?= =?UTF-8?q?=20Daten=20sind=20erfolgreich.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/WorkingHoursRepository.php | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/api/src/Repositories/WorkingHoursRepository.php b/api/src/Repositories/WorkingHoursRepository.php index 72b07de..a32ac76 100644 --- a/api/src/Repositories/WorkingHoursRepository.php +++ b/api/src/Repositories/WorkingHoursRepository.php @@ -7,6 +7,7 @@ namespace TorstenHettstedt\TimekeepingApi\Repositories; use DateInterval; use DateTime; use Exception; +use InvalidArgumentException; use PDO; use PDOException; use TorstenHettstedt\TimekeepingApi\Models\ModelInterface; @@ -83,25 +84,69 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri /** * @param WorkingHours|ModelInterface $model + * + * @throws RepositoryModelAlreadyExists */ 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 + * + * @throws RepositoryRecordNotFoundException */ 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 + * + * @throws RepositoryRecordNotFoundException */ 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(); + } } } \ No newline at end of file