x
This commit is contained in:
1 parent
6096c727d9
commit
3965f4f4da
38 files changed
+1513
-752
No files matched your search
@@ -164,6 +164,30 @@ export const categoriesRoute = new Elysia({ prefix: "/categories" })
|
||||
return status(500, { message: err?.message || "Failed to update translations" });
|
||||
}
|
||||
})
|
||||
.patch("/:id/image", async ({ params, body, status }) => {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) return status(400, { message: "Invalid category ID" });
|
||||
|
||||
try {
|
||||
const category = await prisma.category.update({
|
||||
where: { id },
|
||||
data: {
|
||||
imageUrl: body.imageUrl,
|
||||
},
|
||||
});
|
||||
return category;
|
||||
} catch (err: any) {
|
||||
if (err.code === "P2025") {
|
||||
return status(404, { message: "Category not found" });
|
||||
}
|
||||
console.error("Update category image error:", err);
|
||||
return status(500, { message: "Failed to update category image" });
|
||||
}
|
||||
}, {
|
||||
body: t.Object({
|
||||
imageUrl: t.Union([t.String(), t.Null()]),
|
||||
})
|
||||
})
|
||||
.post("/", async ({ body, status }) => {
|
||||
try {
|
||||
const category = await prisma.category.create({
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import prisma from "./src/lib/prisma";
|
||||
import { categoriesRoute } from "./src/routes/categories";
|
||||
|
||||
async function test() {
|
||||
console.log("Creating/Finding test category...");
|
||||
// Let's find or create a category
|
||||
let category = await prisma.category.findFirst({
|
||||
where: {
|
||||
name: "TestCategoryTemp"
|
||||
}
|
||||
});
|
||||
if (!category) {
|
||||
category = await prisma.category.create({
|
||||
data: {
|
||||
name: "TestCategoryTemp",
|
||||
translations: { create: { langCode: "th", name: "TestCategoryTemp" } }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const id = category.id;
|
||||
console.log(`Using category ID: ${id}, current imageUrl: ${category.imageUrl}`);
|
||||
|
||||
// Test updating image URL to a new value
|
||||
console.log("Updating imageUrl to 'https://example.com/test.png'...");
|
||||
const updateRes = await categoriesRoute.handle(
|
||||
new Request(`http://localhost/categories/${id}/image`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ imageUrl: "https://example.com/test.png" })
|
||||
})
|
||||
);
|
||||
|
||||
console.log("PATCH status:", updateRes.status);
|
||||
const updatedCategory = await updateRes.json();
|
||||
console.log("PATCH response:", updatedCategory);
|
||||
|
||||
if (updatedCategory.imageUrl !== "https://example.com/test.png") {
|
||||
throw new Error("imageUrl was not updated correctly!");
|
||||
}
|
||||
|
||||
// Test setting imageUrl to null
|
||||
console.log("Updating imageUrl to null...");
|
||||
const nullRes = await categoriesRoute.handle(
|
||||
new Request(`http://localhost/categories/${id}/image`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ imageUrl: null })
|
||||
})
|
||||
);
|
||||
|
||||
console.log("PATCH null status:", nullRes.status);
|
||||
const nulledCategory = await nullRes.json();
|
||||
console.log("PATCH null response:", nulledCategory);
|
||||
|
||||
if (nulledCategory.imageUrl !== null) {
|
||||
throw new Error("imageUrl was not nulled correctly!");
|
||||
}
|
||||
|
||||
// Clean up if we created the temp category
|
||||
if (category.name === "TestCategoryTemp") {
|
||||
await prisma.category.delete({ where: { id } });
|
||||
console.log("Cleaned up temp category.");
|
||||
}
|
||||
|
||||
console.log("All PATCH tests passed successfully!");
|
||||
}
|
||||
|
||||
test().catch(console.error);
|
||||
Reference in new issue
Block a user