|
1 | 1 | const { PrismaClient } = require("@prisma/client"); |
2 | 2 | const prisma = new PrismaClient(); |
| 3 | +const { asyncHandler, AppError } = require("../utills/errorHandler"); |
3 | 4 |
|
4 | | -async function createCategory(request, response) { |
5 | | - try { |
6 | | - const { name } = request.body; |
7 | | - const category = await prisma.category.create({ |
8 | | - data: { |
9 | | - name, |
10 | | - }, |
11 | | - }); |
12 | | - return response.status(201).json(category); |
13 | | - } catch (error) { |
14 | | - console.error("Error creating category:", error); |
15 | | - return response.status(500).json({ error: "Error creating category" }); |
| 5 | +const createCategory = asyncHandler(async (request, response) => { |
| 6 | + const { name } = request.body; |
| 7 | + |
| 8 | + if (!name || name.trim().length === 0) { |
| 9 | + throw new AppError("Category name is required", 400); |
| 10 | + } |
| 11 | + |
| 12 | + const category = await prisma.category.create({ |
| 13 | + data: { |
| 14 | + name: name.trim(), |
| 15 | + }, |
| 16 | + }); |
| 17 | + return response.status(201).json(category); |
| 18 | +}); |
| 19 | + |
| 20 | +const updateCategory = asyncHandler(async (request, response) => { |
| 21 | + const { id } = request.params; |
| 22 | + const { name } = request.body; |
| 23 | + |
| 24 | + if (!id) { |
| 25 | + throw new AppError("Category ID is required", 400); |
| 26 | + } |
| 27 | + |
| 28 | + if (!name || name.trim().length === 0) { |
| 29 | + throw new AppError("Category name is required", 400); |
| 30 | + } |
| 31 | + |
| 32 | + const existingCategory = await prisma.category.findUnique({ |
| 33 | + where: { |
| 34 | + id: id, |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + if (!existingCategory) { |
| 39 | + throw new AppError("Category not found", 404); |
| 40 | + } |
| 41 | + |
| 42 | + const updatedCategory = await prisma.category.update({ |
| 43 | + where: { |
| 44 | + id: existingCategory.id, |
| 45 | + }, |
| 46 | + data: { |
| 47 | + name: name.trim(), |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + return response.status(200).json(updatedCategory); |
| 52 | +}); |
| 53 | + |
| 54 | +const deleteCategory = asyncHandler(async (request, response) => { |
| 55 | + const { id } = request.params; |
| 56 | + |
| 57 | + if (!id) { |
| 58 | + throw new AppError("Category ID is required", 400); |
16 | 59 | } |
17 | | -} |
18 | | - |
19 | | -async function updateCategory(request, response) { |
20 | | - try { |
21 | | - const { id } = request.params; |
22 | | - const { name } = request.body; |
23 | | - |
24 | | - const existingCategory = await prisma.category.findUnique({ |
25 | | - where: { |
26 | | - id: id, |
27 | | - }, |
28 | | - }); |
29 | | - |
30 | | - if (!existingCategory) { |
31 | | - return response.status(404).json({ error: "Category not found" }); |
32 | | - } |
33 | | - |
34 | | - const updatedCategory = await prisma.category.update({ |
35 | | - where: { |
36 | | - id: existingCategory.id, |
37 | | - }, |
38 | | - data: { |
39 | | - name, |
40 | | - }, |
41 | | - }); |
42 | | - |
43 | | - return response.status(200).json(updatedCategory); |
44 | | - } catch (error) { |
45 | | - return response.status(500).json({ error: "Error updating category" }); |
| 60 | + |
| 61 | + const existingCategory = await prisma.category.findUnique({ |
| 62 | + where: { |
| 63 | + id: id, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + if (!existingCategory) { |
| 68 | + throw new AppError("Category not found", 404); |
46 | 69 | } |
47 | | -} |
48 | | - |
49 | | -async function deleteCategory(request, response) { |
50 | | - try { |
51 | | - const { id } = request.params; |
52 | | - await prisma.category.delete({ |
53 | | - where: { |
54 | | - id: id, |
55 | | - }, |
56 | | - }); |
57 | | - return response.status(204).send(); |
58 | | - } catch (error) { |
59 | | - console.log(error); |
60 | | - return response.status(500).json({ error: "Error deleting category" }); |
| 70 | + |
| 71 | + // Check if category has products |
| 72 | + const productsWithCategory = await prisma.product.findFirst({ |
| 73 | + where: { |
| 74 | + categoryId: id, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + if (productsWithCategory) { |
| 79 | + throw new AppError("Cannot delete category that has products", 400); |
61 | 80 | } |
62 | | -} |
63 | 81 |
|
64 | | -async function getCategory(request, response) { |
| 82 | + await prisma.category.delete({ |
| 83 | + where: { |
| 84 | + id: id, |
| 85 | + }, |
| 86 | + }); |
| 87 | + return response.status(204).send(); |
| 88 | +}); |
| 89 | + |
| 90 | +const getCategory = asyncHandler(async (request, response) => { |
65 | 91 | const { id } = request.params; |
| 92 | + |
| 93 | + if (!id) { |
| 94 | + throw new AppError("Category ID is required", 400); |
| 95 | + } |
| 96 | + |
66 | 97 | const category = await prisma.category.findUnique({ |
67 | 98 | where: { |
68 | 99 | id: id, |
69 | 100 | }, |
70 | 101 | }); |
| 102 | + |
71 | 103 | if (!category) { |
72 | | - return response.status(404).json({ error: "Category not found" }); |
| 104 | + throw new AppError("Category not found", 404); |
73 | 105 | } |
| 106 | + |
74 | 107 | return response.status(200).json(category); |
75 | | -} |
76 | | - |
77 | | -async function getAllCategories(request, response) { |
78 | | - try { |
79 | | - const categories = await prisma.category.findMany({}); |
80 | | - return response.json(categories); |
81 | | - } catch (error) { |
82 | | - return response.status(500).json({ error: "Error fetching categories" }); |
83 | | - } |
84 | | -} |
| 108 | +}); |
| 109 | + |
| 110 | +const getAllCategories = asyncHandler(async (request, response) => { |
| 111 | + const categories = await prisma.category.findMany({}); |
| 112 | + return response.json(categories); |
| 113 | +}); |
85 | 114 |
|
86 | 115 | module.exports = { |
87 | 116 | createCategory, |
|
0 commit comments