From ec382aa9fc7c267cac4d0a2e1fc978df14a8e772 Mon Sep 17 00:00:00 2001 From: Astha Singh Date: Fri, 1 Sep 2023 16:03:14 +0530 Subject: [PATCH 1/4] #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 00000000..be0f0939 --- /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 00000000..2c14a762 --- /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 00000000..cbb881ec --- /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 From c223f99813752dd4e2a05e38f3cf5acf354ee191 Mon Sep 17 00:00:00 2001 From: Astha Singh Date: Sat, 2 Sep 2023 23:33:15 +0530 Subject: [PATCH 2/4] Added-getTutorials-to-services --- app.js | 2 ++ services/tutorial.js | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 769f3985..dd98d6b2 100644 --- a/app.js +++ b/app.js @@ -10,6 +10,7 @@ import usersRouter from "#routes/users"; import authRouter from "#routes/auth"; import accreditationRouter from "#routes/accreditation"; import infrastructureRouter from "#routes/infrastructure"; +import tutorialRouter from "#models/tutorial"; import { identifyUser } from "#middleware/identifyUser"; const app = express(); @@ -33,5 +34,6 @@ app.use("/users", usersRouter); app.use("/auth", authRouter); app.use("/accreditation", accreditationRouter); app.use("/infrastructure", infrastructureRouter); +app.use("/tutorial", tutorialRouter); export default app; diff --git a/services/tutorial.js b/services/tutorial.js index cbb881ec..3c7b258e 100644 --- a/services/tutorial.js +++ b/services/tutorial.js @@ -14,7 +14,7 @@ export async function addNewTutorial(no, title, hours, cognitiveLevel,) { throw new databaseError.DataEntryError("Add Tutorial"); } -export async function gettutorials(filter) { +export async function getTutorials(filter) { const tutorials = await Tutorial.read(filter); if (tutorials) { return tutorials; @@ -39,5 +39,5 @@ export async function updateTutorialById(id, data) { } export default { - deleteTutorialById, addNewTutorial, updateTutorialById, + deleteTutorialById, addNewTutorial, updateTutorialById, getTutorials, }; \ No newline at end of file From cc81a4bc129a526c599b1e4f3b300a40fe2506ba Mon Sep 17 00:00:00 2001 From: Astha Singh Date: Sat, 2 Sep 2023 23:50:41 +0530 Subject: [PATCH 3/4] added-apidocs-for-tutorials --- _apidoc.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/_apidoc.js b/_apidoc.js index e629aba8..15c72aee 100644 --- a/_apidoc.js +++ b/_apidoc.js @@ -309,3 +309,73 @@ * @apiSuccess {Date} accreditation.dateofAccreditation Date on which accreditation was issued. * @apiSuccess {Date} accreditation.dateofExpiry Date till which accreditation is valid. */ +//------------------------------------------------------------------------------------------ +// Tutorials. +// ------------------------------------------------------------------------------------------ + +/** + * @api {post} /tutorial/add Add Tutorial + * @apiName AddTutorial + * @apiGroup Tutorial + * + * @apiBody {Number} no The number of tutorial. + * @apiBody {String} title The title of tutorial. + * @apiBody {Number} hours The hours required for tutorial . + * @apiBody {String} cognitiveLevel The cognitiveLvel of tutorial. + + * + * @apiSuccess {String} res Success message with the ID of the added tutorial. + * + * @apiError (Error 500) DatabaseError Error while inserting in the database. + * + * @apiDescription Adds a new tutorial to the system. + */ + +/** + * @api {get} tutorial/list Get Tutorial List + * @apiName GetTutorial + * @apiGroup Tutorial + * + * @apiQuery {Number} [no] Number of Tutorial. + * @apiQuery {String} [title] Title of Tutorial. + * @apiQuery {Number} [hours] Hours required for Tutorial + * @apiQuery {String} [cognitiveLevel] Level of Tutorial. + + * + * @apiSuccess {Tutorial[]} res Array of Filtered Tutorial Doc . + * @apiSuccess {String} tutorial._id ID of document given by database. + * @apiSuccess {Number} tutorial.no Number of Tutorial. + * @apiSuccess {String} tutorial.title Title of Tutorial. + * @apiSuccess {String} tutorial.hours Hours of Tutorial. + * @apiSuccess {Number} tutorial.cognitiveLevel CognitiveLevel of Tutorial. + */ + +/** + * @api {delete} /tutorial/delete/:tutorialId Delete Tutorial + * @apiName DeleteTutorial, + * @apiGroup Tutorial + * + * @apiParam {String} tutorialId The ID of the tutorial document to delete. + * + * @apiSuccess {String} res Success message indicating the deletion. + * + * @apiError (Error 500) err Error message if there was an error during the deletion. + * +* */ +/** + * @api {post} /tutorial/update Update tutorial details + * @apiName UpdateTutorial + * @apiGroup Tutorial + * @apiDescription update Existing Tutorial details + * + * @apiBody {String} id Id of the tutorial to be updated + * @apiBody {Number} [no] The no of tutorial. + * @apiBody {String} [title] The title of tutorial. + * @apiBody {String} [hours] The hours required for the tutorial. + * @apiBody {Number} [cognitiveLevel] The cognitiveLevel of tutorial. + + * + * @apiSuccess {String} res tutorial updated. + * @apiError (Error 500) err Error in updating database + * + */ \ No newline at end of file From fea966d77c09cdbb5a73b5f27b2ecc1a53a2af50 Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:40:53 +0530 Subject: [PATCH 4/4] fixed code to pass testcases --- app.js | 2 +- routes/tutorial.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index dd98d6b2..74d215f8 100644 --- a/app.js +++ b/app.js @@ -10,7 +10,7 @@ import usersRouter from "#routes/users"; import authRouter from "#routes/auth"; import accreditationRouter from "#routes/accreditation"; import infrastructureRouter from "#routes/infrastructure"; -import tutorialRouter from "#models/tutorial"; +import tutorialRouter from "#routes/tutorial"; import { identifyUser } from "#middleware/identifyUser"; const app = express(); diff --git a/routes/tutorial.js b/routes/tutorial.js index 2c14a762..8f7d3d78 100644 --- a/routes/tutorial.js +++ b/routes/tutorial.js @@ -3,7 +3,7 @@ import tutorialController from "#controller/tutorial"; const router = express.Router(); router.post("/add", tutorialController.addTutorial); -router.get("/list", tutorialController.getTutorial); +router.get("/list", tutorialController.showTutorial); router.post("/update", tutorialController.updateTutorial); router.post("/delete", tutorialController.deleteTutorial);