Skip to content

Commit 1b66771

Browse files
committed
created endpoints
1 parent 1e188bd commit 1b66771

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import departmentRouter from "#routes/department";
2626
import paperRouter from "#routes/paper";
2727
import groupRouter from "#routes/group";
2828
import performarouter from "#routes/performance";
29+
import topicRouter from "#routes/topic";
2930

3031
const app = express();
3132
const currDirName = dirname(fileURLToPath(import.meta.url));
@@ -64,5 +65,6 @@ app.use("/group", groupRouter);
6465
app.use("/semester", semesterRouter);
6566
app.use("/faculty", facultyRouter);
6667
app.use("/performance", performarouter);
68+
app.use("topic",topicRouter);
6769

6870
export default app;

controller/topic.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {
2+
addNewTopic, deleteTopicById, updateTopicById, getTopics,
3+
} from "#services/topic";
4+
import { logger } from "#util";
5+
6+
async function addTopic(req, res) {
7+
const {
8+
title,
9+
} = req.body;
10+
try {
11+
// eslint-disable-next-line max-len
12+
const topic = await addNewTopic(title);
13+
res.json({ res: `added accreditation ${topic.name}`, id: topic.id });
14+
} catch (error) {
15+
logger.error("Error while inserting", error);
16+
res.status(500);
17+
res.json({ err: "Error while inserting in DB" });
18+
}
19+
}
20+
async function deleteTopic(req, res) {
21+
const { id } = req.params;
22+
try {
23+
await deleteTopicById(id);
24+
res.json({ res: "Topic deleted successfully" });
25+
} catch (error) {
26+
logger.error("Error while deleting", error);
27+
res.status(500);
28+
res.json({ err: "Error while deleting from DB" });
29+
}
30+
}
31+
32+
async function updateTopic(req, res) {
33+
const { id } = req.params;
34+
const {
35+
...data
36+
} = req.body;
37+
38+
try {
39+
await updateTopicById(id, data);
40+
res.json({ res: `${id} topic updated` });
41+
} catch (error) {
42+
logger.error("Error while inserting", error);
43+
res.status(500);
44+
res.json({ err: "Error while inserting in DB" });
45+
}
46+
}
47+
48+
async function showTopic(req, res) {
49+
try {
50+
const topic = await getTopics(req.query);
51+
return res.json({ res: topic });
52+
} catch (error) {
53+
logger.error("Error while fetching", error);
54+
res.status(500);
55+
return res.json({ err: "Error while fetching the data" });
56+
}
57+
}
58+
59+
export default {
60+
addTopic, updateTopic, deleteTopic, showTopic,
61+
};
62+

routes/topic.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import express from "express";
2+
import topicController from "#controller/topic";
3+
4+
const router = express.Router();
5+
router.get("/list", topicController.showTopic);
6+
router.post("/add", topicController.addTopic);
7+
router.delete("/delete/:id", topicController.deleteTopic);
8+
router.post("/update/:id", topicController.updateTopic);
9+
10+
export default router;

services/topic.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Topic from "#models/topic";
2+
import databaseError from "#error/database";
3+
4+
export async function addNewAopic(title) {
5+
const newTopic = await Topic.create({
6+
title,
7+
});
8+
if (newTopic.name === name) {
9+
return newAopic;
10+
}
11+
throw new databaseError.DataEntryError("Add Topic");
12+
}
13+
14+
export async function getTopics(filter) {
15+
const topics = await Topic.read(filter);
16+
if (topics) {
17+
return topics;
18+
}
19+
throw new databaseError.DataNotFoundError("Topic");
20+
}
21+
22+
export async function deleteTopicById(topicId) {
23+
const deleted = await Topic.remove({ _id: topicId });
24+
if (deleted) {
25+
return deleted;
26+
}
27+
throw new databaseError.DataDeleteError("Topic");
28+
}
29+
30+
export async function updateTopicById(id, data) {
31+
const updated = await Topic.update({ _id: id }, data);
32+
if (updated) {
33+
return updated;
34+
}
35+
throw new databaseError.DataEntryError("Topic");
36+
}

0 commit comments

Comments
 (0)