Add article DAO
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tyler Perkins 2022-12-23 15:58:54 -05:00
parent bd966f576b
commit b621ced611
4 changed files with 112 additions and 5 deletions

28
sql/0-initdatabase.sql Normal file
View File

@ -0,0 +1,28 @@
-- 22-12-22
-- Init the database
START TRANSACTION;
CREATE DATABASE [IF NOT EXISTS] site;
USE site;
CREATE TABLE post(
ID INT AUTO_INCREMENT PRIMARY KEY,
author VARCHAR(32) NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
title VARCHAR(64) NOT NULL,
);
CREATE TABLE tag(
ID INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(32)
);
CREATE TABLE post_tag(
tag_id INT NOT NULL,
post_id INT NOT NULL,
FOREIGN KEY (tag_id) REFERENCES tag(ID) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES post(ID) ON DELETE CASCADE
);
COMMIT;

44
src/common/article.php Normal file
View File

@ -0,0 +1,44 @@
<?php
class Article {
private $client;
// Factory constructors
public static function makeNewClient() : Article {
$host = getenv("MYSQL_HOST");
$db = getenv("MYSQL_DB");
$user = getenv("MYSQL_USER");
$pass = getenv("MYSQL_PASS");
$host = "127.0.0.1";
$db = "site";
$user = "root";
$pass = "password";
return new Article($host, $db, $user, $pass);
}
public static function makeNewClientWithSpecs(String $host,
String $db, String $user, String $pass) : Article {
return new Article($host, $db, $user, $pass);
}
private function __construct(String $host, String $db, String $user, String $pass){
$this->client = mysqli_connect($host, $user, $pass, $db);
}
function getAllPosts() {
$result = mysqli_query($this->client, 'SELECT * FROM post');
return mysqli_fetch_all($result);
}
function getMostRecentPosts($limit){
$result = mysqli_query($this->client, 'SELECT * FROM post ORDER BY created DESC LIMIT ' . $limit);
return mysqli_fetch_all($result);
}
function getPostData($postId) {
$result = mysqli_query($this->client, 'SELECT * FROM post WHERE ID = ' . $postId);
return mysqli_fetch_all($result);
}
}
?>

View File

@ -31,7 +31,7 @@
text-decoration: none;
}
.git-clickable-div {
.list-clickable-div {
display: block;
width: calc(100% - 10px - 3px);
height: auto;
@ -42,16 +42,31 @@
background-color: rgba(0,0,0,0.4);
margin: 5px;
}
.git-clickable-div p {
.list-clickable-div p {
margin: 15px 5px;
}
.git-clickable-div:hover {
.list-clickable-div:hover {
text-decoration: underline 0.15em rgba(255,255,255,1);
background-color: rgba(0,0,0,0.85);
}
.clickable-header {
display: block;
text-decoration-color: rgba(255,255,255,0) !important;
}
.clickable-header h3 {
text-decoration: underline 0.15em rgba(255,255,255,0) !important;
transition: 300ms;
}
.clickable-header h3:hover {
text-decoration: underline 0.15em rgba(255,255,255,1);
}
.single-column {
grid-template-columns: 1fr !important;
}
@ -84,6 +99,7 @@
.index-column {
display: grid;
grid-template-rows: 10em 1fr;
width: calc(100% - 3em);
}

View File

@ -3,6 +3,7 @@
<head>
<?php include 'common/include.php' ?>
<?php include 'common/git.php' ?>
<?php include 'common/article.php' ?>
<title>Tyler Perkins - Home</title>
<link rel="stylesheet" href="index.css">
@ -45,7 +46,7 @@
$description = $item["description"];
//build out the html
echo '<a class="git-clickable-div" href="' . $url . '">';
echo '<a class="list-clickable-div" href="' . $url . '">';
echo '<p>' . $name . ' - ' . $description . '</p>';
echo '</a>';
}
@ -54,7 +55,25 @@
</div>
<div class="index-column">
<h3>Articles</h3>
<p>To be implemented</p>
<div>
<?php
$articleClient = Article::makeNewClient();
$posts = $articleClient->getMostRecentPosts(5);
foreach($posts as $post){
if($post == null)
continue;
$id = $post[0];
$author = $post[1];
$title = $post[3];
echo '<a class="list-clickable-div" href="/posts/?id=' . $id . '">';
echo '<p>(' . $author . ') ' . $title . '</p>';
echo '</a>';
}
?>
</div>
</div>
</div>
</div>