-
-
Notifications
You must be signed in to change notification settings - Fork 40
[Mega-Feat]: Implement Group Endpoints #340
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3c3612a
created controller and services for group
sumitst05 730ad7f
created and integrated group routes
sumitst05 8c4eb43
created testcases for all endpoints
sumitst05 388cf05
added apidoc for all endpoints of group
sumitst05 abf0be9
added checks for headers to match json in update operations
sumitst05 79f3ad9
refactored remove() to pass only object id in tests
sumitst05 c84b762
Merge branch 'development' into 314-all-endpoints-for-group
TejasNair9977 7f96c21
removed extra identifyUser
TejasNair9977 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { | ||
| createGroup, deleteGroupById, groupList, updateGroupById, | ||
| } from "#services/group"; | ||
| import { logger } from "#util"; | ||
|
|
||
| async function addGroup(req, res) { | ||
| const { | ||
| title, student, | ||
| } = req.body; | ||
| try { | ||
| const group = await createGroup(title, student); | ||
| res.json({ res: `added group ${group.id}`, id: group.id }); | ||
| } catch (error) { | ||
| logger.error("Error while inserting", error); | ||
| res.status(500); | ||
| res.json({ err: "Error while inserting in DB" }); | ||
| } | ||
| } | ||
|
|
||
| async function updateGroup(req, res) { | ||
| const { id } = req.params; | ||
| const { | ||
| ...data | ||
| } = req.body; | ||
| try { | ||
| await updateGroupById(id, data); | ||
| res.json({ res: `updated group with id ${id}` }); | ||
| } catch (error) { | ||
| logger.error("Error while updating", error); | ||
| res.status(500); | ||
| res.json({ err: "Error while updaing in DB" }); | ||
| } | ||
| } | ||
|
|
||
| async function getGroup(req, res) { | ||
| const filter = req.query; | ||
| const group = await groupList(filter); | ||
| res.json({ res: group }); | ||
| } | ||
|
|
||
| async function deleteGroup(req, res) { | ||
| const { id } = req.params; | ||
| try { | ||
| await deleteGroupById(id); | ||
| res.json({ res: `Deleted group with ID ${id}` }); | ||
| } catch (error) { | ||
| logger.error("Error while deleting", error); | ||
| res.status(500).json({ error: "Error while deleting from DB" }); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| addGroup, deleteGroup, getGroup, updateGroup, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import express from "express"; | ||
| import groupController from "#controller/group"; | ||
|
|
||
| const router = express.Router(); | ||
| router.post("/add", groupController.addGroup); | ||
| router.get("/list", groupController.getGroup); | ||
| router.post("/update/:id", groupController.updateGroup); | ||
| router.delete("/delete/:id", groupController.deleteGroup); | ||
|
|
||
| export default router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import Group from "#models/group"; | ||
| import databaseError from "#error/database"; | ||
|
|
||
| export async function createGroup(title, student) { | ||
| const newGroup = await Group.create({ | ||
| title, student, | ||
| }); | ||
| if (newGroup.title === title) { | ||
| return newGroup; | ||
| } | ||
| throw new databaseError.DataEntryError("group"); | ||
| } | ||
|
|
||
| export async function updateGroupById(id, data) { | ||
| const updated = await Group.update({ _id: id }, data); | ||
| if (updated) { | ||
| return updated; | ||
| } | ||
| throw new databaseError.DataEntryError("group"); | ||
| } | ||
|
|
||
| export async function groupList(filter) { | ||
| const groups = await Group.read(filter, 0); | ||
| return groups; | ||
| } | ||
|
|
||
| export async function deleteGroupById(groupId) { | ||
| const deleted = await Group.remove({ _id: groupId }); | ||
| if (deleted) { | ||
| return deleted; | ||
| } | ||
| throw new databaseError.DataDeleteError("group"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies | ||
| import request from "supertest"; | ||
| import app from "#app"; | ||
| import connector from "#models/databaseUtil"; | ||
| import groupModel from "#models/group"; | ||
|
|
||
| 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) { | ||
| groupModel | ||
| .remove({ | ||
| id: "6500594e2b7b532006c073dd", | ||
| }) | ||
| .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("group API", () => { | ||
| it("should create group", async () => { | ||
| const response = await agent.post("/group/add").send({ | ||
| title: "Group 1", | ||
| student: "64fdc67feca8a69f01b33614", | ||
| }); | ||
| expect(response.headers["content-type"]).toMatch(/json/); | ||
| expect(response.status).toBe(200); | ||
| expect(response.body.res).toMatch(/added group/); | ||
| }); | ||
|
|
||
| describe("after adding group", () => { | ||
| let id; | ||
| beforeEach(async () => { | ||
| id = await agent.post("/group/add").send({ | ||
| title: "Group 1", | ||
| student: "64fdc67feca8a69f01b33614", | ||
| }); | ||
| id = JSON.parse(id.res.text).id; | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await groupModel.remove({ | ||
| id: "6500594e2b7b532006c073dd", | ||
| }); | ||
| }); | ||
|
|
||
| it("should read group", async () => { | ||
| const response = await agent | ||
| .get("/group/list") | ||
| .send({ name: "Building A" }); | ||
| expect(response.status).toBe(200); | ||
| expect(response.body.res).toBeDefined(); | ||
| }); | ||
|
|
||
| it("should update group", async () => { | ||
| const response = await agent | ||
| .post(`/group/update/${id}`) | ||
| .send({ title: "Group 1" }, { title: "Group 2" }); | ||
| expect(response.headers["content-type"]).toMatch(/json/); | ||
| expect(response.status).toBe(200); | ||
| expect(response.body.res).toMatch(/updated group/); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should pass only id as parameter in remove(delete) operation not with title because in group models we are taking only groupId as input