initial code commit
This commit is contained in:
69
Service/BillManager.php
Normal file
69
Service/BillManager.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Service;
|
||||
|
||||
use Hpz937\BillReminder\Database\DatabaseInterface;
|
||||
use Hpz937\BillReminder\Model\Bill;
|
||||
|
||||
class BillManager {
|
||||
private DatabaseInterface $db;
|
||||
|
||||
public function __construct(DatabaseInterface $db) {
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function createBill(Bill $bill): bool {
|
||||
$sql = "INSERT INTO bills (dueDate, amount, description, isPaid) VALUES (:dueDate, :amount, :description, :isPaid)";
|
||||
$this->db->prepare($sql);
|
||||
$this->db->execute([
|
||||
':dueDate' => $bill->dueDate,
|
||||
':amount' => $bill->amount,
|
||||
':description' => $bill->description,
|
||||
':isPaid' => $bill->isPaid ? 1 : 0,
|
||||
]);
|
||||
return $this->db->lastInsertId() > 0;
|
||||
}
|
||||
|
||||
public function getBill(int $id): ?Bill {
|
||||
$sql = "SELECT * FROM bills WHERE id = :id";
|
||||
$this->db->prepare($sql);
|
||||
$this->db->execute([':id' => $id]);
|
||||
$result = $this->db->fetch();
|
||||
if ($result) {
|
||||
return new Bill($result['id'], $result['dueDate'], $result['amount'], $result['description'], $result['isPaid'] == 1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function updateBill(Bill $bill): bool {
|
||||
$sql = "UPDATE bills SET dueDate = :dueDate, amount = :amount, description = :description, isPaid = :isPaid WHERE id = :id";
|
||||
$this->db->prepare($sql);
|
||||
return $this->db->execute([
|
||||
':id' => $bill->id,
|
||||
':dueDate' => $bill->dueDate,
|
||||
':amount' => $bill->amount,
|
||||
':description' => $bill->description,
|
||||
':isPaid' => $bill->isPaid ? 1 : 0,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function deleteBill(int $id): bool {
|
||||
$sql = "DELETE FROM bills WHERE id = :id";
|
||||
$this->db->prepare($sql);
|
||||
return $this->db->execute([':id' => $id]);
|
||||
}
|
||||
|
||||
|
||||
public function listBills(): array {
|
||||
$sql = "SELECT * FROM bills";
|
||||
$this->db->prepare($sql);
|
||||
$this->db->execute();
|
||||
$bills = [];
|
||||
while ($row = $this->db->fetch()) {
|
||||
$bills[] = new Bill($row['id'], $row['dueDate'], $row['amount'], $row['description'], $row['isPaid'] == 1);
|
||||
}
|
||||
return $bills;
|
||||
}
|
||||
}
|
||||
42
Service/UserManager.php
Normal file
42
Service/UserManager.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace Hpz937\BillReminder\Service;
|
||||
|
||||
use Hpz937\BillReminder\Database\DatabaseInterface;
|
||||
use Hpz937\BillReminder\Model\User;
|
||||
|
||||
class UserManager {
|
||||
private DatabaseInterface $db;
|
||||
|
||||
public function __construct(DatabaseInterface $db) {
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function register(string $username, string $password): bool {
|
||||
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
|
||||
|
||||
$sql = "INSERT INTO users (username, passwordHash) VALUES (:username, :passwordHash)";
|
||||
$this->db->prepare($sql);
|
||||
return $this->db->execute([
|
||||
':username' => $username,
|
||||
':passwordHash' => $passwordHash,
|
||||
]);
|
||||
}
|
||||
|
||||
public function login(string $username, string $password): ?User {
|
||||
$sql = "SELECT * FROM users WHERE username = :username";
|
||||
$this->db->prepare($sql);
|
||||
$this->db->execute([':username' => $username]);
|
||||
$result = $this->db->fetch();
|
||||
|
||||
if ($result && (new User($result['id'], $result['username'], $result['passwordHash']))->verifyPassword($password)) {
|
||||
// Start or regenerate the session
|
||||
session_regenerate_id();
|
||||
$_SESSION['user_id'] = $result['id'];
|
||||
return new User($result['id'], $result['username'], $result['passwordHash']);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Additional methods as needed, e.g., for password change, user details update, etc.
|
||||
}
|
||||
Reference in New Issue
Block a user