This commit is contained in:
2025-02-07 02:56:17 -06:00
parent 6b01c165a9
commit a1363ee4b5
5 changed files with 169 additions and 74 deletions

View File

@ -2,7 +2,7 @@ import type { APIRoute } from "astro";
import { getS3Data, putS3Data } from "../../../lib/s3";
import type { RegistryItem } from "../../../lib/types";
const REGISTRY_FILE_KEY = "baby-registry.json";
const REGISTRY_FILE_KEY = "registry.json";
// GET: Get a specific registry item by ID
export const GET: APIRoute = async ({ params }) => {
@ -50,7 +50,7 @@ export const PUT: APIRoute = async ({ request, params }) => {
}
const body = await request.json();
const { name, taken, link } = body;
const { taken, claimedBy } = body;
const registry = await getS3Data<RegistryItem[]>(REGISTRY_FILE_KEY) || [];
const itemIndex = registry.findIndex((item) => item.id === id);
@ -62,12 +62,10 @@ export const PUT: APIRoute = async ({ request, params }) => {
);
}
// Update the item with the provided values
registry[itemIndex] = {
...registry[itemIndex],
name: name !== undefined ? name : registry[itemIndex].name,
taken: taken !== undefined ? taken : registry[itemIndex].taken,
link: link !== undefined ? link : registry[itemIndex].link,
claimedBy: claimedBy !== undefined ? claimedBy : registry[itemIndex].claimedBy,
};
await putS3Data(REGISTRY_FILE_KEY, registry);
@ -85,39 +83,54 @@ export const PUT: APIRoute = async ({ request, params }) => {
}
};
// DELETE: Delete a registry item
export const DELETE: APIRoute = async ({ params }) => {
const headers = {
"Content-Type": "application/json",
};
try {
const { id } = params;
if (!id) {
return new Response(
JSON.stringify({ success: false, error: "Item ID is required" }),
{ status: 400, headers: { "Content-Type": "application/json" } }
{ status: 400, headers }
);
}
// Get current registry data
const registry = await getS3Data<RegistryItem[]>(REGISTRY_FILE_KEY) || [];
// Find the item index
const itemIndex = registry.findIndex((item) => item.id === id);
if (itemIndex === -1) {
return new Response(
JSON.stringify({ success: false, error: "Item not found" }),
{ status: 404, headers: { "Content-Type": "application/json" } }
{ status: 404, headers }
);
}
registry.splice(itemIndex, 1); // Remove the item from the array
// Remove the item
registry.splice(itemIndex, 1);
// Save the updated registry
await putS3Data(REGISTRY_FILE_KEY, registry);
return new Response(JSON.stringify({ success: true }), {
status: 204, // No Content
headers: { "Content-Type": "application/json" },
status: 200,
headers,
});
} catch (error: any) {
console.error("Error deleting registry item:", error.message);
} catch (error) {
console.error("Error deleting registry item:", error);
return new Response(
JSON.stringify({ success: false, error: "Failed to delete item" }),
{ status: 500, headers: { "Content-Type": "application/json" } }
JSON.stringify({
success: false,
error: "Failed to delete item",
details: error instanceof Error ? error.message : "Unknown error",
}),
{ status: 500, headers }
);
}
};

View File

@ -3,7 +3,7 @@ import { getS3Data, putS3Data } from "../../../lib/s3";
import { v4 as uuidv4 } from "uuid";
import type { RegistryItem } from "../../../lib/types";
const REGISTRY_FILE_KEY = "baby-registry.json";
const REGISTRY_FILE_KEY = "registry.json";
// GET: List all registry items
export const GET: APIRoute = async () => {