44 lines
1.4 KiB
PHP
44 lines
1.4 KiB
PHP
<?php
|
|
namespace App\Repositories;
|
|
|
|
class CarRepository extends BaseRepository {
|
|
|
|
public function find($id, $lock = false) {
|
|
$sql = "SELECT * FROM cars WHERE id = :id";
|
|
if ($lock) {
|
|
$sql .= " FOR UPDATE";
|
|
}
|
|
$stmt = $this->db->prepare($sql);
|
|
$stmt->execute(['id' => $id]);
|
|
return $stmt->fetch();
|
|
}
|
|
|
|
public function reserve($carId, $userId, $minutes = 15) {
|
|
$expiresAt = date('Y-m-d H:i:s', strtotime("+$minutes minutes"));
|
|
$sql = "UPDATE cars SET
|
|
reserved_by = :user_id,
|
|
reserved_at = NOW(),
|
|
reservation_expires_at = :expires_at
|
|
WHERE id = :id";
|
|
$stmt = $this->db->prepare($sql);
|
|
return $stmt->execute([
|
|
'user_id' => $userId,
|
|
'expires_at' => $expiresAt,
|
|
'id' => $carId
|
|
]);
|
|
}
|
|
|
|
public function markAsSold($id) {
|
|
$stmt = $this->db->prepare("UPDATE cars SET status = 'sold' WHERE id = :id");
|
|
return $stmt->execute(['id' => $id]);
|
|
}
|
|
|
|
public function isAvailable($id) {
|
|
$sql = "SELECT * FROM cars WHERE id = :id
|
|
AND status = 'approved'
|
|
AND (reserved_by IS NULL OR reservation_expires_at < NOW())";
|
|
$stmt = $this->db->prepare($sql);
|
|
$stmt->execute(['id' => $id]);
|
|
return $stmt->fetch();
|
|
}
|
|
} |