<?php
const DB_HOST = "localhost";
const DB_NAME = "piii";
const DB_USER = "root";
const DB_PASS = "root";
class db extends PDO {
function __construct() {
try {
parent::__construct("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8", DB_USER, DB_PASS);
parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
}
}
public static function get_instance() {
static $instance = false;
if(!$instance) $instance = new self; return $instance;
}
}
class Serie {
public int $id;
public string $nome;
public string $plataforma;
public int $rating;
public function __construct ($id = -1, $nome = "", $plataforma = "", $rating = -1) {
$this->id = $id;
$this->nome = $nome;
$this->plataforma = $plataforma;
$this->rating = $rating;
}
}
class GereSeries{
public $series = [];
function carregar_series() {
$DBH = db::get_instance();
$this->series = [];
try {
$stmt = $DBH->prepare('SELECT * FROM series');
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while ($s = $stmt->fetch()) {
$this->series[] = new Serie($s["id"], $s["nome"], $s["plataforma"], $s["rating"]);
}
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
echo "STACK TRACE: " . $e->getTraceAsString();
}
return $this->series;
}
function inserir_serie(Serie $s): bool {
$DBH = db::get_instance();
try {
$stmt = $DBH->prepare('INSERT INTO series (nome, plataforma, rating) VALUES (?, ?, ?)');
return $stmt->execute([$s->nome, $s->plataforma, $s->rating]);
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
return false;
}
}
function atualizar_serie(Serie $s): bool {
$DBH = db::get_instance();
try {
$stmt = $DBH->prepare('UPDATE series SET nome = ?, plataforma = ?, rating = ? WHERE id = ?');
return $stmt->execute([$s->nome, $s->plataforma, $s->rating, $s->id]);
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
return false;
}
}
function remover_serie(int $id): bool {
$DBH = db::get_instance();
try {
$stmt = $DBH->prepare('DELETE FROM series WHERE id = ?');
return $stmt->execute([$id]);
}catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
return false;
}
}
function obter_serie(int $id): ?serie {
$DBH = db::get_instance();
try {
$stmt = $DBH->prepare('SELECT * FROM series WHERE id = ?');
$stmt->execute([$id]);
$s = $stmt->fetch(PDO::FETCH_ASSOC);
if ($s) {
return new Serie ($s["id"], $s["nome"], $s["plataforma"], $s["rating"]);
}
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
}
return null;
}
}
$gs = new GereSeries();
$mensagem = "";
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["inserir"])) {
$nova_serie = new Serie(
-1,
$_POST["nome"],
$_POST["plataforma"],
$_POST["rating"]
);
if($gs->inserir_serie($nova_serie)) {
$mensagem = "Serie inserida com sucesso";
} else {
$mensagem = "Erro ao inserir série";
}
}
if(isset($_GET["op"]) && $_GET["op"] == "remover" && isset($_GET["id"])) {
if($gs->remover_serie((int)$_GET["id"])) {
$mensagem = "Registo removido com sucesso";
}
}
$gs->carregar_series();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Series</title>
</head>
<body>
<form method = "POST">
<h2>Inserir dados</h2>