buidl out some basic functionality
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
// main.js
|
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
const addBillForm = document.getElementById('addBillForm');
|
const addBillForm = document.getElementById('addBillForm');
|
||||||
if (addBillForm) {
|
if (addBillForm) {
|
||||||
@@ -8,37 +6,12 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
const formData = new FormData(this);
|
const formData = new FormData(this);
|
||||||
axios.post('/api/add-bill', formData)
|
axios.post('/api/add-bill', formData)
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
// Handle success, e.g., close modal, refresh bill list
|
|
||||||
console.log('Bill added successfully');
|
console.log('Bill added successfully');
|
||||||
loadBills(); // Reload the bills list
|
loadBills();
|
||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
// Handle error, e.g., display error message
|
|
||||||
console.error('Error adding bill:', error);
|
console.error('Error adding bill:', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.querySelectorAll('.edit-bill-btn').forEach(button => {
|
|
||||||
button.addEventListener('click', function() {
|
|
||||||
const billId = this.getAttribute('data-bill-id');
|
|
||||||
// Fetch bill details and populate the form in the modal
|
|
||||||
// Then, handle the form submission similar to the add bill form
|
|
||||||
});
|
|
||||||
});
|
|
||||||
document.querySelectorAll('.delete-bill-btn').forEach(button => {
|
|
||||||
button.addEventListener('click', function() {
|
|
||||||
const billId = this.getAttribute('data-bill-id');
|
|
||||||
axios.post('/api/delete-bill', { id: billId })
|
|
||||||
.then(function(response) {
|
|
||||||
// Handle success
|
|
||||||
console.log('Bill deleted successfully');
|
|
||||||
loadBills(); // Reload the bills list
|
|
||||||
})
|
|
||||||
.catch(function(error) {
|
|
||||||
// Handle error
|
|
||||||
console.error('Error deleting bill:', error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,17 +19,31 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
if (document.getElementById('billsTable')) {
|
if (document.getElementById('billsTable')) {
|
||||||
loadBills();
|
loadBills();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
|
// Event delegation for dynamically added "Mark as Paid" buttons
|
||||||
|
document.addEventListener('click', function(e) {
|
||||||
|
if (e.target && e.target.matches('.mark-bill-paid-btn')) {
|
||||||
|
const billId = e.target.getAttribute('data-bill-id');
|
||||||
|
axios.post('/api/mark-bill-paid', { id: billId })
|
||||||
|
.then(function(response) {
|
||||||
|
console.log('Bill marked as paid successfully');
|
||||||
|
loadBills();
|
||||||
|
})
|
||||||
|
.catch(function(error) {
|
||||||
|
console.error('Error marking bill as paid:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function loadBills() {
|
function loadBills() {
|
||||||
const billsTableBody = document.querySelector('#billsTable tbody');
|
const billsTableBody = document.querySelector('#billsTable tbody');
|
||||||
billsTableBody.innerHTML = '<tr><td colspan="4" class="text-center">Loading bills...</td></tr>'; // Loading indicator
|
billsTableBody.innerHTML = '<tr><td colspan="4" class="text-center">Loading bills...</td></tr>';
|
||||||
|
|
||||||
axios.get('/api/bills')
|
axios.get('/api/bills')
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
const bills = response.data;
|
const bills = response.data;
|
||||||
billsTableBody.innerHTML = ''; // Clear loading indicator
|
billsTableBody.innerHTML = '';
|
||||||
|
|
||||||
bills.forEach(function(bill) {
|
bills.forEach(function(bill) {
|
||||||
const row = `
|
const row = `
|
||||||
@@ -65,7 +52,9 @@ function loadBills() {
|
|||||||
<td>$${parseFloat(bill.amount).toFixed(2)}</td>
|
<td>$${parseFloat(bill.amount).toFixed(2)}</td>
|
||||||
<td>${bill.due_date}</td>
|
<td>${bill.due_date}</td>
|
||||||
<td>
|
<td>
|
||||||
<!-- Add action buttons here -->
|
${bill.is_paid ? '' : '<button type="button" class="btn btn-success btn-sm mark-bill-paid-btn" data-bill-id="' + bill.id + '">Mark as Paid</button>'}
|
||||||
|
<button type="button" class="btn btn-primary btn-sm edit-bill-btn" data-bill-id="${bill.id}">Edit</button>
|
||||||
|
<button type="button" class="btn btn-danger btn-sm delete-bill-btn" data-bill-id="${bill.id}">Delete</button>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
`;
|
`;
|
||||||
@@ -74,6 +63,6 @@ function loadBills() {
|
|||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
console.error('Error loading bills:', error);
|
console.error('Error loading bills:', error);
|
||||||
billsTableBody.innerHTML = '<tr><td colspan="4" class="text-center">Error loading bills.</td></tr>'; // Error message
|
billsTableBody.innerHTML = '<tr><td colspan="4" class="text-center">Error loading bills.</td></tr>';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
198
public/index.php
198
public/index.php
@@ -1,181 +1,41 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Hpz937\BillReminder\Bill\Bill;
|
use Hpz937\BillReminder\Controller\HomeController;
|
||||||
use Hpz937\BillReminder\Database\SQLiteAdapter;
|
use Hpz937\BillReminder\Controller\AuthController;
|
||||||
|
use Hpz937\BillReminder\Controller\DashboardController;
|
||||||
|
use Hpz937\BillReminder\Controller\ApiController;
|
||||||
|
use Hpz937\BillReminder\Routing\Router;
|
||||||
|
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
define('PROJECT_ROOT', __DIR__ . '/..');
|
||||||
|
|
||||||
|
require_once PROJECT_ROOT . '/vendor/autoload.php';
|
||||||
|
|
||||||
session_start();
|
session_start();
|
||||||
|
|
||||||
$request = $_SERVER['REQUEST_URI'];
|
$router = new Router();
|
||||||
|
|
||||||
// Initialize database connection
|
// Home Routes
|
||||||
// $dbConfig = require __DIR__ . '/../config/database.php'; // Assuming you have a config file
|
$router->register('GET', '/', [HomeController::class, 'index']);
|
||||||
$db = new \Hpz937\BillReminder\Database\SQLiteAdapter(); // Or whichever adapter you're using
|
|
||||||
|
|
||||||
|
// Auth Routes
|
||||||
|
$router->register('GET', '/login', [AuthController::class, 'showLoginForm']);
|
||||||
|
$router->register('POST', '/login', [AuthController::class, 'login']);
|
||||||
|
$router->register('GET', '/register', [AuthController::class, 'showRegistrationForm']);
|
||||||
|
$router->register('POST', '/register', [AuthController::class, 'register']);
|
||||||
|
|
||||||
switch ($request) {
|
// Dashboard Routes
|
||||||
case '/':
|
$router->register('GET', '/dashboard', [DashboardController::class, 'viewDashboard']);
|
||||||
require __DIR__ . '/../views/home.php';
|
$router->register('GET', '/settings', [DashboardController::class, 'viewSettings']);
|
||||||
break;
|
|
||||||
case '/login':
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
||||||
$username = $_POST['username'] ?? '';
|
|
||||||
$password = $_POST['password'] ?? '';
|
|
||||||
|
|
||||||
if (empty($username) || empty($password)) {
|
// API Routes
|
||||||
$error = 'Username and password are required.';
|
$router->register('GET', '/api/bills', [ApiController::class, 'getBills']);
|
||||||
require __DIR__ . '/../views/auth/login.php';
|
$router->register('POST', '/api/mark-bill-paid', [ApiController::class, 'markBillPaid']);
|
||||||
break;
|
$router->register('POST', '/api/add-bill', [ApiController::class, 'addBill']);
|
||||||
}
|
$router->register('POST', '/api/edit-bill', [ApiController::class, 'editBill']);
|
||||||
|
$router->register('POST', '/api/delete-bill', [ApiController::class, 'deleteBill']);
|
||||||
|
|
||||||
$user = new \Hpz937\BillReminder\User\User($db);
|
// Resolve the current request
|
||||||
if ($user->login($username, $password)) {
|
$requestMethod = $_SERVER['REQUEST_METHOD'];
|
||||||
header('Location: /dashboard');
|
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||||
exit;
|
|
||||||
} else {
|
|
||||||
$error = 'Login failed. Please check your credentials.';
|
|
||||||
require __DIR__ . '/../views/auth/login.php';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
require __DIR__ . '/../views/auth/login.php';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '/register':
|
$router->resolve($requestMethod, $requestUri);
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
||||||
// Extract form data
|
|
||||||
$username = $_POST['username'] ?? '';
|
|
||||||
$password = $_POST['password'] ?? '';
|
|
||||||
$email = $_POST['email'] ?? '';
|
|
||||||
|
|
||||||
// Perform validation (basic example)
|
|
||||||
if (empty($username) || empty($password) || empty($email)) {
|
|
||||||
// Handle validation error
|
|
||||||
$error = 'All fields are required.';
|
|
||||||
require __DIR__ . '/../views/auth/register.php';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instantiate User class and call register method
|
|
||||||
$user = new \Hpz937\BillReminder\User\User($db); // Assume $db is your DatabaseInterface instance
|
|
||||||
if ($user->register($username, $password, $email)) {
|
|
||||||
// Redirect to login on success
|
|
||||||
header('Location: /');
|
|
||||||
exit;
|
|
||||||
} else {
|
|
||||||
// Handle registration error
|
|
||||||
$error = 'Registration failed. Please try again.';
|
|
||||||
require __DIR__ . '/../views/auth/register.php';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
require __DIR__ . '/../views/auth/register.php';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '/dashboard':
|
|
||||||
if (!isset($_SESSION['user_id'])) {
|
|
||||||
header('Location: /login');
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
require __DIR__ . '/../views/dashboard.php';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '/settings':
|
|
||||||
if (!isset($_SESSION['user_id'])) {
|
|
||||||
header('Location: /login');
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
require __DIR__ . '/../views/settings.php';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '/api/bills':
|
|
||||||
if (!isset($_SESSION['user_id'])) {
|
|
||||||
// Return an error response if the user is not logged in
|
|
||||||
echo json_encode(['error' => 'Unauthorized']);
|
|
||||||
http_response_code(401);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize your database and Bill class instance
|
|
||||||
$db = new SQLiteAdapter();
|
|
||||||
$billManager = new Bill($db);
|
|
||||||
|
|
||||||
$userId = $_SESSION['user_id'];
|
|
||||||
$bills = $billManager->getBillsByUserId($userId); // Assuming $billManager is your Bill class instance
|
|
||||||
|
|
||||||
// Return the bills as JSON
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
echo json_encode($bills);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '/api/add-bill':
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) {
|
|
||||||
// Extract bill details from POST data
|
|
||||||
$userId = $_SESSION['user_id']; // Assuming you store user ID in session upon login
|
|
||||||
$description = $_POST['description'] ?? '';
|
|
||||||
$amount = $_POST['amount'] ?? '';
|
|
||||||
$dueDate = $_POST['due_date'] ?? '';
|
|
||||||
|
|
||||||
// Validate the inputs...
|
|
||||||
|
|
||||||
// Insert the bill into the database
|
|
||||||
$result = $billManager->addBill($userId, $dueDate, $amount, $description);
|
|
||||||
|
|
||||||
if ($result) {
|
|
||||||
echo json_encode(['success' => 'Bill added successfully']);
|
|
||||||
} else {
|
|
||||||
http_response_code(500);
|
|
||||||
echo json_encode(['error' => 'Failed to add bill']);
|
|
||||||
}
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '/api/edit-bill':
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) {
|
|
||||||
// Extract bill details and ID from POST data
|
|
||||||
$billId = $_POST['id'] ?? '';
|
|
||||||
$description = $_POST['description'] ?? '';
|
|
||||||
$amount = $_POST['amount'] ?? '';
|
|
||||||
$dueDate = $_POST['due_date'] ?? '';
|
|
||||||
|
|
||||||
// Validate the inputs and ensure the bill belongs to the logged-in user...
|
|
||||||
|
|
||||||
// Update the bill in the database
|
|
||||||
$result = $billManager->editBill($billId, $dueDate, $amount, $description);
|
|
||||||
|
|
||||||
if ($result) {
|
|
||||||
echo json_encode(['success' => 'Bill updated successfully']);
|
|
||||||
} else {
|
|
||||||
http_response_code(500);
|
|
||||||
echo json_encode(['error' => 'Failed to update bill']);
|
|
||||||
}
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '/api/delete-bill':
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) {
|
|
||||||
// Extract bill ID from POST data
|
|
||||||
$billId = $_POST['id'] ?? '';
|
|
||||||
|
|
||||||
// Validate the ID and ensure the bill belongs to the logged-in user...
|
|
||||||
|
|
||||||
// Delete the bill from the database
|
|
||||||
$result = $billManager->deleteBill($billId);
|
|
||||||
|
|
||||||
if ($result) {
|
|
||||||
echo json_encode(['success' => 'Bill deleted successfully']);
|
|
||||||
} else {
|
|
||||||
http_response_code(500);
|
|
||||||
echo json_encode(['error' => 'Failed to delete bill']);
|
|
||||||
}
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
http_response_code(404);
|
|
||||||
echo 'Page not found';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -61,7 +61,8 @@ class Bill
|
|||||||
$this->db->query($sql, $params);
|
$this->db->query($sql, $params);
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
// Handle or log the error appropriately
|
//log the error
|
||||||
|
var_dump($e->getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
77
src/Controller/ApiController.php
Normal file
77
src/Controller/ApiController.php
Normal 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']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/Controller/AuthController.php
Normal file
62
src/Controller/AuthController.php
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
namespace Hpz937\BillReminder\Controller;
|
||||||
|
|
||||||
|
use Hpz937\BillReminder\Database\SQLiteAdapter;
|
||||||
|
use Hpz937\BillReminder\User\User;
|
||||||
|
|
||||||
|
class AuthController {
|
||||||
|
protected $db;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->db = new SQLiteAdapter();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function showLoginForm() {
|
||||||
|
require PROJECT_ROOT . '/views/auth/login.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function login() {
|
||||||
|
$username = $_POST['username'] ?? '';
|
||||||
|
$password = $_POST['password'] ?? '';
|
||||||
|
|
||||||
|
if (empty($username) || empty($password)) {
|
||||||
|
$error = 'Username and password are required.';
|
||||||
|
require PROJECT_ROOT . '/views/auth/login.php';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = new User($this->db);
|
||||||
|
if ($user->login($username, $password)) {
|
||||||
|
header('Location: /dashboard');
|
||||||
|
exit;
|
||||||
|
} else {
|
||||||
|
$error = 'Login failed. Please check your credentials.';
|
||||||
|
require PROJECT_ROOT . '/views/auth/login.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function showRegistrationForm() {
|
||||||
|
require PROJECT_ROOT . '/views/auth/register.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register() {
|
||||||
|
$username = $_POST['username'] ?? '';
|
||||||
|
$password = $_POST['password'] ?? '';
|
||||||
|
$email = $_POST['email'] ?? '';
|
||||||
|
|
||||||
|
if (empty($username) || empty($password) || empty($email)) {
|
||||||
|
$error = 'All fields are required.';
|
||||||
|
require PROJECT_ROOT . '/views/auth/register.php';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = new User($this->db);
|
||||||
|
if ($user->register($username, $password, $email)) {
|
||||||
|
header('Location: /');
|
||||||
|
exit;
|
||||||
|
} else {
|
||||||
|
$error = 'Registration failed. Please try again.';
|
||||||
|
require PROJECT_ROOT . '/views/auth/register.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/Controller/DashboardController.php
Normal file
21
src/Controller/DashboardController.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Hpz937\BillReminder\Controller;
|
||||||
|
|
||||||
|
class DashboardController {
|
||||||
|
public function viewDashboard() {
|
||||||
|
if (!isset($_SESSION['user_id'])) {
|
||||||
|
header('Location: /login');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
require PROJECT_ROOT . '/views/dashboard.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function viewSettings() {
|
||||||
|
if (!isset($_SESSION['user_id'])) {
|
||||||
|
header('Location: /login');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
require PROJECT_ROOT . '/views/settings.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/Controller/HomeController.php
Normal file
8
src/Controller/HomeController.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace Hpz937\BillReminder\Controller;
|
||||||
|
|
||||||
|
class HomeController {
|
||||||
|
public function index() {
|
||||||
|
require PROJECT_ROOT . '/views/home.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,20 +2,24 @@
|
|||||||
|
|
||||||
namespace Hpz937\BillReminder\Database;
|
namespace Hpz937\BillReminder\Database;
|
||||||
|
|
||||||
use PDO;
|
use Exception;
|
||||||
use PDOException;
|
use SQLite3;
|
||||||
|
|
||||||
class SQLiteAdapter implements DatabaseInterface
|
class SQLiteAdapter implements DatabaseInterface
|
||||||
{
|
{
|
||||||
private $connection;
|
private $connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to the SQLite database
|
||||||
|
*
|
||||||
|
* @return SQLite3
|
||||||
|
*/
|
||||||
public function connect()
|
public function connect()
|
||||||
{
|
{
|
||||||
if ($this->connection === null) {
|
if ($this->connection === null) {
|
||||||
try {
|
try {
|
||||||
$this->connection = new PDO('sqlite:' . __DIR__ . '/../../database.db');
|
$this->connection = new SQLite3(__DIR__ . '/../../database.db');
|
||||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
} catch (Exception $e) {
|
||||||
} catch (PDOException $e) {
|
|
||||||
// In a real application, you might want to use a more sophisticated error handling approach
|
// In a real application, you might want to use a more sophisticated error handling approach
|
||||||
die("Connection error: " . $e->getMessage());
|
die("Connection error: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
@@ -24,17 +28,35 @@ class SQLiteAdapter implements DatabaseInterface
|
|||||||
return $this->connection;
|
return $this->connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query the database and return the result
|
||||||
|
*
|
||||||
|
* @param string $query
|
||||||
|
* @param array $params
|
||||||
|
* @return array|null
|
||||||
|
*/
|
||||||
public function query(string $query, array $params = [])
|
public function query(string $query, array $params = [])
|
||||||
{
|
{
|
||||||
$stmt = $this->connect()->prepare($query);
|
$stmt = $this->connect()->prepare($query);
|
||||||
|
|
||||||
if (!$stmt->execute($params)) {
|
foreach ($params as $key => $value) {
|
||||||
// Again, consider a more sophisticated error handling in a real application
|
// var_dump($key, $value);
|
||||||
die("Query error: " . implode(", ", $stmt->errorInfo()));
|
$stmt->bindValue($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// var_dump($stmt->getSQL(true));
|
||||||
|
$result = $stmt->execute();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
die("Query error: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preg_match('/^(SELECT|SHOW|PRAGMA)/i', $query)) {
|
if (preg_match('/^(SELECT|SHOW|PRAGMA)/i', $query)) {
|
||||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$output = [];
|
||||||
|
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||||
|
$output[] = $row;
|
||||||
|
}
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
26
src/Routing/Router.php
Normal file
26
src/Routing/Router.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Hpz937\BillReminder\Routing;
|
||||||
|
|
||||||
|
class Router {
|
||||||
|
protected $routes = [];
|
||||||
|
|
||||||
|
public function register($method, $path, $action) {
|
||||||
|
$this->routes[$method][$path] = $action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolve($method, $uri) {
|
||||||
|
if (isset($this->routes[$method][$uri])) {
|
||||||
|
$action = $this->routes[$method][$uri];
|
||||||
|
if (is_array($action)) {
|
||||||
|
list($class, $method) = $action;
|
||||||
|
return (new $class)->$method();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle 404 Not Found
|
||||||
|
http_response_code(404);
|
||||||
|
echo 'Page not found';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -45,9 +45,9 @@ class User
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$user = $this->db->query($sql, $params);
|
$user = $this->db->query($sql, $params);
|
||||||
if ($user && password_verify($password, $user[0]['password'])) {
|
if ($user && password_verify($password, $user['password'])) {
|
||||||
// Set session or token here as per your session management strategy
|
// Set session or token here as per your session management strategy
|
||||||
$_SESSION['user_id'] = $user[0]['id'];
|
$_SESSION['user_id'] = $user['id'];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ $content = function() { /* use ($bills) if fetching bills from the database */
|
|||||||
<span aria-hidden="true">×</span>
|
<span aria-hidden="true">×</span>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<form action="/add-bill" method="post">
|
<form id="addBillForm">
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="description">Description</label>
|
<label for="description">Description</label>
|
||||||
|
|||||||
@@ -21,6 +21,12 @@
|
|||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
<?php if (isset($error) && !empty($error)): ?>
|
||||||
|
<div class="alert alert-danger" role="alert">
|
||||||
|
<?php echo $error; ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
<?php if (isset($content) && is_callable($content)) $content(); ?>
|
<?php if (isset($content) && is_callable($content)) $content(); ?>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user