78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Hpz937\BillReminder\Controller;
|
|
|
|
use Hpz937\BillReminder\Bill\Bill;
|
|
use Hpz937\BillReminder\Database\SQLiteAdapter;
|
|
|
|
class ApiController {
|
|
protected $db;
|
|
|
|
public function __construct() {
|
|
$this->db = new SQLiteAdapter();
|
|
}
|
|
|
|
public function getBills() {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['error' => 'Unauthorized']);
|
|
http_response_code(401);
|
|
exit;
|
|
}
|
|
|
|
$billManager = new Bill($this->db);
|
|
$userId = $_SESSION['user_id'];
|
|
$bills = $billManager->getBillsByUserId($userId);
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode($bills);
|
|
}
|
|
|
|
public function addBill() {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['error' => 'Unauthorized or Invalid Request']);
|
|
http_response_code(401);
|
|
return;
|
|
}
|
|
|
|
$userId = $_SESSION['user_id'];
|
|
$description = $_POST['description'] ?? '';
|
|
$amount = $_POST['amount'] ?? '';
|
|
$dueDate = $_POST['due_date'] ?? '';
|
|
|
|
// Perform necessary validation on inputs
|
|
|
|
$billManager = new Bill($this->db);
|
|
$result = $billManager->addBill($userId, $description, $amount, $dueDate);
|
|
|
|
if ($result) {
|
|
echo json_encode(['success' => 'Bill added successfully']);
|
|
} else {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Failed to add bill']);
|
|
}
|
|
}
|
|
|
|
public function markBillPaid() {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['error' => 'Unauthorized or Invalid Request']);
|
|
http_response_code(401);
|
|
return;
|
|
}
|
|
|
|
$json = json_decode(file_get_contents("php://input"), true);
|
|
$billId = $json['id'] ?? '';
|
|
|
|
// Perform necessary validation on inputs
|
|
|
|
$billManager = new Bill($this->db);
|
|
$result = $billManager->markAsPaid($billId);
|
|
|
|
if ($result) {
|
|
echo json_encode(['success' => 'Bill marked as paid']);
|
|
} else {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Failed to mark bill as paid']);
|
|
}
|
|
}
|
|
}
|