feat(data): add user, category and wishlist repositories
This commit is contained in:
1 parent
08929b48b2
commit
b5dc9c3ace
3 files changed
+199
No files matched your search
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Repositories;
|
||||||
|
|
||||||
|
use Bazaar\Core\Database;
|
||||||
|
|
||||||
|
final class CategoryRepository
|
||||||
|
{
|
||||||
|
public function __construct(private readonly Database $db)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function all(): array
|
||||||
|
{
|
||||||
|
return $this->db->select('SELECT * FROM categories ORDER BY name ASC');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find(int $id): ?array
|
||||||
|
{
|
||||||
|
return $this->db->selectOne('SELECT * FROM categories WHERE id = ?', [$id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findBySlug(string $slug): ?array
|
||||||
|
{
|
||||||
|
return $this->db->selectOne('SELECT * FROM categories WHERE slug = ?', [$slug]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(string $name, string $slug, string $description): int
|
||||||
|
{
|
||||||
|
return $this->db->insert(
|
||||||
|
'INSERT INTO categories (name, slug, description) VALUES (?, ?, ?)',
|
||||||
|
[$name, $slug, $description]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(int $id, string $name, string $slug, string $description): void
|
||||||
|
{
|
||||||
|
$this->db->statement(
|
||||||
|
'UPDATE categories SET name = ?, slug = ?, description = ? WHERE id = ?',
|
||||||
|
[$name, $slug, $description, $id]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(int $id): void
|
||||||
|
{
|
||||||
|
$this->db->statement('DELETE FROM categories WHERE id = ?', [$id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withProductCounts(): array
|
||||||
|
{
|
||||||
|
return $this->db->select(
|
||||||
|
'SELECT c.*, COUNT(p.id) AS productCount
|
||||||
|
FROM categories c
|
||||||
|
LEFT JOIN products p ON p.categoryId = c.id
|
||||||
|
GROUP BY c.id
|
||||||
|
ORDER BY c.name ASC'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Repositories;
|
||||||
|
|
||||||
|
use Bazaar\Core\Database;
|
||||||
|
|
||||||
|
final class UserRepository
|
||||||
|
{
|
||||||
|
public function __construct(private readonly Database $db)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findByEmail(string $email): ?array
|
||||||
|
{
|
||||||
|
return $this->db->selectOne(
|
||||||
|
'SELECT * FROM users WHERE email = ?',
|
||||||
|
[strtolower(trim($email))]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findById(int $id): ?array
|
||||||
|
{
|
||||||
|
return $this->db->selectOne('SELECT * FROM users WHERE id = ?', [$id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function emailExists(string $email): bool
|
||||||
|
{
|
||||||
|
return (int) $this->db->scalar(
|
||||||
|
'SELECT COUNT(*) FROM users WHERE email = ?',
|
||||||
|
[strtolower(trim($email))]
|
||||||
|
) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(string $email, string $passwordHash, string $name, bool $isAdmin = false, string $role = 'customer'): int
|
||||||
|
{
|
||||||
|
if ($isAdmin) {
|
||||||
|
$role = 'admin';
|
||||||
|
}
|
||||||
|
return $this->db->insert(
|
||||||
|
'INSERT INTO users (email, passwordHash, name, isAdmin, role, createdAt)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?)',
|
||||||
|
[strtolower(trim($email)), $passwordHash, $name, $isAdmin ? 1 : 0, $role, gmdate('c')]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setRole(int $id, string $role): void
|
||||||
|
{
|
||||||
|
$this->db->statement(
|
||||||
|
'UPDATE users SET role = ?, isAdmin = ? WHERE id = ?',
|
||||||
|
[$role, $role === 'admin' ? 1 : 0, $id]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function all(): array
|
||||||
|
{
|
||||||
|
return $this->db->select('SELECT id, email, name, role, isAdmin, createdAt FROM users ORDER BY createdAt DESC, id DESC');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function count(): int
|
||||||
|
{
|
||||||
|
return (int) $this->db->scalar('SELECT COUNT(*) FROM users');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Repositories;
|
||||||
|
|
||||||
|
use Bazaar\Core\Database;
|
||||||
|
|
||||||
|
final class WishlistRepository
|
||||||
|
{
|
||||||
|
public function __construct(private readonly Database $db)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function has(int $userId, int $productId): bool
|
||||||
|
{
|
||||||
|
return (int) $this->db->scalar(
|
||||||
|
'SELECT COUNT(*) FROM wishlistItems WHERE userId = ? AND productId = ?',
|
||||||
|
[$userId, $productId]
|
||||||
|
) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add(int $userId, int $productId): void
|
||||||
|
{
|
||||||
|
$this->db->statement(
|
||||||
|
'INSERT INTO wishlistItems (userId, productId, createdAt)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
ON CONFLICT(userId, productId) DO NOTHING',
|
||||||
|
[$userId, $productId, gmdate('c')]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function remove(int $userId, int $productId): void
|
||||||
|
{
|
||||||
|
$this->db->statement(
|
||||||
|
'DELETE FROM wishlistItems WHERE userId = ? AND productId = ?',
|
||||||
|
[$userId, $productId]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toggle(int $userId, int $productId): bool
|
||||||
|
{
|
||||||
|
if ($this->has($userId, $productId)) {
|
||||||
|
$this->remove($userId, $productId);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$this->add($userId, $productId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function forUser(int $userId): array
|
||||||
|
{
|
||||||
|
return $this->db->select(
|
||||||
|
'SELECT p.*, c.name AS categoryName, c.slug AS categorySlug, w.createdAt AS savedAt
|
||||||
|
FROM wishlistItems w
|
||||||
|
JOIN products p ON p.id = w.productId
|
||||||
|
LEFT JOIN categories c ON c.id = p.categoryId
|
||||||
|
WHERE w.userId = ?
|
||||||
|
ORDER BY w.createdAt DESC, w.id DESC',
|
||||||
|
[$userId]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function productIdsForUser(int $userId): array
|
||||||
|
{
|
||||||
|
return array_map(
|
||||||
|
'intval',
|
||||||
|
array_column(
|
||||||
|
$this->db->select('SELECT productId FROM wishlistItems WHERE userId = ?', [$userId]),
|
||||||
|
'productId'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function count(int $userId): int
|
||||||
|
{
|
||||||
|
return (int) $this->db->scalar('SELECT COUNT(*) FROM wishlistItems WHERE userId = ?', [$userId]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user