You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.6 KiB
90 lines
3.6 KiB
<?php
|
|
namespace App\Controllers;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
class QuoteController {
|
|
protected $db;
|
|
|
|
public function __construct($container) {
|
|
$this->db = $container->get('db');
|
|
}
|
|
|
|
// Récupérer toutes les citations
|
|
public function getQuotes(Request $request, Response $response, $args) {
|
|
$sql = "SELECT * FROM Quote";
|
|
try {
|
|
$stmt = $this->db->query($sql);
|
|
$quotes = $stmt->fetchAll(\PDO::FETCH_OBJ);
|
|
$payload = json_encode($quotes);
|
|
} catch (\PDOException $e) {
|
|
$payload = json_encode(['error' => $e->getMessage()]);
|
|
}
|
|
$response->getBody()->write($payload);
|
|
return $response->withHeader('Content-Type', 'application/json');
|
|
}
|
|
|
|
// Récupérer une citation par son ID
|
|
public function getQuote(Request $request, Response $response, $args) {
|
|
$id = $args['id'];
|
|
$sql = "SELECT * FROM Quote WHERE id_quote = :id";
|
|
try {
|
|
$stmt = $this->db->prepare($sql);
|
|
$stmt->bindParam("id", $id);
|
|
$stmt->execute();
|
|
$quote = $stmt->fetch(\PDO::FETCH_OBJ);
|
|
if(!$quote){
|
|
$payload = json_encode(['message' => 'Quote not found']);
|
|
$response->getBody()->write($payload);
|
|
return $response->withHeader('Content-Type', 'application/json')->withStatus(404);
|
|
}
|
|
$payload = json_encode($quote);
|
|
} catch (\PDOException $e) {
|
|
$payload = json_encode(['error' => $e->getMessage()]);
|
|
}
|
|
$response->getBody()->write($payload);
|
|
return $response->withHeader('Content-Type', 'application/json');
|
|
}
|
|
|
|
// Créer une nouvelle citation
|
|
public function createQuote(Request $request, Response $response, $args) {
|
|
$data = $request->getParsedBody();
|
|
$sql = "INSERT INTO Quote (content, likes, langue, isValide, reason, id_caracter, id_source, id_user_verif) VALUES (:content, :likes, :langue, :isValide, :reason, :id_caracter, :id_source, :id_user_verif)";
|
|
try {
|
|
$stmt = $this->db->prepare($sql);
|
|
$stmt->bindParam("content", $data['content']);
|
|
$stmt->bindParam("likes", $data['likes']);
|
|
$stmt->bindParam("langue", $data['langue']);
|
|
$stmt->bindParam("isValide", $data['isValide']);
|
|
$stmt->bindParam("reason", $data['reason']);
|
|
$stmt->bindParam("id_caracter", $data['id_caracter']);
|
|
$stmt->bindParam("id_source", $data['id_source']);
|
|
$stmt->bindParam("id_user_verif", $data['id_user_verif']);
|
|
$stmt->execute();
|
|
$data['id_quote'] = $this->db->lastInsertId();
|
|
$payload = json_encode($data);
|
|
} catch (\PDOException $e) {
|
|
$payload = json_encode(['error' => $e->getMessage()]);
|
|
}
|
|
$response->getBody()->write($payload);
|
|
return $response->withHeader('Content-Type', 'application/json');
|
|
}
|
|
|
|
// Supprimer une citation
|
|
public function deleteQuote(Request $request, Response $response, $args) {
|
|
$id = $args['id'];
|
|
$sql = "DELETE FROM Quote WHERE id_quote = :id";
|
|
try {
|
|
$stmt = $this->db->prepare($sql);
|
|
$stmt->bindParam("id", $id);
|
|
$stmt->execute();
|
|
$payload = json_encode(['message' => 'Quote deleted']);
|
|
} catch (\PDOException $e) {
|
|
$payload = json_encode(['error' => $e->getMessage()]);
|
|
}
|
|
$response->getBody()->write($payload);
|
|
return $response->withHeader('Content-Type', 'application/json');
|
|
}
|
|
}
|