Lógica para tomar un turno
[isfdt166-ansi-edi2-2024-server.git] / code / post_turno.php
blob1d1bf2280aed112f8e8d1dd067084c063cb63ece
1 <?php
3 function handle_request($id, $input)
5 $input = json_decode($input, true);
6 $appointment = $input['turno'];
7 $petId = $input['mascota_id'];
9 $db = new SQLite3('database.sqlite');
11 $veterinaries = [];
13 $result = $db->query('SELECT id FROM veterinario');
15 while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
16 $veterinaries[] = $row['id'];
19 $result = $db->query("SELECT veterinario_id FROM turno WHERE turno = '{$appointment}'");
21 while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
22 $key = array_search($row['veterinario_id'], $veterinaries);
23 if (false != $key) {
24 unset($veterinaries[$key]);
28 $veterinaryId = array_shift($veterinaries);
30 $db->exec("INSERT INTO turno (turno, mascota_id, veterinario_id) VALUES ('{$appointment}', '{$petId}', '{$veterinaryId}')");
32 $db->close();
34 echo json_encode([
35 'turno' => $appointment,
36 'mascota_id' => $petId,
37 ]);