CouponEventRepository.php
6 years ago
CouponPackageRepository.php
3 years ago
CouponRepository.php
1 year ago
CouponServiceRepository.php
6 years ago
CouponEventRepository.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Coupon; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 6 | use AmeliaBooking\Domain\Entity\Coupon\Coupon; |
| 7 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 8 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 9 | |
| 10 | /** |
| 11 | * Class CouponEventRepository |
| 12 | * |
| 13 | * @package AmeliaBooking\Infrastructure\Repository\Coupon |
| 14 | */ |
| 15 | class CouponEventRepository extends AbstractRepository |
| 16 | { |
| 17 | |
| 18 | /** |
| 19 | * @param Coupon $coupon |
| 20 | * @param Event $event |
| 21 | * |
| 22 | * @return mixed |
| 23 | * @throws QueryExecutionException |
| 24 | */ |
| 25 | public function add($coupon, $event) |
| 26 | { |
| 27 | $couponData = $coupon->toArray(); |
| 28 | $eventData = $event->toArray(); |
| 29 | |
| 30 | $params = [ |
| 31 | ':couponId' => $couponData['id'], |
| 32 | ':eventId' => $eventData['id'], |
| 33 | ]; |
| 34 | |
| 35 | try { |
| 36 | $statement = $this->connection->prepare( |
| 37 | "INSERT INTO {$this->table} |
| 38 | ( |
| 39 | `couponId`, |
| 40 | `eventId` |
| 41 | ) |
| 42 | VALUES ( |
| 43 | :couponId, |
| 44 | :eventId |
| 45 | )" |
| 46 | ); |
| 47 | |
| 48 | $res = $statement->execute($params); |
| 49 | |
| 50 | if (!$res) { |
| 51 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 52 | } |
| 53 | |
| 54 | return $this->connection->lastInsertId(); |
| 55 | } catch (\Exception $e) { |
| 56 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param int $couponId |
| 62 | * @param int $eventId |
| 63 | * |
| 64 | * @return mixed |
| 65 | * @throws QueryExecutionException |
| 66 | */ |
| 67 | public function deleteForEvent($couponId, $eventId) |
| 68 | { |
| 69 | $params = [ |
| 70 | ':couponId' => $couponId, |
| 71 | ':eventId' => $eventId, |
| 72 | ]; |
| 73 | |
| 74 | try { |
| 75 | $statement = $this->connection->prepare( |
| 76 | "DELETE FROM {$this->table} WHERE couponId = :couponId AND eventId = :eventId" |
| 77 | ); |
| 78 | |
| 79 | return $statement->execute($params); |
| 80 | } catch (\Exception $e) { |
| 81 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 |