Skip to content

Commit 9b04609

Browse files
Merge pull request #359 from tcet-opensource/317_CRUD_endpoint_apidocs_testcase_Paper
317 endpoint testcase apidoc of paper
2 parents 62a0673 + 4e30d10 commit 9b04609

File tree

7 files changed

+315
-1
lines changed

7 files changed

+315
-1
lines changed

_apidoc.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,64 @@
666666
* attainment as per Bloom's Taxanomy (L1-L6).
667667
*/
668668

669+
// ------------------------------------------------------------------------------------------
670+
// Paper.
671+
// ------------------------------------------------------------------------------------------
672+
//
673+
/**
674+
* @api {post} /paper/add Add Paper
675+
* @apiName AddPaper
676+
* @apiDescription Adds a new Paper.
677+
* @apiGroup Paper
678+
*
679+
* @apiBody {String} [answersheetID] The id of the Answersheet.
680+
* @apiBody {connector.Schema.Types.ObjectId} Exam The Exam which is associated.
681+
* @apiBody {connector.Schema.Types.ObjectId} Student The Student which is associated.
682+
* @apiBody {connector.Schema.Types.ObjectId} Faculty The Faculty which is associated.
683+
* @apiBody {Number} [marks] marks in the paper.
684+
*
685+
* @apiSuccess {String} res added Paper successfully.
686+
*
687+
* @apiError (Error 500) DatabaseError Error while inserting in the DB.
688+
*
689+
*/
690+
691+
/**
692+
* @api {get} /paper/list Listdown Paper
693+
* @apiName GetPaper
694+
* @apiDescription Listdown the Paper.
695+
* @apiGroup Paper
696+
*
697+
* @apiQuery {String} [answersheetID] The id of the Answersheet.
698+
* @apiQuery {connector.Schema.Types.ObjectId} Exam The Exam which is associated.
699+
* @apiQuery {connector.Schema.Types.ObjectId} Student The Student which is associated.
700+
* @apiQuery {connector.Schema.Types.ObjectId} Faculty The Faculty which is associated.
701+
* @apiQuery {Number} [marks] marks in the paper.
702+
*
703+
* @apiSuccess {Paper[]} res Array of Filtered Paper Doc.
704+
* @apiSuccess {String} paper._id ID of paper given by database.
705+
* @apiSuccess {String} paper.answersheetID ID of answersheet.
706+
* @apiSuccess {connector.Schema.Types.ObjectId} paper.exam associated Exam.
707+
* @apiSuccess {connector.Schema.Types.ObjectId} paper.student associated Student.
708+
* @apiSuccess {connector.Schema.Types.ObjectId} paper.faculty associated Faculty.
709+
* @apiSuccess {Number} paper.marks The marks in the Paper.
710+
* @apiError (Error 500) err Error while fetching the data.
711+
*/
712+
713+
/**
714+
* @api {delete} /paper/delete/:id Delete Paper
715+
* @apiName DeletePaper
716+
* @apiDescription Remove the existing Paper.
717+
* @apiGroup Paper
718+
*
719+
* @apiParam {String} answersheetID The ID of the answersheet to delete.
720+
*
721+
* @apiSuccess {String} res Paper deleted successfully.
722+
*
723+
* @apiError (Error 500) err Error while deleting from DB.
724+
*
725+
* */
726+
669727
// ------------------------------------------------------------------------------------------
670728
// Assignment.
671729
// ------------------------------------------------------------------------------------------
@@ -836,6 +894,25 @@
836894
*
837895
* */
838896

