CategoryRepository.php
2 years ago
ExtraRepository.php
5 years ago
PackageCustomerRepository.php
1 year ago
PackageCustomerServiceRepository.php
1 year ago
PackageRepository.php
2 years ago
PackageServiceLocationRepository.php
5 years ago
PackageServiceProviderRepository.php
5 years ago
PackageServiceRepository.php
2 years ago
ProviderServiceRepository.php
1 year ago
ResourceEntitiesRepository.php
2 years ago
ResourceRepository.php
2 years ago
ServiceRepository.php
2 years ago
ExtraRepository.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Bookable\Service; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Bookable\Service\Extra; |
| 6 | use AmeliaBooking\Domain\Factory\Bookable\Service\ExtraFactory; |
| 7 | use AmeliaBooking\Domain\Repository\Bookable\Service\ExtraRepositoryInterface; |
| 8 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 9 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 10 | |
| 11 | /** |
| 12 | * Class ExtraRepository |
| 13 | * |
| 14 | * @package AmeliaBooking\Infrastructure\Repository\Bookable\Service |
| 15 | */ |
| 16 | class ExtraRepository extends AbstractRepository implements ExtraRepositoryInterface |
| 17 | { |
| 18 | |
| 19 | const FACTORY = ExtraFactory::class; |
| 20 | |
| 21 | /** |
| 22 | * @param Extra $entity |
| 23 | * |
| 24 | * @return mixed |
| 25 | * @throws QueryExecutionException |
| 26 | */ |
| 27 | public function add($entity) |
| 28 | { |
| 29 | $data = $entity->toArray(); |
| 30 | |
| 31 | $params = [ |
| 32 | ':name' => $data['name'], |
| 33 | ':description' => $data['description'], |
| 34 | ':price' => $data['price'], |
| 35 | ':maxQuantity' => $data['maxQuantity'], |
| 36 | ':duration' => $data['duration'], |
| 37 | ':serviceId' => $data['serviceId'], |
| 38 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 39 | ':position' => $data['position'], |
| 40 | ':translations' => $data['translations'], |
| 41 | ]; |
| 42 | |
| 43 | try { |
| 44 | $statement = $this->connection->prepare( |
| 45 | "INSERT INTO |
| 46 | {$this->table} |
| 47 | ( |
| 48 | `name`, |
| 49 | `description`, |
| 50 | `price`, |
| 51 | `maxQuantity`, |
| 52 | `duration`, |
| 53 | `serviceId`, |
| 54 | `aggregatedPrice`, |
| 55 | `position`, |
| 56 | `translations` |
| 57 | ) VALUES ( |
| 58 | :name, |
| 59 | :description, |
| 60 | :price, |
| 61 | :maxQuantity, |
| 62 | :duration, |
| 63 | :serviceId, |
| 64 | :aggregatedPrice, |
| 65 | :position, |
| 66 | :translations |
| 67 | )" |
| 68 | ); |
| 69 | |
| 70 | $result = $statement->execute($params); |
| 71 | |
| 72 | if (!$result) { |
| 73 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 74 | } |
| 75 | |
| 76 | return $this->connection->lastInsertId(); |
| 77 | } catch (\Exception $e) { |
| 78 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param int $id |
| 84 | * @param Extra $entity |
| 85 | * |
| 86 | * @return mixed |
| 87 | * @throws QueryExecutionException |
| 88 | */ |
| 89 | public function update($id, $entity) |
| 90 | { |
| 91 | $data = $entity->toArray(); |
| 92 | |
| 93 | $params = [ |
| 94 | ':name' => $data['name'], |
| 95 | ':description' => $data['description'], |
| 96 | ':price' => $data['price'], |
| 97 | ':maxQuantity' => $data['maxQuantity'], |
| 98 | ':duration' => $data['duration'], |
| 99 | ':serviceId' => $data['serviceId'], |
| 100 | ':aggregatedPrice' => $data['aggregatedPrice'] === null ? |
| 101 | $data['aggregatedPrice'] : ((int)$data['aggregatedPrice']), |
| 102 | ':position' => $data['position'], |
| 103 | ':translations' => $data['translations'], |
| 104 | ':id' => $id |
| 105 | ]; |
| 106 | |
| 107 | try { |
| 108 | $statement = $this->connection->prepare( |
| 109 | "UPDATE {$this->table} |
| 110 | SET |
| 111 | `name` = :name, |
| 112 | `description` = :description, |
| 113 | `price` = :price, |
| 114 | `maxQuantity` = :maxQuantity, |
| 115 | `duration` = :duration, |
| 116 | `serviceId` = :serviceId, |
| 117 | `aggregatedPrice` = :aggregatedPrice, |
| 118 | `position` = :position, |
| 119 | `translations` = :translations |
| 120 | WHERE id = :id" |
| 121 | ); |
| 122 | |
| 123 | $result = $statement->execute($params); |
| 124 | |
| 125 | if (!$result) { |
| 126 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 127 | } |
| 128 | |
| 129 | return $result; |
| 130 | } catch (\Exception $e) { |
| 131 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 |