buidl out some basic functionality

This commit is contained in:
2024-02-09 23:00:51 -06:00
parent d21d91b80e
commit e900175c4d
12 changed files with 288 additions and 216 deletions

View File

@@ -0,0 +1,77 @@
<?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']);
}
}
}