897+
/**
898+
* @api {post} /paper/update/ Update Paper
899+
* @apiName UpdatePaper
900+
* @apiGroup Paper
901+
* @apiDescription Update Existing Paper details except
902+
*
903+
* @apiSuccess {Paper[]} res Array of Filtered Paper Doc .
904+
* @apiSuccess {String} paper._id ID of paper given by database.
905+
* @apiSuccess {String} paper.answersheetID Name of Infrastructure
906+
* @apiSuccess {connector.Schema.Types.ObjectId} paper.exam associated Exam.
907+
* @apiSuccess {connector.Schema.Types.ObjectId} paper.student associated Student.
908+
* @apiSuccess {connector.Schema.Types.ObjectId} paper.faculty associated Faculty.
909+
* @apiSuccess {Number} paper.marks The marks in the Paper.
910+
*
911+
* @apiSuccess {String} res Paper updated successfully.
912+
*
913+
* @apiError (Error 500) err Error while updating the data.s
914+
*/
915+
839916
/**
840917
* @api {post} /assignment/update update assignment details
841918
* @apiName UpdateAssignment

app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import moduleRouter from "#routes/module";
2323
import facultyRouter from "#routes/faculty";
2424
import { identifyUser } from "#middleware/identifyUser";
2525
import departmentRouter from "#routes/department";
26+
import paperRouter from "#routes/paper";
2627
import groupRouter from "#routes/group";
2728

2829
const app = express();
@@ -57,7 +58,9 @@ app.use("/timetable", timetableRouter);
5758
app.use("/department", departmentRouter);
5859
app.use("/coursework", courseworkRouter);
5960
app.use("/module", moduleRouter);
61+
app.use("/paper", paperRouter);
6062
app.use("/group", groupRouter);
6163
app.use("/semester", semesterRouter);
6264
app.use("/faculty", facultyRouter);
65+
6366
export default app;

controller/paper.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
createPaper, updatePaperById, listPaper, deletePaperById,
3+
} from "#services/paper";
4+
import { logger } from "#util";
5+
6+
async function addPaper(req, res) {
7+
const {
8+
answerSheetID,
9+
exam,
10+
student,
11+
checkedBy,
12+
mark,
13+
} = req.body;
14+
try {
15+
const newPaper = await createPaper(
16+
answerSheetID,
17+
exam,
18+
student,
19+
checkedBy,
20+
mark,
21+
);
22+
res.json({ res: `added paper for: ${newPaper.answerSheetID}`, id: newPaper.id });
23+
} catch (error) {
24+
logger.error("Error while inserting", error);
25+
res.status(500);
26+
res.json({ err: "Error while inserting in DB" });
27+
}
28+
}
29+
30+
async function updatePaper(req, res) {
31+
const { id } = req.params;
32+
const {
33+
...data
34+
} = req.body;
35+
try {
36+
await updatePaperById(id, data);
37+
res.json({ res: "Paper updated" });
38+
} catch (error) {
39+
logger.error("Error while updating", error);
40+
res.status(500);
41+
res.json({ err: "Error while updating in DB" });
42+
}
43+
}
44+
45+
async function showPaper(req, res) {
46+
const filter = req.query;
47+
const paper = await listPaper(filter);
48+
res.json({ res: paper });
49+
}
50+
51+
async function deletePaper(req, res) {
52+
const { id } = req.params;
53+
try {
54+
await deletePaperById(id);
55+
res.json({ res: `Deleted paper with ID ${id}` });
56+
} catch (error) {
57+
logger.error("Error while deleting", error);
58+
res.status(500).json({ error: "Error while deleting from DB" });
59+
}
60+
}
61+
62+
export default {
63+
addPaper, updatePaper, showPaper, deletePaper,
64+
};

models/paper.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const paperSchema = {
88
mark: { type: Number, required: true },
99
};
1010

11-
// eslint-disable-next-line no-unused-vars
1211
const Paper = connector.model("Paper", paperSchema);
1312

1413
// CRUD OPERATIONS

routes/paper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import express from "express";
2+
import paperController from "#controller/paper";
3+
4+
const router = express.Router();
5+
6+
router.post("/add", paperController.addPaper);
7+
router.get("/list", paperController.showPaper);
8+
router.post("/update/:id", paperController.updatePaper);
9+
router.delete("/delete/:id", paperController.deletePaper);
10+
11+
export default router;

services/paper.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Paper from "#models/paper";
2+
import databaseError from "#error/database";
3+
4+
export async function createPaper(
5+
answerSheetID,
6+
exam,
7+
student,
8+
checkedBy,
9+
mark,
10+
) {
11+
const newPaper = await Paper.create({
12+
answerSheetID,
13+
exam,
14+
student,
15+
checkedBy,
16+
mark,
17+
});
18+
if (newPaper) {
19+
return newPaper;
20+
}
21+
throw new databaseError.DataEntryError("paper");
22+
}
23+
24+
export async function updatePaperById(id, data) {
25+
const updated = await Paper.update({ _id: id }, data);
26+
if (updated) {
27+
return updated;
28+
}
29+
throw new databaseError.DataEntryError("paper");
30+
}
31+
32+
export async function listPaper(filter) {
33+
const paper = await Paper.read(filter, 0);
34+
return paper;
35+
}
36+
37+
export async function deletePaperById(paperId) {
38+
const deleted = await Paper.remove({ _id: paperId });
39+
if (deleted) {
40+
return deleted;
41+
}
42+
throw new databaseError.DataDeleteError("paper");
43+
}

test/routes/paper.test.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import request from "supertest";
2+
import { jest } from "@jest/globals";
3+
import app from "#app";
4+
import paperModel from "#models/paper";
5+
import connector from "#models/databaseUtil";
6+
7+
jest.mock("#util");
8+
9+
let server;
10+
let agent;
11+
12+
beforeAll((done) => {
13+
server = app.listen(null, () => {
14+
agent = request.agent(server);
15+
connector.set("debug", false);
16+
done();
17+
});
18+
});
19+
20+
function cleanUp(callback) {
21+
paperModel.remove(
22+
{
23+
answerSheetID: "asd123",
24+
exam: "64fc3c8bde9fa947ea1f412f",
25+
student: "64fc3c8bde9fa947ea1f412f",
26+
checkedBy: "64fc3c8bde9fa947ea1f412f",
27+
mark: 100,
28+
},
29+
)
30+
.then(() => {
31+
connector.disconnect((DBerr) => {
32+
if (DBerr) console.log("Database disconnect error: ", DBerr);
33+
server.close((serverErr) => {
34+
if (serverErr) console.log(serverErr);
35+
callback();
36+
});
37+
});
38+
});
39+
}
40+
41+
afterAll((done) => {
42+
cleanUp(done);
43+
});
44+
45+
describe("Paper CRUD", () => {
46+
it("should create paper with associated exam, student and faculty", async () => {
47+
const response = await agent.post("/paper/add").send(
48+
{
49+
answerSheetID: "asd123",
50+
exam: "64fc3c8bde9fa947ea1f412f",
51+
student: "64fc3c8bde9fa947ea1f412f",
52+
checkedBy: "64fc3c8bde9fa947ea1f412f",
53+
mark: 100,
54+
},
55+
);
56+
57+
expect(response.status).toBe(200);
58+
expect(response.headers["content-type"]).toMatch(/json/);
59+
});
60+
61+
let id;
62+
beforeEach(async () => {
63+
id = await agent.post("/paper/add").send(
64+
{
65+
answerSheetID: "asd123",
66+
exam: "64fc3c8bde9fa947ea1f412f",
67+
student: "64fc3c8bde9fa947ea1f412f",
68+
checkedBy: "64fc3c8bde9fa947ea1f412f",
69+
mark: 100,
70+
},
71+
);
72+
id = JSON.parse(id.res.text).id;
73+
});
74+
75+
afterEach(async () => {
76+
await paperModel.remove(
77+
{
78+
answerSheetID: "asd123",
79+
exam: "64fc3c8bde9fa947ea1f412f",
80+
student: "64fc3c8bde9fa947ea1f412f",
81+
checkedBy: "64fc3c8bde9fa947ea1f412f",
82+
mark: 100,
83+
},
84+
);
85+
});
86+
87+
it("should read paper", async () => {
88+
const response = await agent
89+
.get("/paper/list")
90+
.send(
91+
{
92+
answerSheetID: "asd123",
93+
exam: "64fc3c8bde9fa947ea1f412f",
94+
student: "64fc3c8bde9fa947ea1f412f",
95+
checkedBy: "64fc3c8bde9fa947ea1f412f",
96+
mark: 100,
97+
},
98+
);
99+
expect(response.body.res).not.toBeNull();
100+
});
101+
102+
it("should update paper", async () => {
103+
const response = await agent
104+
.post(`/paper/update/${id}`)
105+
.send(
106+
{
107+
answerSheetID: "asd123",
108+
exam: "64fc3c8bde9fa947ea1f412f",
109+
student: "64fc3c8bde9fa947ea1f412f",
110+
checkedBy: "64fc3c8bde9fa947ea1f412f",
111+
mark: 100,
112+
},
113+
);
114+
expect(response.status).toBe(200);
115+
expect(response.body.res).toMatch(/Paper updated/);
116+
});
117+
});

0 commit comments

Comments
 (0)