feature: Initial commit

This commit is contained in:
Keith Solomon
2025-02-08 11:53:31 -06:00
commit a28cf6a713
14 changed files with 1970 additions and 0 deletions

40
includes/api.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
require_once './bill.php';
require_once './db.php';
header('Content-Type: application/json');
class InvalidActionException extends Exception {}
try {
$action = $_GET['action'] ?? '';
switch($action) {
case 'add':
$data = [
'date' => $_POST['date'],
'billName' => $_POST['billName'],
'amount' => (float)$_POST['amount'],
'paymentId' => $_POST['paymentId'],
'year' => (int)explode('-', $_POST['date'])[0]
];
echo json_encode(['success' => Bill::add($data)]);
break;
case 'getAll':
echo json_encode(Bill::getAll()->fetchAll(PDO::FETCH_ASSOC));
break;
case 'getTotals':
echo json_encode(Bill::getYearlyTotals()->fetchAll(PDO::FETCH_ASSOC));
break;
default:
throw new InvalidActionException('Invalid action');
}
} catch(Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}

31
includes/bill.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
class Bill {
public static function getAll() {
$db = DB::connect();
return $db->query("SELECT * FROM bills ORDER BY billDate DESC");
}
public static function add($data) {
$db = DB::connect();
$stmt = $db->prepare("INSERT INTO bills
(billDate, billName, amount, paymentId, year)
VALUES (?, ?, ?, ?, ?)");
return $stmt->execute([
$data['date'],
$data['billName'],
$data['amount'],
$data['paymentId'],
$data['year']
]);
}
public static function getYearlyTotals() {
$db = DB::connect();
return $db->query("SELECT year, billName, SUM(amount) as total
FROM bills GROUP BY year, billName");
}
}

17
includes/db.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
class DB {
public static function connect() {
$db = new PDO('sqlite:'.__DIR__.'/../data/bills.db');
$db->exec("CREATE TABLE IF NOT EXISTS bills (
id INTEGER PRIMARY KEY,
billDate TEXT NOT NULL,
billName TEXT NOT NULL,
amount REAL NOT NULL,
paymentId TEXT,
year INTEGER NOT NULL
)");
return $db;
}
}

21
includes/init.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
// Set up error reporting for development (disable in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Define the base path for includes
define('BASE_PATH', __DIR__ . '/');
// Autoload classes from the includes directory
spl_autoload_register(function ($className) {
$filePath = BASE_PATH . '/' . strtolower($className) . '.php';
if (file_exists($filePath)) {
require_once $filePath;
}
});
// Start session management
session_start();
// Additional configuration can be added here if needed