feat(data): add attribute and coupon repositories

This commit is contained in:
dualfroz committed 2026-08-24 09:40:02 +02:00
1 parent b5dc9c3ace
commit da11a2119a
2 files changed
+156

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
<?php
namespace Bazaar\Repositories;
use Bazaar\Core\Database;
final class AttributeRepository
{
public function __construct(private readonly Database $db)
{
}
public function all(): array
{
return $this->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;
}
}
+83
View File
@@ -0,0 +1,83 @@
<?php
namespace Bazaar\Repositories;
use Bazaar\Core\Database;
final class CouponRepository
{
public function __construct(private readonly Database $db)
{
}
public function all(): array
{
return $this->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]);
}
}