CustomFieldEventRepository.php
3 months ago
CustomFieldOptionRepository.php
3 months ago
CustomFieldRepository.php
3 months ago
CustomFieldServiceRepository.php
3 months ago
CustomFieldServiceRepository.php
144 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure\Repository\CustomField; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Entity\CustomField\CustomFieldOption; |
| 11 | use AmeliaBooking\Domain\Factory\CustomField\CustomFieldOptionFactory; |
| 12 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 14 | |
| 15 | /** |
| 16 | * Class CustomFieldOptionRepository |
| 17 | * |
| 18 | * @package AmeliaBooking\Infrastructure\Repository\CustomField |
| 19 | */ |
| 20 | class CustomFieldServiceRepository extends AbstractRepository |
| 21 | { |
| 22 | public const FACTORY = CustomFieldOptionFactory::class; |
| 23 | |
| 24 | /** |
| 25 | * @param $customFieldId |
| 26 | * @param $serviceId |
| 27 | * |
| 28 | * @return int |
| 29 | * @throws QueryExecutionException |
| 30 | */ |
| 31 | public function add($customFieldId, $serviceId) |
| 32 | { |
| 33 | $params = [ |
| 34 | ':customFieldId' => $customFieldId, |
| 35 | ':serviceId' => $serviceId |
| 36 | ]; |
| 37 | |
| 38 | try { |
| 39 | $statement = $this->connection->prepare( |
| 40 | "INSERT INTO |
| 41 | {$this->table} |
| 42 | ( |
| 43 | `customFieldId`, `serviceId` |
| 44 | ) VALUES ( |
| 45 | :customFieldId, :serviceId |
| 46 | )" |
| 47 | ); |
| 48 | |
| 49 | $statement->execute($params); |
| 50 | } catch (\Exception $e) { |
| 51 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 52 | } |
| 53 | |
| 54 | return $this->connection->lastInsertId(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param int $id |
| 59 | * @param CustomFieldOption $entity |
| 60 | * |
| 61 | * @return bool |
| 62 | * @throws QueryExecutionException |
| 63 | */ |
| 64 | public function update($id, $entity) |
| 65 | { |
| 66 | $data = $entity->toArray(); |
| 67 | |
| 68 | $params = [ |
| 69 | ':customFieldId' => $data['customFieldId'], |
| 70 | ':label' => $data['label'], |
| 71 | ':position' => $data['position'], |
| 72 | ':id' => $id, |
| 73 | ]; |
| 74 | |
| 75 | try { |
| 76 | $statement = $this->connection->prepare( |
| 77 | "UPDATE {$this->table} |
| 78 | SET |
| 79 | `customFieldId` = :customFieldId, |
| 80 | `label` = :label, |
| 81 | `position` = :position |
| 82 | WHERE |
| 83 | id = :id" |
| 84 | ); |
| 85 | |
| 86 | $statement->execute($params); |
| 87 | } catch (\Exception $e) { |
| 88 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param int $customFieldId |
| 96 | * |
| 97 | * @return array |
| 98 | * @throws QueryExecutionException |
| 99 | */ |
| 100 | public function getByCustomFieldId($customFieldId) |
| 101 | { |
| 102 | try { |
| 103 | $statement = $this->connection->query( |
| 104 | "SELECT |
| 105 | cfs.id, |
| 106 | cfs.customFieldId, |
| 107 | cfs.serviceId |
| 108 | FROM {$this->table} cfs |
| 109 | WHERE cfs.customFieldId = {$customFieldId}" |
| 110 | ); |
| 111 | |
| 112 | $rows = $statement->fetchAll(); |
| 113 | } catch (\Exception $e) { |
| 114 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 115 | } |
| 116 | |
| 117 | return $rows; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param int $customFieldId |
| 122 | * @param $serviceId |
| 123 | * |
| 124 | * @return bool |
| 125 | * @throws QueryExecutionException |
| 126 | */ |
| 127 | public function deleteByCustomFieldIdAndServiceId($customFieldId, $serviceId) |
| 128 | { |
| 129 | try { |
| 130 | $statement = $this->connection->prepare( |
| 131 | "DELETE FROM {$this->table} WHERE customFieldId = :customFieldId AND serviceId = :serviceId" |
| 132 | ); |
| 133 | |
| 134 | $statement->bindParam(':customFieldId', $customFieldId); |
| 135 | $statement->bindParam(':serviceId', $serviceId); |
| 136 | |
| 137 | $statement->execute(); |
| 138 | return true; |
| 139 | } catch (\Exception $e) { |
| 140 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 |