3 Commits
2 changed files with 99 additions and 7 deletions
@@ -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;
@@ -43,7 +44,7 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
public function findAll(): array
{
$models = [];
$stmt = $this->database->prepare('SELECT "Datum" as workingDay, "Arbeitszeit" as workingTime FROM public."Arbeitszeiten"');
$stmt = $this->database->prepare('select "Datum" as workingDay, "Arbeitszeit" as workingTime from public."Arbeitszeiten"');
$stmt->execute();
while ($row = $stmt->fetch()) {
$model = new WorkingHours();
@@ -67,7 +68,7 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
*/
public function findByKey(mixed $primary_key): WorkingHours
{
$stmt = $this->database->prepare('SELECT "Datum" as workingDay, "Arbeitszeit" as workingTime FROM public."Arbeitszeiten" WHERE "Datum" = ?');
$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();
@@ -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();
}
}
}
@@ -6,8 +6,10 @@ use Codeception\Test\Unit;
use DateInterval;
use DateTime;
use Exception;
use InvalidArgumentException;
use PDO;
use PDOStatement;
use stdClass;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryModelAlreadyExists;
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
@@ -58,6 +60,20 @@ class WorkingHoursRepositoryTest extends Unit
$repository->findByKey(self::NEW_DATE);
}
/**
* @throws Exception
*/
public function testInsertWithInvalidModel(): void
{
$repository = new WorkingHoursRepository($this->pdoObject);
$model = $this->make(stdClass::class, [
'workingDay' => new DateTime(self::NEW_DATE),
'workingTime' => new DateInterval(self::NEW_INTERVAL),
]);
$this->expectException(InvalidArgumentException::class);
$repository->insert($model);
}
/**
* @throws RepositoryRecordNotFoundException
* @throws Exception
@@ -91,6 +107,21 @@ class WorkingHoursRepositoryTest extends Unit
$repository->insert($model);
}
/**
* @throws RepositoryRecordNotFoundException
* @throws Exception
*/
public function testDeleteWithInvalidModel(): void
{
$repository = new WorkingHoursRepository($this->pdoObject);
$model = $this->make(stdClass::class, [
'workingDay' => new DateTime(self::EXISTING_DATE),
'workingTime' => new DateInterval(self::EXISTING_INTERVAL),
]);
$this->expectException(InvalidArgumentException::class);
$repository->delete($model);
}
/**
* @throws RepositoryRecordNotFoundException
* @throws Exception
@@ -118,6 +149,22 @@ class WorkingHoursRepositoryTest extends Unit
$repository->delete($model);
}
/**
* @throws RepositoryRecordNotFoundException
* @throws Exception
*/
public function testUpdateWithInvalidModel(): void
{
$repository = new WorkingHoursRepository($this->pdoObject);
$model = $this->make(stdClass::class, [
'workingDay' => new DateTime(self::EXISTING_DATE),
'workingTime' => new DateInterval(self::NEW_INTERVAL),
]);
$this->expectException(InvalidArgumentException::class);
$repository->update($model);
}
/**
* @throws RepositoryRecordNotFoundException
* @throws Exception