ameliabooking
/
src
/
Infrastructure
/
Repository
/
Booking
/
Appointment
/
CustomerBookingExtraRepository.php
AppointmentRepository.php
1 year ago
CustomerBookingExtraRepository.php
2 years ago
CustomerBookingRepository.php
1 year ago
CustomerBookingExtraRepository.php
115 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Booking\Appointment; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBookingExtra; |
| 6 | use AmeliaBooking\Domain\Factory\Booking\Appointment\CustomerBookingExtraFactory; |
| 7 | use AmeliaBooking\Domain\Repository\Booking\Appointment\CustomerBookingExtraRepositoryInterface; |
| 8 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 9 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 10 | |
| 11 | /** |
| 12 | * Class CustomerBookingExtraRepository |
| 13 | * |
| 14 | * @package AmeliaBooking\Infrastructure\Repository\Booking\Appointment |
| 15 | */ |
| 16 | class CustomerBookingExtraRepository extends AbstractRepository implements CustomerBookingExtraRepositoryInterface |
| 17 | { |
| 18 | |
| 19 | const FACTORY = CustomerBookingExtraFactory::class; |
| 20 | |
| 21 | /** |
| 22 | * @param CustomerBookingExtra $entity |
| 23 | * |
| 24 | * @return mixed |
| 25 | * @throws QueryExecutionException |
| 26 | */ |
| 27 | public function add($entity) |
| 28 | { |
| 29 | $data = $entity->toArray(); |
| 30 | |
| 31 | $params = [ |
| 32 | ':customerBookingId' => $data['customerBookingId'], |
| 33 | ':extraId' => $data['extraId'], |
| 34 | ':quantity' => $data['quantity'], |
| 35 | ':price' => $data['price'], |
| 36 | ':tax' => !empty($data['tax']) ? json_encode($data['tax']) : null, |
| 37 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 38 | ]; |
| 39 | |
| 40 | try { |
| 41 | $statement = $this->connection->prepare( |
| 42 | "INSERT INTO {$this->table} |
| 43 | ( |
| 44 | `customerBookingId`, |
| 45 | `extraId`, |
| 46 | `quantity`, |
| 47 | `aggregatedPrice`, |
| 48 | `price`, |
| 49 | `tax` |
| 50 | ) |
| 51 | VALUES ( |
| 52 | :customerBookingId, |
| 53 | :extraId, |
| 54 | :quantity, |
| 55 | :aggregatedPrice, |
| 56 | :price, |
| 57 | :tax |
| 58 | )" |
| 59 | ); |
| 60 | |
| 61 | $res = $statement->execute($params); |
| 62 | |
| 63 | if (!$res) { |
| 64 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 65 | } |
| 66 | |
| 67 | return $this->connection->lastInsertId(); |
| 68 | } catch (\Exception $e) { |
| 69 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param int $id |
| 75 | * @param CustomerBookingExtra $entity |
| 76 | * |
| 77 | * @return mixed |
| 78 | * @throws QueryExecutionException |
| 79 | */ |
| 80 | public function update($id, $entity) |
| 81 | { |
| 82 | $data = $entity->toArray(); |
| 83 | |
| 84 | $params = [ |
| 85 | ':id' => $id, |
| 86 | ':customerBookingId' => $data['customerBookingId'], |
| 87 | ':extraId' => $data['extraId'], |
| 88 | ':quantity' => $data['quantity'], |
| 89 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 90 | ]; |
| 91 | |
| 92 | try { |
| 93 | $statement = $this->connection->prepare( |
| 94 | "UPDATE {$this->table} |
| 95 | SET |
| 96 | `customerBookingId` = :customerBookingId, |
| 97 | `extraId` = :extraId, |
| 98 | `aggregatedPrice` = :aggregatedPrice, |
| 99 | `quantity` = :quantity |
| 100 | WHERE id = :id" |
| 101 | ); |
| 102 | |
| 103 | $res = $statement->execute($params); |
| 104 | |
| 105 | if (!$res) { |
| 106 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 107 | } |
| 108 | |
| 109 | return $res; |
| 110 | } catch (\Exception $e) { |
| 111 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 |