Files
html/generated/api-20260411-210238/schema.sql
2026-04-12 22:57:03 +02:00

244 lines
5.2 KiB
SQL

**Schema de base pour l'API REST de gestion de projets**
```sql
-- Création de la base de données
CREATE DATABASE projet_rest;
-- Connexion à la base de données
\c projet_rest
-- Création de la table d'utilisateurs
CREATE TABLE utilisateurs (
id SERIAL PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
mdp VARCHAR(255) NOT NULL,
nom VARCHAR(255) NOT NULL,
prenom VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Création de la table de projets
CREATE TABLE projets (
id SERIAL PRIMARY KEY,
titre VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Création de la table de tâches
CREATE TABLE taches (
id SERIAL PRIMARY KEY,
titre VARCHAR(255) NOT NULL,
description TEXT,
projet_id INTEGER NOT NULL REFERENCES projets(id),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Création de la table de membres
CREATE TABLE membres (
id SERIAL PRIMARY KEY,
utilisateur_id INTEGER NOT NULL REFERENCES utilisateurs(id),
projet_id INTEGER NOT NULL REFERENCES projets(id),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (utilisateur_id, projet_id)
);
-- Création de l'index sur la table d'utilisateurs
CREATE INDEX idx_users_email ON utilisateurs (email);
-- Création de l'index sur la table de projets
CREATE INDEX idx_projects_titre ON projets (titre);
-- Création de l'index sur la table de tâches
CREATE INDEX idx_tasks_projet_id ON taches (projet_id);
-- Création de l'index sur la table de membres
CREATE INDEX idx_membres_projet_id ON membres (projet_id);
-- Insert des données de démo
INSERT INTO utilisateurs (email, mdp, nom, prenom)
VALUES
('john.doe@example.com', 'hashed_mdp', 'Doe', 'John'),
('jane.doe@example.com', 'hashed_mdp', 'Doe', 'Jane'),
('bob.smith@example.com', 'hashed_mdp', 'Smith', 'Bob');
INSERT INTO projets (titre, description)
VALUES
('Projet 1', 'Projet 1'),
('Projet 2', 'Projet 2'),
('Projet 3', 'Projet 3');
INSERT INTO taches (titre, description, projet_id)
VALUES
('Tâche 1', 'Tâche 1', 1),
('Tâche 2', 'Tâche 2', 2),
('Tâche 3', 'Tâche 3', 3);
INSERT INTO membres (utilisateur_id, projet_id)
VALUES
(1, 1),
(2, 2),
(3, 3);
```
**API REST pour gestion de projets**
```php
// Fichier de configuration
$ayarlar = [
'dbname' => 'projet_rest',
'username' => 'utilisateur',
'password' => 'mdp',
'host' => 'localhost',
'port' => 5432,
];
// Fichier de connexion à la base de données
$dsn = "pgsql:host=$ayarlar[host];dbname=$ayarlar[dbname]";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $ayarlar['username'], $ayarlar['password'], $options);
// Fichier de modèle (Model)
class Utilisateur
{
private $id;
private $email;
private $mdp;
private $nom;
private $prenom;
public function __construct($id, $email, $mdp, $nom, $prenom)
{
$this->id = $id;
$this->email = $email;
$this->mdp = $mdp;
$this->nom = $nom;
$this->prenom = $prenom;
}
public function getId()
{
return $this->id;
}
public function getEmail()
{
return $this->email;
}
public function getMdp()
{
return $this->mdp;
}
public function getNom()
{
return $this->nom;
}
public function getPrenom()
{
return $this->prenom;
}
public function setDate($created_at, $updated_at)
{
$this->created_at = $created_at;
$this->updated_at = $updated_at;
}
}
class Projet
{
private $id;
private $titre;
private $description;
public function __construct($id, $titre, $description)
{
$this->id = $id;
$this->titre = $titre;
$this->description = $description;
}
public function getId()
{
return $this->id;
}
public function getTitre()
{
return $this->titre;
}
public function getDescription()
{
return $this->description;
}
public function setDate($created_at, $updated_at)
{
$this->created_at = $created_at;
$this->updated_at = $updated_at;
}
}
class Tache
{
private $id;
private $titre;
private $description;
private $projet_id;
public function __construct($id, $titre, $description, $projet_id)
{
$this->id = $id;
$this->titre = $titre;
$this->description = $description;
$this->projet_id = $projet_id;
}
public function getId()
{
return $this->id;
}
public function getTitre()
{
return $this->titre;
}
public function getDescription()
{
return $this->description;
}
public function getProjetId()
{
return $this->projet_id;
}
public function setDate($created_at, $updated_at)
{
$this->created_at = $created_at;
$this->updated_at = $updated_at;
}
}
class Membre
{
private $id;
private $utilisateur_id;
private $projet_id;
public function __construct($id, $utilisateur_id, $projet_id)
{
$this->id = $id;
$this->utilisateur_id = $utilisateur_id;
$this