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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user