From 02ac15d8e3143cb7a241276f998a102edcae0e19 Mon Sep 17 00:00:00 2001 From: Ankit <115006624+ANKIT638-ux@users.noreply.github.com> Date: Mon, 25 Sep 2023 00:19:50 +0530 Subject: [PATCH] endpoint testcase apidoc of paper --- _apidoc.js | 77 +++++++++++++++++++++++++ app.js | 3 + controller/paper.js | 64 +++++++++++++++++++++ models/paper.js | 1 - routes/paper.js | 11 ++++ services/paper.js | 43 ++++++++++++++ test/routes/paper.test.js | 117 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 315 insertions(+), 1 deletion(-) create mode 100644 controller/paper.js create mode 100644 routes/paper.js create mode 100644 services/paper.js create mode 100644 test/routes/paper.test.js diff --git a/_apidoc.js b/_apidoc.js index 2d034d4e..de325c0f 100644 --- a/_apidoc.js +++ b/_apidoc.js @@ -665,3 +665,80 @@ * @apiSuccess {String[]} module.cognitiveLevels Array of cognitive levels of * attainment as per Bloom's Taxanomy (L1-L6). */ + +// ------------------------------------------------------------------------------------------ +// Paper. +// ------------------------------------------------------------------------------------------ +// +/** + * @api {post} /paper/add Add Paper + * @apiName AddPaper + * @apiDescription Adds a new Paper. + * @apiGroup Paper + * + * @apiBody {String} [answersheetID] The id of the Answersheet. + * @apiBody {connector.Schema.Types.ObjectId} Exam The Exam which is associated. + * @apiBody {connector.Schema.Types.ObjectId} Student The Student which is associated. + * @apiBody {connector.Schema.Types.ObjectId} Faculty The Faculty which is associated. + * @apiBody {Number} [marks] marks in the paper. + * + * @apiSuccess {String} res added Paper successfully. + * + * @apiError (Error 500) DatabaseError Error while inserting in the DB. + * + */ + +/** + * @api {get} /paper/list Listdown Paper + * @apiName GetPaper + * @apiDescription Listdown the Paper. + * @apiGroup Paper + * + * @apiQuery {String} [answersheetID] The id of the Answersheet. + * @apiQuery {connector.Schema.Types.ObjectId} Exam The Exam which is associated. + * @apiQuery {connector.Schema.Types.ObjectId} Student The Student which is associated. + * @apiQuery {connector.Schema.Types.ObjectId} Faculty The Faculty which is associated. + * @apiQuery {Number} [marks] marks in the paper. + * + * @apiSuccess {Paper[]} res Array of Filtered Paper Doc. + * @apiSuccess {String} paper._id ID of paper given by database. + * @apiSuccess {String} paper.answersheetID ID of answersheet. + * @apiSuccess {connector.Schema.Types.ObjectId} paper.exam associated Exam. + * @apiSuccess {connector.Schema.Types.ObjectId} paper.student associated Student. + * @apiSuccess {connector.Schema.Types.ObjectId} paper.faculty associated Faculty. + * @apiSuccess {Number} paper.marks The marks in the Paper. + * @apiError (Error 500) err Error while fetching the data. + */ + +/** + * @api {delete} /paper/delete/:id Delete Paper + * @apiName DeletePaper + * @apiDescription Remove the existing Paper. + * @apiGroup Paper + * + * @apiParam {String} answersheetID The ID of the answersheet to delete. + * + * @apiSuccess {String} res Paper deleted successfully. + * + * @apiError (Error 500) err Error while deleting from DB. + * +* */ + +/** + * @api {post} /paper/update/ Update Paper + * @apiName UpdatePaper + * @apiGroup Paper + * @apiDescription Update Existing Paper details except + * + * @apiSuccess {Paper[]} res Array of Filtered Paper Doc . + * @apiSuccess {String} paper._id ID of paper given by database. + * @apiSuccess {String} paper.answersheetID Name of Infrastructure + * @apiSuccess {connector.Schema.Types.ObjectId} paper.exam associated Exam. + * @apiSuccess {connector.Schema.Types.ObjectId} paper.student associated Student. + * @apiSuccess {connector.Schema.Types.ObjectId} paper.faculty associated Faculty. + * @apiSuccess {Number} paper.marks The marks in the Paper. + * + * @apiSuccess {String} res Paper updated successfully. + * + * @apiError (Error 500) err Error while updating the data.s + */ \ No newline at end of file diff --git a/app.js b/app.js index ddecdf42..a8800740 100644 --- a/app.js +++ b/app.js @@ -20,6 +20,7 @@ import courseworkRouter from "#routes/coursework"; import moduleRouter from "#routes/module"; import { identifyUser } from "#middleware/identifyUser"; import departmentRouter from "#routes/department"; +import paperRouter from "#routes/paper"; const app = express(); const currDirName = dirname(fileURLToPath(import.meta.url)); @@ -52,4 +53,6 @@ app.use("/timetable", timetableRouter); app.use("/department", departmentRouter); app.use("/coursework", courseworkRouter); app.use("/module", moduleRouter); +app.use("/paper", paperRouter); + export default app; diff --git a/controller/paper.js b/controller/paper.js new file mode 100644 index 00000000..6a6e6888 --- /dev/null +++ b/controller/paper.js @@ -0,0 +1,64 @@ +import { + createPaper, updatePaperById, listPaper, deletePaperById, +} from "#services/paper"; +import { logger } from "#util"; + +async function addPaper(req, res) { + const { + answerSheetID, + exam, + student, + checkedBy, + mark, + } = req.body; + try { + const newPaper = await createPaper( + answerSheetID, + exam, + student, + checkedBy, + mark, + ); + res.json({ res: `added paper for: ${newPaper.answerSheetID}`, id: newPaper.id }); + } catch (error) { + logger.error("Error while inserting", error); + res.status(500); + res.json({ err: "Error while inserting in DB" }); + } +} + +async function updatePaper(req, res) { + const { id } = req.params; + const { + ...data + } = req.body; + try { + await updatePaperById(id, data); + res.json({ res: "Paper updated" }); + } catch (error) { + logger.error("Error while updating", error); + res.status(500); + res.json({ err: "Error while updating in DB" }); + } +} + +async function showPaper(req, res) { + const filter = req.query; + const paper = await listPaper(filter); + res.json({ res: paper }); +} + +async function deletePaper(req, res) { + const { id } = req.params; + try { + await deletePaperById(id); + res.json({ res: `Deleted paper with ID ${id}` }); + } catch (error) { + logger.error("Error while deleting", error); + res.status(500).json({ error: "Error while deleting from DB" }); + } +} + +export default { + addPaper, updatePaper, showPaper, deletePaper, +}; diff --git a/models/paper.js b/models/paper.js index 5076960c..1bb2d467 100644 --- a/models/paper.js +++ b/models/paper.js @@ -8,7 +8,6 @@ const paperSchema = { mark: { type: Number, required: true }, }; -// eslint-disable-next-line no-unused-vars const Paper = connector.model("Paper", paperSchema); // CRUD OPERATIONS diff --git a/routes/paper.js b/routes/paper.js new file mode 100644 index 00000000..f754ea35 --- /dev/null +++ b/routes/paper.js @@ -0,0 +1,11 @@ +import express from "express"; +import paperController from "#controller/paper"; + +const router = express.Router(); + +router.post("/add", paperController.addPaper); +router.get("/list", paperController.showPaper); +router.post("/update/:id", paperController.updatePaper); +router.delete("/delete/:id", paperController.deletePaper); + +export default router; diff --git a/services/paper.js b/services/paper.js new file mode 100644 index 00000000..64301a18 --- /dev/null +++ b/services/paper.js @@ -0,0 +1,43 @@ +import Paper from "#models/paper"; +import databaseError from "#error/database"; + +export async function createPaper( + answerSheetID, + exam, + student, + checkedBy, + mark, +) { + const newPaper = await Paper.create({ + answerSheetID, + exam, + student, + checkedBy, + mark, + }); + if (newPaper) { + return newPaper; + } + throw new databaseError.DataEntryError("paper"); +} + +export async function updatePaperById(id, data) { + const updated = await Paper.update({ _id: id }, data); + if (updated) { + return updated; + } + throw new databaseError.DataEntryError("paper"); +} + +export async function listPaper(filter) { + const paper = await Paper.read(filter, 0); + return paper; +} + +export async function deletePaperById(paperId) { + const deleted = await Paper.remove({ _id: paperId }); + if (deleted) { + return deleted; + } + throw new databaseError.DataDeleteError("paper"); +} diff --git a/test/routes/paper.test.js b/test/routes/paper.test.js new file mode 100644 index 00000000..788cb431 --- /dev/null +++ b/test/routes/paper.test.js @@ -0,0 +1,117 @@ +import request from "supertest"; +import { jest } from "@jest/globals"; +import app from "#app"; +import paperModel from "#models/paper"; +import connector from "#models/databaseUtil"; + +jest.mock("#util"); + +let server; +let agent; + +beforeAll((done) => { + server = app.listen(null, () => { + agent = request.agent(server); + connector.set("debug", false); + done(); + }); +}); + +function cleanUp(callback) { + paperModel.remove( + { + answerSheetID: "asd123", + exam: "64fc3c8bde9fa947ea1f412f", + student: "64fc3c8bde9fa947ea1f412f", + checkedBy: "64fc3c8bde9fa947ea1f412f", + mark: 100, + }, + ) + .then(() => { + connector.disconnect((DBerr) => { + if (DBerr) console.log("Database disconnect error: ", DBerr); + server.close((serverErr) => { + if (serverErr) console.log(serverErr); + callback(); + }); + }); + }); +} + +afterAll((done) => { + cleanUp(done); +}); + +describe("Paper CRUD", () => { + it("should create paper with associated exam, student and faculty", async () => { + const response = await agent.post("/paper/add").send( + { + answerSheetID: "asd123", + exam: "64fc3c8bde9fa947ea1f412f", + student: "64fc3c8bde9fa947ea1f412f", + checkedBy: "64fc3c8bde9fa947ea1f412f", + mark: 100, + }, + ); + + expect(response.status).toBe(200); + expect(response.headers["content-type"]).toMatch(/json/); + }); + + let id; + beforeEach(async () => { + id = await agent.post("/paper/add").send( + { + answerSheetID: "asd123", + exam: "64fc3c8bde9fa947ea1f412f", + student: "64fc3c8bde9fa947ea1f412f", + checkedBy: "64fc3c8bde9fa947ea1f412f", + mark: 100, + }, + ); + id = JSON.parse(id.res.text).id; + }); + + afterEach(async () => { + await paperModel.remove( + { + answerSheetID: "asd123", + exam: "64fc3c8bde9fa947ea1f412f", + student: "64fc3c8bde9fa947ea1f412f", + checkedBy: "64fc3c8bde9fa947ea1f412f", + mark: 100, + }, + ); + }); + + it("should read paper", async () => { + const response = await agent + .get("/paper/list") + .send( + { + answerSheetID: "asd123", + exam: "64fc3c8bde9fa947ea1f412f", + student: "64fc3c8bde9fa947ea1f412f", + checkedBy: "64fc3c8bde9fa947ea1f412f", + mark: 100, + }, + ); + expect(response.body.res).not.toBeNull(); + }); + + it("should update paper", async () => { + const response = await agent + .post(`/paper/update/${id}`) + .send( + { + answerSheetID: "asd123", + exam: "64fc3c8bde9fa947ea1f412f", + student: "64fc3c8bde9fa947ea1f412f", + checkedBy: "64fc3c8bde9fa947ea1f412f", + mark: 100, + }, + ); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/Paper updated/); + }); + }); \ No newline at end of file