Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

254 all endpoints for topicss #404

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ This ERP system will be used to manage various aspects of the operations of coll

- **Technology stack**: We will be using node.js and express for the backend along with NoSQL, that is, MongoDB as the database due to the modular nature of this project.

## Hacktoberfest
### Powered by:
<img width="311" alt="mlh-logo-color" src="https://github.com/tcet-opensource/hacktober-fest/assets/55846983/d5ccae96-86a7-4fed-8f00-e9f1d0aa5cac">

### How to contribute
Read our [workflow](https://opensource.tcetmumbai.in/docs/resources/workflows/external-workflow/) guide, and have a look at issues marked with the <code>Hacktoberfest</code> tag on it. Do not forget to read the rest of the README. For serious doubts, contact the project maintainers on our discord server.

## Dependencies

All the dependencies used in this project will be listed in the `package.json` file from the root directory.
Expand Down
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;
61 changes: 61 additions & 0 deletions controller/topic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 === title) {
return newTopic;
}
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");
}