From ec382aa9fc7c267cac4d0a2e1fc978df14a8e772 Mon Sep 17 00:00:00 2001 From: Astha Singh Date: Fri, 1 Sep 2023 16:03:14 +0530 Subject: [PATCH] #273-endpoints-for-tutorials --- controller/tutorial.js | 60 ++++++++++++++++++++++++++++++++++++++++++ routes/tutorial.js | 10 +++++++ services/tutorial.js | 43 ++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 controller/tutorial.js create mode 100644 routes/tutorial.js create mode 100644 services/tutorial.js diff --git a/controller/tutorial.js b/controller/tutorial.js new file mode 100644 index 0000000..be0f093 --- /dev/null +++ b/controller/tutorial.js @@ -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, + }; \ No newline at end of file diff --git a/routes/tutorial.js b/routes/tutorial.js new file mode 100644 index 0000000..2c14a76 --- /dev/null +++ b/routes/tutorial.js @@ -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; \ No newline at end of file diff --git a/services/tutorial.js b/services/tutorial.js new file mode 100644 index 0000000..cbb881e --- /dev/null +++ b/services/tutorial.js @@ -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, +}; \ No newline at end of file