familyHub/api/grocery_item_delete.php

40 lines
1.2 KiB
PHP

<?php
require_once __DIR__ . '/../includes/api_bootstrap.php';
require_once __DIR__ . '/../includes/grocery_helpers.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
sendJson(['success' => false, 'error' => 'Method not allowed'], 405);
}
$people = normalizePeopleList(readJsonFile('people.json'));
requireActivePerson($people);
$body = readJsonBody();
$storeId = isset($body['storeId']) ? trim((string) $body['storeId']) : '';
$itemId = isset($body['itemId']) ? trim((string) $body['itemId']) : '';
if ($storeId === '' || $itemId === '') {
sendJson(['success' => false, 'error' => 'storeId and itemId are required'], 400);
}
$lists = normalizeGroceryLists(readJsonFile('grocery_lists.json'));
$items = $lists['byStore'][$storeId] ?? [];
$next = [];
foreach ($items as $it) {
if (($it['id'] ?? '') === $itemId) {
continue;
}
$next[] = $it;
}
if (count($next) === count($items)) {
sendJson(['success' => false, 'error' => 'Item not found'], 404);
}
$lists['byStore'][$storeId] = $next;
if (!writeJsonFile('grocery_lists.json', $lists)) {
sendJson(['success' => false, 'error' => 'Failed to save grocery list'], 500);
}
sendJson(['success' => true]);