Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
62 changes: 62 additions & 0 deletions controller/topic.js
Original file line number Diff line number Diff line change
@@ -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,
};

10 changes: 10 additions & 0 deletions routes/topic.js
Original file line number Diff line number Diff line change
@@ -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;
36 changes: 36 additions & 0 deletions services/topic.js
Original file line number Diff line number Diff line change
@@ -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");
}