feat(data): add attribute and coupon repositories
This commit is contained in:
1 parent
b5dc9c3ace
commit
da11a2119a
2 files changed
+156
No files matched your search
@@ -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]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user