-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6963dd3
commit ec382aa
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |