ProviderLocationRepository.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Location; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Location\ProviderLocation; |
| 6 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 7 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 8 | |
| 9 | /** |
| 10 | * Class ProviderLocationRepository |
| 11 | * |
| 12 | * @package AmeliaBooking\Infrastructure\Repository\Location |
| 13 | */ |
| 14 | class ProviderLocationRepository extends AbstractRepository |
| 15 | { |
| 16 | public const FACTORY = ProviderLocationRepository::class; |
| 17 | |
| 18 | /** |
| 19 | * @param ProviderLocation $entity |
| 20 | * |
| 21 | * @return int |
| 22 | * @throws QueryExecutionException |
| 23 | */ |
| 24 | public function add($entity) |
| 25 | { |
| 26 | $data = $entity->toArray(); |
| 27 | |
| 28 | $params = [ |
| 29 | ':userId' => $data['userId'], |
| 30 | ':locationId' => $data['locationId'], |
| 31 | ]; |
| 32 | |
| 33 | try { |
| 34 | $statement = $this->connection->prepare( |
| 35 | "INSERT INTO {$this->table} |
| 36 | (`userId`, `locationId`) |
| 37 | VALUES |
| 38 | (:userId, :locationId)" |
| 39 | ); |
| 40 | |
| 41 | $statement->execute($params); |
| 42 | } catch (\Exception $e) { |
| 43 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 44 | } |
| 45 | |
| 46 | return $this->connection->lastInsertId(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param int $userId |
| 51 | * |
| 52 | * @return bool |
| 53 | * @throws QueryExecutionException |
| 54 | */ |
| 55 | public function delete($userId) |
| 56 | { |
| 57 | try { |
| 58 | $statement = $this->connection->prepare("DELETE FROM {$this->table} WHERE userId = :userId"); |
| 59 | $statement->bindParam(':userId', $userId); |
| 60 | $statement->execute(); |
| 61 | return true; |
| 62 | } catch (\Exception $e) { |
| 63 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param ProviderLocation $entity |
| 69 | * |
| 70 | * @return void |
| 71 | * @throws QueryExecutionException |
| 72 | */ |
| 73 | public function update($entity) |
| 74 | { |
| 75 | $data = $entity->toArray(); |
| 76 | |
| 77 | $params = [ |
| 78 | ':userId' => $data['userId'], |
| 79 | ':locationId' => $data['locationId'], |
| 80 | ]; |
| 81 | |
| 82 | try { |
| 83 | $statement = $this->connection->prepare( |
| 84 | "UPDATE {$this->table} |
| 85 | SET `locationId` = :locationId |
| 86 | WHERE userId = :userId" |
| 87 | ); |
| 88 | |
| 89 | $statement->execute($params); |
| 90 | } catch (\Exception $e) { |
| 91 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 |