Skip to content

Commit 2bbaafb

Browse files
committed
feat: implement document management with CRUD operations
1 parent b4bf6ce commit 2bbaafb

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

src/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { frontendUrl } from "./config";
66
import projectRoutes from "./routes/project.routes";
77
import userRoutes from "./routes/user.routes";
88
import documentCategoryRoutes from "./routes/document-category.routes";
9+
import documentRoutes from "./routes/document.routes";
910
import { auth } from "./middleware/auth.middleware";
1011

1112
const app = express();
@@ -26,5 +27,6 @@ app.use(auth);
2627
app.use("/api/users", userRoutes);
2728
app.use("/api/projects", projectRoutes);
2829
app.use("/api/document-categories", documentCategoryRoutes);
30+
app.use("/api/documents", documentRoutes);
2931

3032
export default app;
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import { Request, Response, NextFunction } from "express";
2+
import prisma from "../db/prisma";
3+
import * as response from "../utils/response";
4+
5+
export const createDocument = async (
6+
req: Request,
7+
res: Response,
8+
next: NextFunction
9+
): Promise<void> => {
10+
try {
11+
const {
12+
title,
13+
description,
14+
link,
15+
tags,
16+
visibility,
17+
createdById,
18+
categoryId,
19+
} = req.body;
20+
21+
const category = await prisma.documentCategory.findUnique({
22+
where: { id: categoryId },
23+
});
24+
25+
if (!category) {
26+
return response.errorResponse(res, "Document category not found.");
27+
}
28+
29+
const document = await prisma.document.create({
30+
data: {
31+
title,
32+
description,
33+
link,
34+
tags,
35+
visibility,
36+
createdById,
37+
categoryId,
38+
},
39+
});
40+
41+
return response.successResponse(
42+
res,
43+
"Document created successfully.",
44+
document
45+
);
46+
} catch (error) {
47+
console.error(error);
48+
return response.errorResponse(res, "Internal server error.");
49+
}
50+
};
51+
52+
export const getDocumentsByCategory = async (
53+
req: Request,
54+
res: Response,
55+
next: NextFunction
56+
): Promise<void> => {
57+
try {
58+
const { categoryId } = req.params;
59+
60+
const documents = await prisma.document.findMany({
61+
where: { categoryId },
62+
orderBy: { createdAt: "desc" },
63+
});
64+
65+
if (documents.length === 0) {
66+
return response.errorResponse(
67+
res,
68+
"No documents found in this category."
69+
);
70+
}
71+
72+
return response.successResponse(res, "Documents fetched successfully.", {
73+
data: documents,
74+
});
75+
} catch (error) {
76+
console.error(error);
77+
return response.errorResponse(res, "Internal server error.");
78+
}
79+
};
80+
81+
export const getDocumentById = async (
82+
req: Request,
83+
res: Response,
84+
next: NextFunction
85+
): Promise<void> => {
86+
try {
87+
const { id } = req.params;
88+
89+
const document = await prisma.document.findUnique({
90+
where: { id },
91+
});
92+
93+
if (!document) {
94+
return response.errorResponse(res, "Document not found.");
95+
}
96+
97+
return response.successResponse(
98+
res,
99+
"Document fetched successfully.",
100+
document
101+
);
102+
} catch (error) {
103+
console.error(error);
104+
return response.errorResponse(res, "Internal server error.");
105+
}
106+
};
107+
108+
export const updateDocument = async (
109+
req: Request,
110+
res: Response,
111+
next: NextFunction
112+
): Promise<void> => {
113+
try {
114+
const { id } = req.params;
115+
const { title, description, link, tags, visibility, categoryId } = req.body;
116+
117+
const existingDocument = await prisma.document.findUnique({
118+
where: { id },
119+
});
120+
121+
if (!existingDocument) {
122+
return response.errorResponse(res, "Document not found.");
123+
}
124+
125+
const category = await prisma.documentCategory.findUnique({
126+
where: { id: categoryId },
127+
});
128+
129+
if (!category) {
130+
return response.errorResponse(res, "Document category not found.");
131+
}
132+
133+
const updatedDocument = await prisma.document.update({
134+
where: { id },
135+
data: {
136+
title,
137+
description,
138+
link,
139+
tags,
140+
visibility,
141+
categoryId,
142+
},
143+
});
144+
145+
return response.successResponse(
146+
res,
147+
"Document updated successfully.",
148+
updatedDocument
149+
);
150+
} catch (error) {
151+
console.error(error);
152+
return response.errorResponse(res, "Internal server error.");
153+
}
154+
};
155+
156+
export const deleteDocument = async (
157+
req: Request,
158+
res: Response,
159+
next: NextFunction
160+
): Promise<void> => {
161+
try {
162+
const { id } = req.params;
163+
164+
const existingDocument = await prisma.document.findUnique({
165+
where: { id },
166+
});
167+
168+
if (!existingDocument) {
169+
return response.errorResponse(res, "Document not found.");
170+
}
171+
172+
await prisma.document.delete({
173+
where: { id },
174+
});
175+
176+
return response.successResponse(res, "Document deleted successfully.");
177+
} catch (error) {
178+
console.error(error);
179+
return response.errorResponse(res, "Internal server error.");
180+
}
181+
};

src/routes/document.routes.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import express from "express";
2+
import {
3+
createDocument,
4+
getDocumentsByCategory,
5+
getDocumentById,
6+
updateDocument,
7+
deleteDocument,
8+
} from "../controllers/document.controller";
9+
10+
const router = express.Router();
11+
12+
router.post("/documents", createDocument);
13+
router.get("/documents/category/:categoryId", getDocumentsByCategory);
14+
router.get("/documents/:id", getDocumentById);
15+
router.put("/documents/:id", updateDocument);
16+
router.delete("/documents/:id", deleteDocument);
17+
18+
export default router;

0 commit comments

Comments
 (0)