diff --git a/src/Repositories/AttributeRepository.php b/src/Repositories/AttributeRepository.php new file mode 100644 index 0000000..ab50b63 --- /dev/null +++ b/src/Repositories/AttributeRepository.php @@ -0,0 +1,73 @@ +db->select('SELECT * FROM attributes ORDER BY name ASC'); + } + + public function find(int $id): ?array + { + return $this->db->selectOne('SELECT * FROM attributes WHERE id = ?', [$id]); + } + + public function findBySlug(string $slug): ?array + { + return $this->db->selectOne('SELECT * FROM attributes WHERE slug = ?', [$slug]); + } + + public function create(string $name, string $slug): int + { + return $this->db->insert( + 'INSERT INTO attributes (name, slug) VALUES (?, ?)', + [$name, $slug] + ); + } + + public function values(int $attributeId): array + { + return $this->db->select( + 'SELECT * FROM attributeValues WHERE attributeId = ? ORDER BY position ASC, value ASC', + [$attributeId] + ); + } + + public function findValueBySlug(int $attributeId, string $slug): ?array + { + return $this->db->selectOne( + 'SELECT * FROM attributeValues WHERE attributeId = ? AND slug = ?', + [$attributeId, $slug] + ); + } + + public function findValueBySlugAcrossAttributes(string $slug): ?array + { + return $this->db->selectOne('SELECT * FROM attributeValues WHERE slug = ? LIMIT 1', [$slug]); + } + + public function createValue(int $attributeId, string $value, string $slug, int $position = 0): int + { + return $this->db->insert( + 'INSERT INTO attributeValues (attributeId, value, slug, position) VALUES (?, ?, ?, ?)', + [$attributeId, $value, $slug, $position] + ); + } + + public function withValues(): array + { + $attributes = $this->all(); + foreach ($attributes as &$attribute) { + $attribute['values'] = $this->values((int) $attribute['id']); + } + return $attributes; + } +} diff --git a/src/Repositories/CouponRepository.php b/src/Repositories/CouponRepository.php new file mode 100644 index 0000000..c3939e5 --- /dev/null +++ b/src/Repositories/CouponRepository.php @@ -0,0 +1,83 @@ +db->select('SELECT * FROM coupons ORDER BY createdAt DESC, id DESC'); + } + + public function find(int $id): ?array + { + return $this->db->selectOne('SELECT * FROM coupons WHERE id = ?', [$id]); + } + + public function findByCode(string $code): ?array + { + return $this->db->selectOne( + 'SELECT * FROM coupons WHERE code = ?', + [strtoupper(trim($code))] + ); + } + + public function create(array $fields): int + { + return $this->db->insert( + 'INSERT INTO coupons + (code, type, value, minSubtotalCents, maxRedemptions, startsAt, expiresAt, isActive, createdAt) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', + [ + strtoupper(trim($fields['code'])), + $fields['type'], + $fields['value'], + $fields['minSubtotalCents'], + $fields['maxRedemptions'], + $fields['startsAt'], + $fields['expiresAt'], + $fields['isActive'] ? 1 : 0, + gmdate('c'), + ] + ); + } + + public function update(int $id, array $fields): void + { + $this->db->statement( + 'UPDATE coupons SET + type = ?, value = ?, minSubtotalCents = ?, maxRedemptions = ?, + startsAt = ?, expiresAt = ?, isActive = ? + WHERE id = ?', + [ + $fields['type'], + $fields['value'], + $fields['minSubtotalCents'], + $fields['maxRedemptions'], + $fields['startsAt'], + $fields['expiresAt'], + $fields['isActive'] ? 1 : 0, + $id, + ] + ); + } + + public function recordRedemption(int $id): void + { + $this->db->statement( + 'UPDATE coupons SET redeemedCount = redeemedCount + 1 WHERE id = ?', + [$id] + ); + } + + public function delete(int $id): void + { + $this->db->statement('DELETE FROM coupons WHERE id = ?', [$id]); + } +}