diff --git a/app.js b/app.js index a595584..74aed8d 100644 --- a/app.js +++ b/app.js @@ -26,6 +26,7 @@ import departmentRouter from "#routes/department"; import paperRouter from "#routes/paper"; import groupRouter from "#routes/group"; import performarouter from "#routes/performance"; +import topicRouter from "#routes/topic"; const app = express(); const currDirName = dirname(fileURLToPath(import.meta.url)); @@ -64,5 +65,6 @@ app.use("/group", groupRouter); app.use("/semester", semesterRouter); app.use("/faculty", facultyRouter); app.use("/performance", performarouter); +app.use("topic",topicRouter); export default app; diff --git a/controller/topic.js b/controller/topic.js new file mode 100644 index 0000000..1406744 --- /dev/null +++ b/controller/topic.js @@ -0,0 +1,62 @@ +import { + addNewTopic, deleteTopicById, updateTopicById, getTopics, + } from "#services/topic"; + import { logger } from "#util"; + + async function addTopic(req, res) { + const { + title, + } = req.body; + try { + // eslint-disable-next-line max-len + const topic = await addNewTopic(title); + res.json({ res: `added accreditation ${topic.name}`, id: topic.id }); + } catch (error) { + logger.error("Error while inserting", error); + res.status(500); + res.json({ err: "Error while inserting in DB" }); + } + } + async function deleteTopic(req, res) { + const { id } = req.params; + try { + await deleteTopicById(id); + res.json({ res: "Topic deleted successfully" }); + } catch (error) { + logger.error("Error while deleting", error); + res.status(500); + res.json({ err: "Error while deleting from DB" }); + } + } + + async function updateTopic(req, res) { + const { id } = req.params; + const { + ...data + } = req.body; + + try { + await updateTopicById(id, data); + res.json({ res: `${id} topic updated` }); + } catch (error) { + logger.error("Error while inserting", error); + res.status(500); + res.json({ err: "Error while inserting in DB" }); + } + } + + async function showTopic(req, res) { + try { + const topic = await getTopics(req.query); + return res.json({ res: topic }); + } catch (error) { + logger.error("Error while fetching", error); + res.status(500); + return res.json({ err: "Error while fetching the data" }); + } + } + + export default { + addTopic, updateTopic, deleteTopic, showTopic, + }; + \ No newline at end of file diff --git a/routes/topic.js b/routes/topic.js new file mode 100644 index 0000000..268e070 --- /dev/null +++ b/routes/topic.js @@ -0,0 +1,10 @@ +import express from "express"; +import topicController from "#controller/topic"; + +const router = express.Router(); +router.get("/list", topicController.showTopic); +router.post("/add", topicController.addTopic); +router.delete("/delete/:id", topicController.deleteTopic); +router.post("/update/:id", topicController.updateTopic); + +export default router; diff --git a/services/topic.js b/services/topic.js new file mode 100644 index 0000000..53c2c29 --- /dev/null +++ b/services/topic.js @@ -0,0 +1,36 @@ +import Topic from "#models/topic"; +import databaseError from "#error/database"; + +export async function addNewTopic(title) { + const newTopic = await Topic.create({ + title, + }); + if (newTopic.name === name) { + return newAopic; + } + throw new databaseError.DataEntryError("Add Topic"); +} + +export async function getTopics(filter) { + const topics = await Topic.read(filter); + if (topics) { + return topics; + } + throw new databaseError.DataNotFoundError("Topic"); +} + +export async function deleteTopicById(topicId) { + const deleted = await Topic.remove({ _id: topicId }); + if (deleted) { + return deleted; + } + throw new databaseError.DataDeleteError("Topic"); +} + +export async function updateTopicById(id, data) { + const updated = await Topic.update({ _id: id }, data); + if (updated) { + return updated; + } + throw new databaseError.DataEntryError("Topic"); +}