api-erstellen #1

Merged
TorstenHettstedt merged 52 commits from api-erstellen into master 2021-04-07 13:00:46 +02:00
Showing only changes of commit d0799eaae2 - Show all commits
@@ -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();
}
}
}