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

#275 apidocs for tutorials #309

Closed
wants to merge 3 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
70 changes: 70 additions & 0 deletions _apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*/
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
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, getTutorials,
};
Loading