ProviderLocationRepository.php
102 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 | 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 | $res = $statement->execute($params); |
| 42 | if (!$res) { |
| 43 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 44 | } |
| 45 | } catch (\Exception $e) { |
| 46 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 47 | } |
| 48 | |
| 49 | return $this->connection->lastInsertId(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param int $userId |
| 54 | * |
| 55 | * @return bool |
| 56 | * @throws QueryExecutionException |
| 57 | */ |
| 58 | public function delete($userId) |
| 59 | { |
| 60 | try { |
| 61 | $statement = $this->connection->prepare("DELETE FROM {$this->table} WHERE userId = :userId"); |
| 62 | $statement->bindParam(':userId', $userId); |
| 63 | return $statement->execute(); |
| 64 | } catch (\Exception $e) { |
| 65 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param ProviderLocation $entity |
| 71 | * |
| 72 | * @return int |
| 73 | * @throws QueryExecutionException |
| 74 | */ |
| 75 | public function update($entity) |
| 76 | { |
| 77 | $data = $entity->toArray(); |
| 78 | |
| 79 | $params = [ |
| 80 | ':userId' => $data['userId'], |
| 81 | ':locationId' => $data['locationId'], |
| 82 | ]; |
| 83 | |
| 84 | try { |
| 85 | $statement = $this->connection->prepare( |
| 86 | "UPDATE {$this->table} |
| 87 | SET `locationId` = :locationId |
| 88 | WHERE userId = :userId" |
| 89 | ); |
| 90 | |
| 91 | $res = $statement->execute($params); |
| 92 | if (!$res) { |
| 93 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 94 | } |
| 95 | } catch (\Exception $e) { |
| 96 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 97 | } |
| 98 | |
| 99 | return $this->connection->lastInsertId(); |
| 100 | } |
| 101 | } |
| 102 |