Skip to content

Commit 1213e8e

Browse files
Merge pull request #340 from tcet-opensource/314-all-endpoints-for-group
[Mega-Feat]: Implement Group Endpoints
2 parents 692065c + 7f96c21 commit 1213e8e

File tree

7 files changed

+252
-4
lines changed

7 files changed

+252
-4
lines changed

_apidoc.js

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

669+
// ------------------------------------------------------------------------------------------
670+
// Group.
671+
// ------------------------------------------------------------------------------------------
672+
673+
/**
674+
* @api {post} /group/add Add Group
675+
* @apiName AddGroup
676+
* @apiGroup Group
677+
* @apiDescription Add a new group.
678+
*
679+
* @apiBody {String} title Group title.
680+
* @apiBody {ObjectId[]} students Array of student ObjectIDs.
681+
*
682+
* @apiSuccess {String} res Response message.
683+
* @apiError (Error 500) GroupAddError Error while adding the group
684+
*
685+
* @apiSuccessExample Success-Response:
686+
* HTTP/1.1 200 OK
687+
* {
688+
* "res": "added group Example Group"
689+
* }
690+
*
691+
* @apiErrorExample Error-Response:
692+
* HTTP/1.1 500 Internal Server Error
693+
* {
694+
* "err": "Error while inserting in DB"
695+
* }
696+
*/
697+
669698
// ------------------------------------------------------------------------------------------
670699
// Semester
671700
// ------------------------------------------------------------------------------------------
@@ -757,9 +786,6 @@
757786
* @apiError (Error 500) DatabaseError Error while inserting in the database.
758787
*
759788
* @apiDescription Adds a new Activity to the system.
760-
*/
761-
762-
/**
763789
*
764790
* @apiSuccessExample Success-Response:
765791
* HTTP/1.1 200 OK
@@ -774,6 +800,45 @@
774800
* }
775801
*/
776802

