ameliabooking
/
src
/
Infrastructure
/
Repository
/
Booking
/
Appointment
/
CustomerBookingExtraRepository.php
AppointmentRepository.php
2 weeks ago
CustomerBookingExtraRepository.php
3 months ago
CustomerBookingRepository.php
2 weeks ago
CustomerBookingExtraRepository.php
106 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 | public const FACTORY = CustomerBookingExtraFactory::class; |
| 19 | |
| 20 | /** |
| 21 | * @param CustomerBookingExtra $entity |
| 22 | * |
| 23 | * @return mixed |
| 24 | * @throws QueryExecutionException |
| 25 | */ |
| 26 | public function add($entity) |
| 27 | { |
| 28 | $data = $entity->toArray(); |
| 29 | |
| 30 | $params = [ |
| 31 | ':customerBookingId' => $data['customerBookingId'], |
| 32 | ':extraId' => $data['extraId'], |
| 33 | ':quantity' => $data['quantity'], |
| 34 | ':price' => $data['price'], |
| 35 | ':tax' => !empty($data['tax']) ? json_encode($data['tax']) : null, |
| 36 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 37 | ]; |
| 38 | |
| 39 | try { |
| 40 | $statement = $this->connection->prepare( |
| 41 | "INSERT INTO {$this->table} |
| 42 | ( |
| 43 | `customerBookingId`, |
| 44 | `extraId`, |
| 45 | `quantity`, |
| 46 | `aggregatedPrice`, |
| 47 | `price`, |
| 48 | `tax` |
| 49 | ) |
| 50 | VALUES ( |
| 51 | :customerBookingId, |
| 52 | :extraId, |
| 53 | :quantity, |
| 54 | :aggregatedPrice, |
| 55 | :price, |
| 56 | :tax |
| 57 | )" |
| 58 | ); |
| 59 | |
| 60 | $statement->execute($params); |
| 61 | |
| 62 | return $this->connection->lastInsertId(); |
| 63 | } catch (\Exception $e) { |
| 64 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param int $id |
| 70 | * @param CustomerBookingExtra $entity |
| 71 | * |
| 72 | * @return mixed |
| 73 | * @throws QueryExecutionException |
| 74 | */ |
| 75 | public function update($id, $entity) |
| 76 | { |
| 77 | $data = $entity->toArray(); |
| 78 | |
| 79 | $params = [ |
| 80 | ':id' => $id, |
| 81 | ':customerBookingId' => $data['customerBookingId'], |
| 82 | ':extraId' => $data['extraId'], |
| 83 | ':quantity' => $data['quantity'], |
| 84 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 85 | ]; |
| 86 | |
| 87 | try { |
| 88 | $statement = $this->connection->prepare( |
| 89 | "UPDATE {$this->table} |
| 90 | SET |
| 91 | `customerBookingId` = :customerBookingId, |
| 92 | `extraId` = :extraId, |
| 93 | `aggregatedPrice` = :aggregatedPrice, |
| 94 | `quantity` = :quantity |
| 95 | WHERE id = :id" |
| 96 | ); |
| 97 | |
| 98 | $statement->execute($params); |
| 99 | |
| 100 | return true; |
| 101 | } catch (\Exception $e) { |
| 102 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 |