Skip to content

Commit 9d9c399

Browse files
Merge pull request #310 from tcet-opensource/#275-apidocs-for-tutorials
#275 apidocs for tutorials
2 parents 9758649 + a3622d0 commit 9d9c399

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

_apidoc.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,76 @@
309309
* @apiSuccess {Date} accreditation.dateofAccreditation Date on which accreditation was issued.
310310
* @apiSuccess {Date} accreditation.dateofExpiry Date till which accreditation is valid.
311311
*/
312+
//------------------------------------------------------------------------------------------
313+
// Tutorials.
314+
// ------------------------------------------------------------------------------------------
315+
316+
/**
317+
* @api {post} /tutorial/add Add Tutorial
318+
* @apiName AddTutorial
319+
* @apiGroup Tutorial
320+
*
321+
* @apiBody {Number} no The number of tutorial.
322+
* @apiBody {String} title The title of tutorial.
323+
* @apiBody {Number} hours The hours required for tutorial .
324+
* @apiBody {String} cognitiveLevel The cognitiveLvel of tutorial.
325+
326+
*
327+
* @apiSuccess {String} res Success message with the ID of the added tutorial.
328+
*
329+
* @apiError (Error 500) DatabaseError Error while inserting in the database.
330+
*
331+
* @apiDescription Adds a new tutorial to the system.
332+
*/
333+
334+
/**
335+
* @api {get} tutorial/list Get Tutorial List
336+
* @apiName GetTutorial
337+
* @apiGroup Tutorial
338+
*
339+
* @apiQuery {Number} [no] Number of Tutorial.
340+
* @apiQuery {String} [title] Title of Tutorial.
341+
* @apiQuery {Number} [hours] Hours required for Tutorial
342+
* @apiQuery {String} [cognitiveLevel] Level of Tutorial.
343+
344+
*
345+
* @apiSuccess {Tutorial[]} res Array of Filtered Tutorial Doc .
346+
* @apiSuccess {String} tutorial._id ID of document given by database.
347+
* @apiSuccess {Number} tutorial.no Number of Tutorial.
348+
* @apiSuccess {String} tutorial.title Title of Tutorial.
349+
* @apiSuccess {String} tutorial.hours Hours of Tutorial.
350+
* @apiSuccess {Number} tutorial.cognitiveLevel CognitiveLevel of Tutorial.
351+
*/
352+
353+
/**
354+
* @api {delete} /tutorial/delete/:tutorialId Delete Tutorial
355+
* @apiName DeleteTutorial,
356+
* @apiGroup Tutorial
357+
*
358+
* @apiParam {String} tutorialId The ID of the tutorial document to delete.
359+
*
360+
* @apiSuccess {String} res Success message indicating the deletion.
361+
*
362+
* @apiError (Error 500) err Error message if there was an error during the deletion.
363+
*
364+
* */
365+
/**
366+
* @api {post} /tutorial/update Update tutorial details
367+
* @apiName UpdateTutorial
368+
* @apiGroup Tutorial
369+
* @apiDescription update Existing Tutorial details
370+
*
371+
* @apiBody {String} id Id of the tutorial to be updated
372+
* @apiBody {Number} [no] The no of tutorial.
373+
* @apiBody {String} [title] The title of tutorial.
374+
* @apiBody {String} [hours] The hours required for the tutorial.
375+
* @apiBody {Number} [cognitiveLevel] The cognitiveLevel of tutorial.
376+
377+
*
378+
* @apiSuccess {String} res tutorial updated.
379+
* @apiError (Error 500) err Error in updating database
380+
*
381+
*/
312382

313383
// ------------------------------------------------------------------------------------------
314384
// Timetable.

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import usersRouter from "#routes/users";
1010
import authRouter from "#routes/auth";
1111
import accreditationRouter from "#routes/accreditation";
1212
import infrastructureRouter from "#routes/infrastructure";
13+
import tutorialRouter from "#routes/tutorial";
1314
import assignmentRouter from "#routes/assignment";
1415
import timetableRouter from "#routes/timetable";
1516
import courseworkRouter from "#routes/coursework";
@@ -38,6 +39,7 @@ app.use("/users", usersRouter);
3839
app.use("/auth", authRouter);
3940
app.use("/accreditation", accreditationRouter);
4041
app.use("/infrastructure", infrastructureRouter);
42+
app.use("/tutorial", tutorialRouter);
4143
app.use("/assignment", assignmentRouter);
4244
app.use("/timetable", timetableRouter);
4345
app.use("/department", departmentRouter);

