<?php

require_once '../../core/api_bootstrap.php';

$data = Request::get();

try {

Validator::required($data,['placement']);

$db = new Database();
$conn = $db->connect();

$stmt = $conn->prepare("

SELECT *
FROM ads
WHERE placement=:placement
AND status=1
AND (start_date IS NULL OR start_date<=CURDATE())
AND (end_date IS NULL OR end_date>=CURDATE())

ORDER BY priority DESC
LIMIT 5

");

$stmt->execute([
':placement'=>$data['placement']
]);

$ads = $stmt->fetchAll();

Response::success($ads);

}
catch(Exception $e){

Logger::log("system.log",$e->getMessage());

Response::error("Server error",500);

}
