Skip to content
Snippets Groups Projects
Commit 2c63ed2a authored by debbyalmadea's avatar debbyalmadea
Browse files

add: Catalog Domain and Repository

parent ce871385
No related merge requests found
db/ db/
.env .env
\ No newline at end of file .DS_Store
\ No newline at end of file
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'role') THEN
CREATE TYPE role AS ENUM ('BASIC', 'ADMIN');
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id VARCHAR(255) PRIMARY KEY, id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL password VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
role role DEFAULT 'BASIC'
); );
CREATE TABLE IF NOT EXISTS sessions ( CREATE TABLE IF NOT EXISTS sessions (
id VARCHAR(255) PRIMARY KEY, id VARCHAR(255) PRIMARY KEY,
user_id VARCHAR(255) NOT NULL, user_id VARCHAR(255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) FOREIGN KEY (user_id) REFERENCES users(id)
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'category') THEN
CREATE TYPE category AS ENUM ('ANIME', 'DRAMA');
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS catalogs (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description VARCHAR(255),
poster VARCHAR(255) NOT NULL,
trailer VARCHAR(255),
category category NOT NULL
); );
\ No newline at end of file
<?php
class Catalog
{
public int $id;
public string $title;
public ?string $description = null;
public string $poster;
public ?string $trailer = null;
public string $category;
public function __toString()
{
return "Catalog: {id: $this->id, title: $this->title, description: $this->description, poster: $this->poster, trailer: $this->trailer, category: $this->category}";
}
}
\ No newline at end of file
...@@ -5,4 +5,6 @@ class User ...@@ -5,4 +5,6 @@ class User
public string $id; public string $id;
public string $name; public string $name;
public string $password; public string $password;
} public string $email;
public string $role;
}
\ No newline at end of file
<?php
require_once __DIR__ . '/../Domain/Catalog.php';
class CatalogRepository
{
private \PDO $connection;
public function __construct(\PDO $connection)
{
$this->connection = $connection;
}
public function save(Catalog $catalog): Catalog
{
$statement = $this->connection->prepare("INSERT INTO catalogs(title, description, poster, trailer, category) VALUES (?, ?, ?, ?, ?)");
$statement->execute([
$catalog->title,
$catalog->description,
$catalog->poster,
$catalog->trailer,
$catalog->category,
]);
return $catalog;
}
public function findById(string $id): ?Catalog
{
$statement = $this->connection->prepare("SELECT id, title, description, poster, trailer, category FROM catalogs WHERE id = ?");
$statement->execute([$id]);
try {
if ($row = $statement->fetch()) {
$catalog = new Catalog();
$catalog->id = $row['id'];
$catalog->title = $row['title'];
$catalog->description = $row['description'];
$catalog->poster = $row['poster'];
$catalog->trailer = $row['trailer'];
$catalog->category = $row['category'];
return $catalog;
} else {
return null;
}
} finally {
$statement->closeCursor();
}
}
public function deleteAll(): void
{
$this->connection->exec("DELETE FROM catalogs");
}
}
\ No newline at end of file
...@@ -13,16 +13,20 @@ class UserRepository ...@@ -13,16 +13,20 @@ class UserRepository
public function save(User $user): User public function save(User $user): User
{ {
$statement = $this->connection->prepare("INSERT INTO users(id, name, password) VALUES (?, ?, ?)"); $statement = $this->connection->prepare("INSERT INTO users(id, name, password, email, role) VALUES (?, ?, ?)");
$statement->execute([ $statement->execute([
$user->id, $user->name, $user->password, $user->id,
$user->name,
$user->password,
$user->email,
$user->role,
]); ]);
return $user; return $user;
} }
public function findById(string $id): ?User public function findById(string $id): ?User
{ {
$statement = $this->connection->prepare("SELECT id, name, password FROM users WHERE id = ?"); $statement = $this->connection->prepare("SELECT id, name, password, email, role FROM users WHERE id = ?");
$statement->execute([$id]); $statement->execute([$id]);
try { try {
...@@ -31,6 +35,8 @@ class UserRepository ...@@ -31,6 +35,8 @@ class UserRepository
$user->id = $row['id']; $user->id = $row['id'];
$user->name = $row['name']; $user->name = $row['name'];
$user->password = $row['password']; $user->password = $row['password'];
$user->email = $row['email'];
$user->role = $row['role'];
return $user; return $user;
} else { } else {
...@@ -45,4 +51,4 @@ class UserRepository ...@@ -45,4 +51,4 @@ class UserRepository
{ {
$this->connection->exec("DELETE FROM users"); $this->connection->exec("DELETE FROM users");
} }
} }
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment