Files

323 lines
5.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
/*
* =========================================================
* API : MODIFICATION D'UN ENREGISTREMENT DE LA TABLE indice
* =========================================================
*/
require_once __DIR__ . '/database.php';
header(
'Content-Type: application/json; charset=utf-8'
);
/*
* =========================================================
* VERIFICATION METHODE HTTP
* =========================================================
*/
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(
[
'status' => 'error',
'message' => 'Méthode HTTP non autorisée.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
/*
* =========================================================
* LECTURE DU JSON
* =========================================================
*/
$contenu = file_get_contents('php://input');
if ($contenu === false || trim($contenu) === '') {
http_response_code(400);
echo json_encode(
[
'status' => 'error',
'message' => 'Aucune donnée reçue.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
$data = json_decode(
$contenu,
true
);
/*
* Vérification JSON
*/
if (!is_array($data)) {
http_response_code(400);
echo json_encode(
[
'status' => 'error',
'message' => 'Données JSON invalides.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
/*
* =========================================================
* RECUPERATION DES CHAMPS
* =========================================================
*/
$id = isset($data['id'])
? (int) $data['id']
: 0;
$indice = isset($data['indice'])
? trim((string) $data['indice'])
: '';
$nom = isset($data['nom'])
? trim((string) $data['nom'])
: '';
$isin = isset($data['isin'])
? strtoupper(trim((string) $data['isin']))
: '';
$symbole = isset($data['symbole'])
? strtoupper(trim((string) $data['symbole']))
: '';
$actif = !empty($data['actif']) ? 1 : 0;
$afficher = !empty($data['afficher']) ? 1 : 0;
/*
* =========================================================
* VALIDATION
* =========================================================
*/
if ($id <= 0) {
http_response_code(400);
echo json_encode(
[
'status' => 'error',
'message' => 'ID invalide.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
if ($indice === '') {
http_response_code(400);
echo json_encode(
[
'status' => 'error',
'message' => 'Le champ Indice est obligatoire.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
if ($nom === '') {
http_response_code(400);
echo json_encode(
[
'status' => 'error',
'message' => 'Le champ Nom est obligatoire.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
if ($isin === '') {
http_response_code(400);
echo json_encode(
[
'status' => 'error',
'message' => 'Le champ ISIN est obligatoire.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
if ($symbole === '') {
http_response_code(400);
echo json_encode(
[
'status' => 'error',
'message' => 'Le champ Symbole est obligatoire.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
/*
* Vérification longueur ISIN.
*/
if (strlen($isin) > 12) {
http_response_code(400);
echo json_encode(
[
'status' => 'error',
'message' => 'Le code ISIN ne doit pas dépasser 12 caractères.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
/*
* =========================================================
* MODIFICATION
* =========================================================
*/
try {
/*
* Vérification existence.
*/
$sqlCheck = "
SELECT id
FROM indice
WHERE id = :id
LIMIT 1
";
$stmtCheck = $pdo->prepare($sqlCheck);
$stmtCheck->execute(
[
':id' => $id
]
);
$existant = $stmtCheck->fetch();
if (!$existant) {
http_response_code(404);
echo json_encode(
[
'status' => 'error',
'message' => 'L\'enregistrement demandé n\'existe pas.'
],
JSON_UNESCAPED_UNICODE
);
exit;
}
/*
* Mise à jour.
*
* updated_at est mis à jour automatiquement ici.
*/
$sql = "
UPDATE indice
SET
indice = :indice,
nom = :nom,
isin = :isin,
symbole = :symbole,
actif = :actif,
afficher = :afficher,
updated_at = NOW()
WHERE id = :id
";
$stmt = $pdo->prepare($sql);
$stmt->execute(
[
':indice' => $indice,
':nom' => $nom,
':isin' => $isin,
':symbole' => $symbole,
':actif' => $actif,
':afficher' => $afficher,
':id' => $id
]
);
/*
* =====================================================
* REPONSE
* =====================================================
*/
echo json_encode(
[
'status' => 'success',
'message' => 'Enregistrement modifié avec succès.',
'id' => $id
],
JSON_UNESCAPED_UNICODE |
JSON_UNESCAPED_SLASHES
);
} catch (PDOException $e) {
/*
* Erreur MySQL.
*/
http_response_code(500);
echo json_encode(
[
'status' => 'error',
'message' => 'Erreur lors de la modification de l\'enregistrement.'
],
JSON_UNESCAPED_UNICODE
);
}