Skip to content

Commit

Permalink
#273-endpoints-for-tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
asthasingh182004 committed Sep 1, 2023
1 parent 6963dd3 commit ec382aa
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
60 changes: 60 additions & 0 deletions controller/tutorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
addNewTutorial, deleteTutorialById, updateTutorialById, getTutorials,
} from "#services/tutorial";
import { logger } from "#util";

async function addTutorial(req, res) {
const {
no, title, hours, cognitiveLevel,
} = req.body;
try {
// eslint-disable-next-line max-len
const tutorial = await addNewTutorial(no, title, hours, cognitiveLevel,);
res.json({ res: `added tutorial ${tutorial.name}` });
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}
async function deleteTutorial(req, res) {
const { tutorialId } = req.params;
try {
await deleteTutorialById(tutorialId);
res.json({ res: "Tutorial deleted successfully" });
} catch (error) {
logger.error("Error while deleting", error);
res.status(500);
res.json({ err: "Error while deleting from DB" });
}
}

async function updateTutorial(req, res) {
const {
id, ...data
} = req.body;

try {
await updateTutorialById(id, data);
res.json({ res: "tutorial updated" });
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}

async function showTutorial(req, res) {
try {
const tutorial = await getTutorials(req.query);
return res.json({ res: tutorial });
} catch (error) {
logger.error("Error while fetching", error);
res.status(500);
return res.json({ err: "Error while fetching the data" });
}
}

export default {
addTutorial, updateTutorial, deleteTutorial, showTutorial,
};
10 changes: 10 additions & 0 deletions routes/tutorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from "express";
import tutorialController from "#controller/tutorial";

const router = express.Router();
router.post("/add", tutorialController.addTutorial);
router.get("/list", tutorialController.getTutorial);
router.post("/update", tutorialController.updateTutorial);
router.post("/delete", tutorialController.deleteTutorial);

export default router;
43 changes: 43 additions & 0 deletions services/tutorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Tutorial from "#models/tutorial";
import databaseError from "#error/database";

export async function addNewTutorial(no, title, hours, cognitiveLevel,) {
const newTutorial = await Tutorial.create({
no,
title,
hours,
cognitiveLevel,
});
if (newTutorial.name === name) {
return newTutorial;
}
throw new databaseError.DataEntryError("Add Tutorial");
}

export async function gettutorials(filter) {
const tutorials = await Tutorial.read(filter);
if (tutorials) {
return tutorials;
}
throw new databaseError.DataNotFoundError("Tutorial");
}

export async function deleteTutorialById(tutorialId) {
const deleted = await Tutorial.remove({ _id: tutorialId });
if (deleted) {
return deleted;
}
throw new databaseError.DataDeleteError("Tutorial");
}

export async function updateTutorialById(id, data) {
const updated = await Tutorial.update({ _id: id }, data);
if (updated) {
return updated;
}
throw new databaseError.DataEntryError("Tutorial");
}

export default {
deleteTutorialById, addNewTutorial, updateTutorialById,
};

0 comments on commit ec382aa

Please sign in to comment.