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