controller/tutorial.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {
2+
addNewTutorial, deleteTutorialById, updateTutorialById, getTutorials,
3+
} from "#services/tutorial";
4+
import { logger } from "#util";
5+
6+
async function addTutorial(req, res) {
7+
const {
8+
no, title, hours, cognitiveLevel,
9+
} = req.body;
10+
try {
11+
// eslint-disable-next-line max-len
12+
const tutorial = await addNewTutorial(no, title, hours, cognitiveLevel,);
13+
res.json({ res: `added tutorial ${tutorial.name}` });
14+
} catch (error) {
15+
logger.error("Error while inserting", error);
16+
res.status(500);
17+
res.json({ err: "Error while inserting in DB" });
18+
}
19+
}
20+
async function deleteTutorial(req, res) {
21+
const { tutorialId } = req.params;
22+
try {
23+
await deleteTutorialById(tutorialId);
24+
res.json({ res: "Tutorial deleted successfully" });
25+
} catch (error) {
26+
logger.error("Error while deleting", error);
27+
res.status(500);
28+
res.json({ err: "Error while deleting from DB" });
29+
}
30+
}
31+
32+
async function updateTutorial(req, res) {
33+
const {
34+
id, ...data
35+
} = req.body;
36+
37+
try {
38+
await updateTutorialById(id, data);
39+
res.json({ res: "tutorial updated" });
40+
} catch (error) {
41+
logger.error("Error while inserting", error);
42+
res.status(500);
43+
res.json({ err: "Error while inserting in DB" });
44+
}
45+
}
46+
47+
async function showTutorial(req, res) {
48+
try {
49+
const tutorial = await getTutorials(req.query);
50+
return res.json({ res: tutorial });
51+
} catch (error) {
52+
logger.error("Error while fetching", error);
53+
res.status(500);
54+
return res.json({ err: "Error while fetching the data" });
55+
}
56+
}
57+
58+
export default {
59+
addTutorial, updateTutorial, deleteTutorial, showTutorial,
60+
};

routes/tutorial.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import express from "express";
2+
import tutorialController from "#controller/tutorial";
3+
4+
const router = express.Router();
5+
router.post("/add", tutorialController.addTutorial);
6+
router.get("/list", tutorialController.showTutorial);
7+
router.post("/update", tutorialController.updateTutorial);
8+
router.post("/delete", tutorialController.deleteTutorial);
9+
10+
export default router;

services/tutorial.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Tutorial from "#models/tutorial";
2+
import databaseError from "#error/database";
3+
4+
export async function addNewTutorial(no, title, hours, cognitiveLevel,) {
5+
const newTutorial = await Tutorial.create({
6+
no,
7+
title,
8+
hours,
9+
cognitiveLevel,
10+
});
11+
if (newTutorial.name === name) {
12+
return newTutorial;
13+
}
14+
throw new databaseError.DataEntryError("Add Tutorial");
15+
}
16+
17+
export async function getTutorials(filter) {
18+
const tutorials = await Tutorial.read(filter);
19+
if (tutorials) {
20+
return tutorials;
21+
}
22+
throw new databaseError.DataNotFoundError("Tutorial");
23+
}
24+
25+
export async function deleteTutorialById(tutorialId) {
26+
const deleted = await Tutorial.remove({ _id: tutorialId });
27+
if (deleted) {
28+
return deleted;
29+
}
30+
throw new databaseError.DataDeleteError("Tutorial");
31+
}
32+
33+
export async function updateTutorialById(id, data) {
34+
const updated = await Tutorial.update({ _id: id }, data);
35+
if (updated) {
36+
return updated;
37+
}
38+
throw new databaseError.DataEntryError("Tutorial");
39+
}
40+
41+
export default {
42+
deleteTutorialById, addNewTutorial, updateTutorialById, getTutorials,
43+
};

0 commit comments

Comments
 (0)