buidl out some basic functionality
This commit is contained in:
198
public/index.php
198
public/index.php
@@ -1,181 +1,41 @@
|
||||
<?php
|
||||
|
||||
use Hpz937\BillReminder\Bill\Bill;
|
||||
use Hpz937\BillReminder\Database\SQLiteAdapter;
|
||||
use Hpz937\BillReminder\Controller\HomeController;
|
||||
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();
|
||||
|
||||
$request = $_SERVER['REQUEST_URI'];
|
||||
$router = new Router();
|
||||
|
||||
// Initialize database connection
|
||||
// $dbConfig = require __DIR__ . '/../config/database.php'; // Assuming you have a config file
|
||||
$db = new \Hpz937\BillReminder\Database\SQLiteAdapter(); // Or whichever adapter you're using
|
||||
// Home Routes
|
||||
$router->register('GET', '/', [HomeController::class, 'index']);
|
||||
|
||||
// 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) {
|
||||
case '/':
|
||||
require __DIR__ . '/../views/home.php';
|
||||
break;
|
||||
case '/login':
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$username = $_POST['username'] ?? '';
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
$error = 'Username and password are required.';
|
||||
require __DIR__ . '/../views/auth/login.php';
|
||||
break;
|
||||
}
|
||||
|
||||
$user = new \Hpz937\BillReminder\User\User($db);
|
||||
if ($user->login($username, $password)) {
|
||||
header('Location: /dashboard');
|
||||
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':
|
||||
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;
|
||||
// Dashboard Routes
|
||||
$router->register('GET', '/dashboard', [DashboardController::class, 'viewDashboard']);
|
||||
$router->register('GET', '/settings', [DashboardController::class, 'viewSettings']);
|
||||
|
||||
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;
|
||||
}
|
||||
// API Routes
|
||||
$router->register('GET', '/api/bills', [ApiController::class, 'getBills']);
|
||||
$router->register('POST', '/api/mark-bill-paid', [ApiController::class, 'markBillPaid']);
|
||||
$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']);
|
||||
|
||||
// Initialize your database and Bill class instance
|
||||
$db = new SQLiteAdapter();
|
||||
$billManager = new Bill($db);
|
||||
// Resolve the current request
|
||||
$requestMethod = $_SERVER['REQUEST_METHOD'];
|
||||
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
|
||||
$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;
|
||||
}
|
||||
$router->resolve($requestMethod, $requestUri);
|
||||
|
||||
Reference in New Issue
Block a user