buidl out some basic functionality
This commit is contained in:
@@ -61,7 +61,8 @@ class Bill
|
||||
$this->db->query($sql, $params);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
//log the error
|
||||
var_dump($e->getMessage());
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,25 @@
|
||||
<?php
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Exception;
|
||||
use SQLite3;
|
||||
|
||||
class SQLiteAdapter implements DatabaseInterface
|
||||
{
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* Connect to the SQLite database
|
||||
*
|
||||
* @return SQLite3
|
||||
*/
|
||||
public function connect()
|
||||
{
|
||||
if ($this->connection === null) {
|
||||
try {
|
||||
$this->connection = new PDO('sqlite:' . __DIR__ . '/../../database.db');
|
||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
} catch (PDOException $e) {
|
||||
$this->connection = new SQLite3(__DIR__ . '/../../database.db');
|
||||
} catch (Exception $e) {
|
||||
// In a real application, you might want to use a more sophisticated error handling approach
|
||||
die("Connection error: " . $e->getMessage());
|
||||
}
|
||||
@@ -24,17 +28,35 @@ class SQLiteAdapter implements DatabaseInterface
|
||||
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 = [])
|
||||
{
|
||||
$stmt = $this->connect()->prepare($query);
|
||||
|
||||
if (!$stmt->execute($params)) {
|
||||
// Again, consider a more sophisticated error handling in a real application
|
||||
die("Query error: " . implode(", ", $stmt->errorInfo()));
|
||||
foreach ($params as $key => $value) {
|
||||
// var_dump($key, $value);
|
||||
$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)) {
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$output = [];
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$output[] = $row;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
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 {
|
||||
$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
|
||||
$_SESSION['user_id'] = $user[0]['id'];
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user