mirror of
https://github.com/bckelley/SimpleMVC.git
synced 2026-08-24 03:34:11 -05:00
init commit
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* Post Class
|
||||
*
|
||||
* This class is responsible for handling post-related database operations.
|
||||
*/
|
||||
class Post {
|
||||
private $db; // Database instance
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->db = new Database;
|
||||
}
|
||||
|
||||
/**
|
||||
* GetPosts Method
|
||||
*
|
||||
* Fetches all posts from the database.
|
||||
*
|
||||
* @return array An array of post objects
|
||||
*/
|
||||
public function getPosts() {
|
||||
$this->db->query('SELECT *, posts.id as postId,
|
||||
users.id as userId,
|
||||
posts.created_at as postCreated,
|
||||
users.created_at as userCreated
|
||||
FROM posts INNER JOIN users ON posts.user_id = users.id ORDER BY posts.created_at DESC');
|
||||
|
||||
$res = $this->db->resultSet();
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* GetPost Method
|
||||
*
|
||||
* Fetches a single post by its ID.
|
||||
*
|
||||
* @param int $id The ID of the post
|
||||
* @return object An object representing a single post
|
||||
*/
|
||||
public function getPost($id) {
|
||||
$this->db->query('SELECT * FROM posts WHERE id = :id');
|
||||
$this->db->bind(':id', $id);
|
||||
|
||||
$row = $this->db->single();
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* AddPost Method
|
||||
*
|
||||
* Inserts a new post into the database.
|
||||
*
|
||||
* @param array $data An associative array containing post data
|
||||
* @return bool Returns true on success, false on failure
|
||||
*/
|
||||
public function addPost($data) {
|
||||
$this->db->query('INSERT INTO posts (user_id, title, summary, body) VALUES(:userId, :title, :summary, :body)');
|
||||
|
||||
$this->db->bind(':userId', $data['userId']);
|
||||
$this->db->bind(':title', $data['title']);
|
||||
$this->db->bind(':summary', $data['summary']);
|
||||
$this->db->bind(':body', $data['body']);
|
||||
|
||||
if ($this->db->execute()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UpdatePost Method
|
||||
*
|
||||
* Updates an existing post in the database.
|
||||
*
|
||||
* @param array $data An associative array containing post data
|
||||
* @return bool Returns true on success, false on failure
|
||||
*/
|
||||
public function updatePost($data) {
|
||||
$this->db->query('UPDATE posts SET title=:title, summary=:summary, body=:body WHERE id=:id');
|
||||
|
||||
$this->db->bind(':id', $data['id']);
|
||||
$this->db->bind(':title', $data['title']);
|
||||
$this->db->bind(':summary', $data['summary']);
|
||||
$this->db->bind(':body', $data['body']);
|
||||
|
||||
if ($this->db->execute()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DeletePost Method
|
||||
*
|
||||
* Deletes a post from the database by its ID.
|
||||
*
|
||||
* @param int $id The ID of the post
|
||||
* @return bool Returns true on success, false on failure
|
||||
*/
|
||||
public function deletePost($id) {
|
||||
$this->db->query('DELETE FROM posts WHERE id = :id');
|
||||
$this->db->bind(':id', $id);
|
||||
|
||||
if ($this->db->execute()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* User Class
|
||||
*
|
||||
* This class is responsible for handling user-related database operations.
|
||||
*/
|
||||
class User {
|
||||
private $db; // Database instance
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->db = new Database;
|
||||
}
|
||||
|
||||
/**
|
||||
* GetUserById Method
|
||||
*
|
||||
* Fetches a user by their ID.
|
||||
*
|
||||
* @param int $id The ID of the user
|
||||
* @return object An object representing the user
|
||||
*/
|
||||
public function getUserById($id) {
|
||||
$this->db->query('SELECT * FROM users WHERE id = :id');
|
||||
$this->db->bind(':id', $id);
|
||||
|
||||
$row = $this->db->single();
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* FindUserByEmail Method
|
||||
*
|
||||
* Checks if a user with the given email exists.
|
||||
*
|
||||
* @param string $email The email of the user
|
||||
* @return bool Returns true if the user exists, false otherwise
|
||||
*/
|
||||
public function findUserByEmail($email) {
|
||||
$this->db->query('SELECT * FROM users WHERE email = :email');
|
||||
$this->db->bind(':email', $email);
|
||||
|
||||
$row = $this->db->single();
|
||||
|
||||
if ($this->db->rowCount() > 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register Method
|
||||
*
|
||||
* Registers a new user in the database.
|
||||
*
|
||||
* @param array $data An associative array containing user data
|
||||
* @return bool Returns true on success, false on failure
|
||||
*/
|
||||
public function register($data) {
|
||||
$this->db->query('INSERT INTO users (name, email, password) VALUES(:name, :email, :password)');
|
||||
|
||||
$this->db->bind(':name', $data['username']);
|
||||
$this->db->bind(':email', $data['email']);
|
||||
$this->db->bind(':password', $data['password']);
|
||||
|
||||
if ($this->db->execute()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Login Method
|
||||
*
|
||||
* Performs user login based on email and password.
|
||||
*
|
||||
* @param string $email The email of the user
|
||||
* @param string $password The password of the user
|
||||
* @return mixed Returns the user object on success, false on failure
|
||||
*/
|
||||
public function login($email, $password) {
|
||||
$this->db->query('SELECT * FROM users WHERE email = :email');
|
||||
$this->db->bind(':email', $email);
|
||||
|
||||
$row = $this->db->single();
|
||||
|
||||
$hashedPassword = $row->password;
|
||||
if (password_verify($password, $hashedPassword)) {
|
||||
return $row;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user