46 lines
1.6 KiB
PHP
46 lines
1.6 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
session_start();
|
|
|
|
$user_id = $_SESSION['user_id'] ?? null;
|
|
if (!$user_id) {
|
|
echo json_encode(['success' => false, 'error' => 'Not logged in']);
|
|
exit;
|
|
}
|
|
|
|
$startup_id = $_POST['startup_id'] ?? null;
|
|
$action = $_POST['action'] ?? 'follow'; // 'follow' or 'unfollow'
|
|
|
|
if (!$startup_id) {
|
|
echo json_encode(['success' => false, 'error' => 'Startup ID is required']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
if ($action === 'follow') {
|
|
$stmt = db()->prepare("INSERT IGNORE INTO startup_followers (startup_id, user_id) VALUES (?, ?)");
|
|
$stmt->execute([$startup_id, $user_id]);
|
|
|
|
// Update denormalized count
|
|
$stmt = db()->prepare("UPDATE startups SET followers_count = (SELECT COUNT(*) FROM startup_followers WHERE startup_id = ?) WHERE id = ?");
|
|
$stmt->execute([$startup_id, $startup_id]);
|
|
} else {
|
|
$stmt = db()->prepare("DELETE FROM startup_followers WHERE startup_id = ? AND user_id = ?");
|
|
$stmt->execute([$startup_id, $user_id]);
|
|
|
|
// Update denormalized count
|
|
$stmt = db()->prepare("UPDATE startups SET followers_count = (SELECT COUNT(*) FROM startup_followers WHERE startup_id = ?) WHERE id = ?");
|
|
$stmt->execute([$startup_id, $startup_id]);
|
|
}
|
|
|
|
// Get new count
|
|
$stmt = db()->prepare("SELECT followers_count FROM startups WHERE id = ?");
|
|
$stmt->execute([$startup_id]);
|
|
$new_count = $stmt->fetchColumn();
|
|
|
|
echo json_encode(['success' => true, 'new_count' => $new_count]);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|