Skip to content

Commit

Permalink
feat: add reading page
Browse files Browse the repository at this point in the history
  • Loading branch information
vittorfigueiredo committed Mar 2, 2024
1 parent 024bbd5 commit 5177075
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 24 deletions.
3 changes: 2 additions & 1 deletion app/controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

abstract class controller
{
protected static function view(string $view, array $data = []) {
protected static function view(string $view, array $data = []): void
{
$viewPath = dirname(__FILE__, 2) . "/views";

if (!file_exists($viewPath . DIRECTORY_SEPARATOR . $view . ".php")) {
Expand Down
47 changes: 47 additions & 0 deletions app/controllers/ReadingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace app\controllers;

use GuzzleHttp\Client;

class ReadingController extends Controller
{
private string $openLibraryApiUrl;
public Client $httpClient;

public function __construct()
{
$this->openLibraryApiUrl = $_ENV["OPENLIBRARY_URL"];
$this->httpClient = new Client();
}

public function index(): mixed
{
$response = $this->httpClient->get("https://{$this->openLibraryApiUrl}/people/vitor_figueiredo/books/currently-reading.json");
$responseBody = json_decode($response->getBody()->getContents());

$currentlyReading = [];
$readedBooks = [];

foreach ($responseBody->reading_log_entries as $book) {
array_push($currentlyReading, [
"title" => $book->work->title,
"author" => "By {$book->work->author_names[0]}",
"cover" => self::getBookCover($book->work->cover_id),
]);
}

return $this->view("reading", [
"title" => "Leituras – Vitor Figueiredo",
"currentlyReading" => $currentlyReading,
"readedBooks" => $readedBooks,
]);
}

protected function getBookCover($coverId): string
{
return "https://covers.openlibrary.org/b/id/{$coverId}-M.jpg";
}
}
20 changes: 10 additions & 10 deletions app/routes/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class Router
{
const CONTROLLER_NAMESPACE = "app\\controllers";

public static function load(string $controller, string $method)
public static function load(string $controller, string $method): void
{
try {
$controllerNamespace = self::CONTROLLER_NAMESPACE."\\".$controller;
$controllerNamespace = self::CONTROLLER_NAMESPACE . "\\" . $controller;

if (!class_exists($controllerNamespace)) {
throw new \Exception("The controller {$controller} not exists!");
Expand All @@ -38,17 +38,18 @@ public static function routes(): array
{
return [
"GET" => [
"/" => fn() => self::load("HomeController", "index"),
"/article" => fn() => self::load("ArticleController", "index"),
"/contact" => fn() => self::load("ContactController", "index"),
"/projects" => fn() => self::load("ProjectController", "index"),
"/api/article/all" => fn() => self::load("ArticleController", "getArticles"),
"/api/article/popular" => fn() => self::load("ArticleController", "getPopularArticles"),
"/" => fn () => self::load("HomeController", "index"),
"/article" => fn () => self::load("ArticleController", "index"),
"/reading" => fn () => self::load("ReadingController", "index"),
"/contact" => fn () => self::load("ContactController", "index"),
"/projects" => fn () => self::load("ProjectController", "index"),
"/api/article/all" => fn () => self::load("ArticleController", "getArticles"),
"/api/article/popular" => fn () => self::load("ArticleController", "getPopularArticles"),
],
];
}

public static function execute()
public static function execute(): void
{
try {
$routes = self::routes();
Expand Down Expand Up @@ -79,7 +80,6 @@ public static function execute()
}

$router();

} catch (\Throwable $th) {
echo $th->getMessage();
}
Expand Down
17 changes: 4 additions & 13 deletions app/views/master.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ function gtag() {
<li class="nav-item">
<a class="nav-link projects text-dark" href="/projects">Projetos</a>
</li>
<li class="nav-item" hidden>
<a class="nav-link music text-dark" href="/music">Musica</a>
<li class="nav-item">
<a class="nav-link reading text-dark" href="/reading">Leituras</a>
</li>
<li class="nav-item">
<a class="nav-link contact text-dark" href="/contact">Contato</a>
Expand All @@ -61,17 +61,8 @@ function gtag() {

<div class="form-check form-switch ms-auto mt-3 me-3">
<label class="form-check-label ms-3" for="lightSwitch">
<svg
xmlns="http://www.w3.org/2000/svg"
width="25"
height="25"
fill="currentColor"
class="bi bi-brightness-high"
viewBox="0 0 16 16"
>
<path
d="M8 11a3 3 0 1 1 0-6 3 3 0 0 1 0 6zm0 1a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z"
/>
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" fill="currentColor" class="bi bi-brightness-high" viewBox="0 0 16 16">
<path d="M8 11a3 3 0 1 1 0-6 3 3 0 0 1 0 6zm0 1a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z" />
</svg>
</label>
<input class="form-check-input" type="checkbox" id="lightSwitch" />
Expand Down
48 changes: 48 additions & 0 deletions app/views/reading.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php $this->layout("master", ["title" => $title, "currentlyReading" => $currentlyReading]) ?>

<div class="d-flex justify-content-center mt-3">
<h1 class="d-flex align-items-center fw-bolder me-2">Leituras</h1>
</div>
<div class="g-4 mt-4">
<h2 class="mb-3">Lendo</h2>
<p class="mb-4 text-dark">Aqui você encontra os livros que estou lendo atualmente. Estes dados vem do <a href="https://openlibrary.org/people/vitor_figueiredo/books/currently-reading" class="text-light text-decoration-underline" target="_blank">meu perfil Open Library</a>.</p>
<?php if (count($currentlyReading) > 0) { ?>
<ul class="list-group">
<?php foreach ($currentlyReading as $book) { ?>
<a href="#<?= $book["title"] ?>" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center bg-secondary">
<div class="flex-column text-white">
<?= $book["title"] ?>
<p><small><?= $book["author"] ?></small></p>
</div>
<div class="image-parent">
<img src="<?= $book["cover"] ?>" class="img-fluid" alt="<?= $book["title"] ?>" width="50px" height="auto">
</div>
</a>
<?php } ?>
</ul>
<?php } else { ?>
<p class="text-light text-center font-weight-bold"><strong>Nenhum livro sendo lido recentemente.</strong></p>
<?php } ?>

<h2 class="mt-5 mb-3">Lidos</h2>
<p class="mb-4 text-dark">Aqui tem alguns livros que li recentemente. Você pode encontrar a lista completa no <a href="https://openlibrary.org/people/vitor_figueiredo/books/already-read" class="text-light text-decoration-underline" target="_blank">meu perfil Open Library</a>.</p>
<?php if (count($readedBooks) > 0) { ?>
<?php foreach ($readedBooks as $book) { ?>
<div class="card mb-2 w-100 bg-dark">
<div class="row g-0">
<div class="me-3 col-md-1">
<img class="p-1 m-0" src="<?= $book["cover"] ?>" width="auto" height="100px" alt="<?= $book["title"] ?>">
</div>
<div class="col-md-8">
<div class="card-body pt-2">
<h5 class="card-title"><?= $book["title"] ?></h5>
<p class="card-text"><?= $book["author"] ?></p>
</div>
</div>
</div>
</div>
<?php } ?>
<?php } else { ?>
<p class="text-light text-center my-5"><strong>Nenhum livro lido recentemente.</strong></p>
<?php } ?>
</div>

0 comments on commit 5177075

Please sign in to comment.