<?php
// fetch_posts.php
// Autors: Kārlis Mālnieks
// pievieno datubazes savienojumu
require_once 'db_connection.php';

// sagatavo SQL pieprasijumu, lai iegutu visus ierakstus ar lietotaju vardiem
$sql = "SELECT p.*, e.username FROM posts p JOIN emails_v e ON p.user_id = e.id ORDER BY p.created_at DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);

// atkodē attelu celus no JSON formata
foreach ($posts as &$post) {
    $post['image'] = json_decode($post['image'], true);
}

// atgriez ierakstus JSON formata
echo json_encode(['posts' => $posts]);

?>
