x
This commit is contained in:
1 parent
f5893c3fad
commit
570810acdb
8 files changed
+65875
-6
No files matched your search
@@ -217,9 +217,66 @@ export const productsRoute = new Elysia({ prefix: "/products" })
|
||||
.get("/brands", async () => {
|
||||
return await prisma.productBrand.findMany({
|
||||
orderBy: { name: "asc" },
|
||||
include: { translations: true }
|
||||
include: {
|
||||
translations: true,
|
||||
_count: { select: { products: true } }
|
||||
}
|
||||
});
|
||||
})
|
||||
.post("/brands", async ({ body, status }) => {
|
||||
const payload = body as any;
|
||||
const name = payload.name?.trim();
|
||||
if (!name) return status(400, { message: "Brand name is required" });
|
||||
|
||||
const existing = await prisma.productBrand.findUnique({ where: { name } });
|
||||
if (existing) return status(400, { message: "Brand name already exists" });
|
||||
|
||||
const brand = await prisma.productBrand.create({
|
||||
data: { name },
|
||||
include: {
|
||||
translations: true,
|
||||
_count: { select: { products: true } }
|
||||
}
|
||||
});
|
||||
return { success: true, brand };
|
||||
})
|
||||
.put("/brands/:id", async ({ params, body, status }) => {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) return status(400, { message: "Invalid brand ID" });
|
||||
|
||||
const payload = body as any;
|
||||
const name = payload.name?.trim();
|
||||
if (!name) return status(400, { message: "Brand name is required" });
|
||||
|
||||
const existing = await prisma.productBrand.findFirst({
|
||||
where: { name, NOT: { id } }
|
||||
});
|
||||
if (existing) return status(400, { message: "Brand name already in use by another brand" });
|
||||
|
||||
const brand = await prisma.productBrand.update({
|
||||
where: { id },
|
||||
data: { name },
|
||||
include: {
|
||||
translations: true,
|
||||
_count: { select: { products: true } }
|
||||
}
|
||||
});
|
||||
return { success: true, brand };
|
||||
})
|
||||
.delete("/brands/:id", async ({ params, status }) => {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) return status(400, { message: "Invalid brand ID" });
|
||||
|
||||
const count = await prisma.product.count({ where: { brandId: id } });
|
||||
if (count > 0) {
|
||||
return status(400, {
|
||||
message: `Cannot delete brand: ${count} product(s) are currently assigned to it. Please reassign them first.`
|
||||
});
|
||||
}
|
||||
|
||||
await prisma.productBrand.delete({ where: { id } });
|
||||
return { success: true, message: "Brand deleted successfully" };
|
||||
})
|
||||
.patch("/brands/:id/translations", async ({ params, body, status }) => {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) return status(400, { message: "Invalid brand ID" });
|
||||
@@ -678,6 +735,12 @@ export const productsRoute = new Elysia({ prefix: "/products" })
|
||||
await assertActiveCategory(categoryId, tx);
|
||||
updateData.categoryId = categoryId;
|
||||
}
|
||||
if (payload.brandId !== undefined) {
|
||||
const nextBrandId = Number(payload.brandId);
|
||||
const brandExists = await tx.productBrand.findUnique({ where: { id: nextBrandId } });
|
||||
if (!brandExists) throw new Error(`Brand with ID ${nextBrandId} not found.`);
|
||||
updateData.brandId = nextBrandId;
|
||||
}
|
||||
if (body.options !== undefined) updateData.options = body.options;
|
||||
if (body.titleGraphicUrl !== undefined) updateData.titleGraphicUrl = body.titleGraphicUrl || null;
|
||||
|
||||
|
||||
Reference in new issue
Block a user