803+
/**
804+
* @api {delete} /group/delete/:id Delete Group
805+
* @apiName DeleteGroup
806+
* @apiGroup Group
807+
*
808+
* @apiParam {ObjectId} id The ObjectID of the group to delete.
809+
*
810+
* @apiSuccess {String} res Success message indicating the deletion.
811+
* @apiError (Error 500) GroupDeleteError Error while deleting the group
812+
*
813+
*/
814+
815+
/**
816+
* @api {post} /group/update/:id Update Group Details
817+
* @apiName UpdateGroup
818+
* @apiGroup Group
819+
* @apiDescription Update existing group details.
820+
*
821+
* @apiParam {ObjectId} id The ObjectID of the group to update.
822+
* @apiBody {String} [title] Group title.
823+
* @apiBody {ObjectId[]} [students] Array of student ObjectIDs.
824+
*
825+
* @apiSuccess {String} res Group updated.
826+
* @apiError (Error 500) GroupUpdateError Error in updating database
827+
*
828+
*/
829+
830+
/**
831+
* @api {get} /group/list Get Group List
832+
* @apiName GetGroupList
833+
* @apiGroup Group
834+
*
835+
* @apiQuery {String} [title] Title of the group.
836+
*
837+
* @apiSuccess {Group[]} res Array of filtered group documents.
838+
* @apiSuccess {ObjectId} group._id ObjectID of the group document in the database.
839+
* @apiSuccess {String} group.title Title of the group.
840+
* @apiSuccess {ObjectId[]} group.students Array of student ObjectIDs in the group.
841+
*/
777842
/**
778843
* @api {delete} /timetable/delete/:timetableId Delete Timetable
779844
* @apiName DeleteTimetable

app.js

Lines changed: 2 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 groupRouter from "#routes/group";
2627

2728
const app = express();
2829
const currDirName = dirname(fileURLToPath(import.meta.url));
@@ -56,6 +57,7 @@ app.use("/timetable", timetableRouter);
5657
app.use("/department", departmentRouter);
5758
app.use("/coursework", courseworkRouter);
5859
app.use("/module", moduleRouter);
60+
app.use("/group", groupRouter);
5961
app.use("/semester", semesterRouter);
6062
app.use("/faculty", facultyRouter);
6163
export default app;

controller/group.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
createGroup, deleteGroupById, groupList, updateGroupById,
3+
} from "#services/group";
4+
import { logger } from "#util";
5+
6+
async function addGroup(req, res) {
7+
const {
8+
title, student,
9+
} = req.body;
10+
try {
11+
const group = await createGroup(title, student);
12+
res.json({ res: `added group ${group.id}`, id: group.id });
13+
} catch (error) {
14+
logger.error("Error while inserting", error);
15+
res.status(500);
16+
res.json({ err: "Error while inserting in DB" });
17+
}
18+
}
19+
20+
async function updateGroup(req, res) {
21+
const { id } = req.params;
22+
const {
23+
...data
24+
} = req.body;
25+
try {
26+
await updateGroupById(id, data);
27+
res.json({ res: `updated group with id ${id}` });
28+
} catch (error) {
29+
logger.error("Error while updating", error);
30+
res.status(500);
31+
res.json({ err: "Error while updaing in DB" });
32+
}
33+
}
34+
35+
async function getGroup(req, res) {
36+
const filter = req.query;
37+
const group = await groupList(filter);
38+
res.json({ res: group });
39+
}
40+
41+
async function deleteGroup(req, res) {
42+
const { id } = req.params;
43+
try {
44+
await deleteGroupById(id);
45+
res.json({ res: `Deleted group with ID ${id}` });
46+
} catch (error) {
47+
logger.error("Error while deleting", error);
48+
res.status(500).json({ error: "Error while deleting from DB" });
49+
}
50+
}
51+
52+
export default {
53+
addGroup, deleteGroup, getGroup, updateGroup,
54+
};

models/group.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function read(filter, limit = 1) {
1919
}
2020

2121
async function update(filter, updateObject, options = { multi: true }) {
22-
const updateResult = await Group.updateManyupdateMany(filter, { $set: updateObject }, options);
22+
const updateResult = await Group.updateMany(filter, { $set: updateObject }, options);
2323
return updateResult.acknowledged;
2424
}
2525
async function remove(groupId) {

routes/group.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 groupController from "#controller/group";
3+
4+
const router = express.Router();
5+
router.post("/add", groupController.addGroup);
6+
router.get("/list", groupController.getGroup);
7+
router.post("/update/:id", groupController.updateGroup);
8+
router.delete("/delete/:id", groupController.deleteGroup);
9+
10+
export default router;

services/group.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Group from "#models/group";
2+
import databaseError from "#error/database";
3+
4+
export async function createGroup(title, student) {
5+
const newGroup = await Group.create({
6+
title, student,
7+
});
8+
if (newGroup.title === title) {
9+
return newGroup;
10+
}
11+
throw new databaseError.DataEntryError("group");
12+
}
13+
14+
export async function updateGroupById(id, data) {
15+
const updated = await Group.update({ _id: id }, data);
16+
if (updated) {
17+
return updated;
18+
}
19+
throw new databaseError.DataEntryError("group");
20+
}
21+
22+
export async function groupList(filter) {
23+
const groups = await Group.read(filter, 0);
24+
return groups;
25+
}
26+
27+
export async function deleteGroupById(groupId) {
28+
const deleted = await Group.remove({ _id: groupId });
29+
if (deleted) {
30+
return deleted;
31+
}
32+
throw new databaseError.DataDeleteError("group");
33+
}

test/routes/group.test.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
2+
import request from "supertest";
3+
import app from "#app";
4+
import connector from "#models/databaseUtil";
5+
import groupModel from "#models/group";
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+
groupModel
22+
.remove({
23+
id: "6500594e2b7b532006c073dd",
24+
})
25+
.then(() => {
26+
connector.disconnect((DBerr) => {
27+
if (DBerr) console.log("Database disconnect error: ", DBerr);
28+
server.close((serverErr) => {
29+
if (serverErr) console.log(serverErr);
30+
callback();
31+
});
32+
});
33+
});
34+
}
35+
36+
afterAll((done) => {
37+
cleanUp(done);
38+
});
39+
40+
describe("group API", () => {
41+
it("should create group", async () => {
42+
const response = await agent.post("/group/add").send({
43+
title: "Group 1",
44+
student: "64fdc67feca8a69f01b33614",
45+
});
46+
expect(response.headers["content-type"]).toMatch(/json/);
47+
expect(response.status).toBe(200);
48+
expect(response.body.res).toMatch(/added group/);
49+
});
50+
51+
describe("after adding group", () => {
52+
let id;
53+
beforeEach(async () => {
54+
id = await agent.post("/group/add").send({
55+
title: "Group 1",
56+
student: "64fdc67feca8a69f01b33614",
57+
});
58+
id = JSON.parse(id.res.text).id;
59+
});
60+
61+
afterEach(async () => {
62+
await groupModel.remove({
63+
id: "6500594e2b7b532006c073dd",
64+
});
65+
});
66+
67+
it("should read group", async () => {
68+
const response = await agent
69+
.get("/group/list")
70+
.send({ name: "Building A" });
71+
expect(response.status).toBe(200);
72+
expect(response.body.res).toBeDefined();
73+
});
74+
75+
it("should update group", async () => {
76+
const response = await agent
77+
.post(`/group/update/${id}`)
78+
.send({ title: "Group 1" }, { title: "Group 2" });
79+
expect(response.headers["content-type"]).toMatch(/json/);
80+
expect(response.status).toBe(200);
81+
expect(response.body.res).toMatch(/updated group/);
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)