-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #302 from tcet-opensource/277-create-all-endpoints…
…-for-assingment 277 create all endpoints for assingment
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,53 @@ | ||
import { | ||
createAssignment, deleteAssignmentById, assignmentList, updateAssignmentById, | ||
} from "#services/assignment"; | ||
import { logger } from "#util"; | ||
|
||
async function addAssignment(req, res) { | ||
const { | ||
no, title, type, marks, | ||
} = req.body; | ||
try { | ||
const newAssignment = await createAssignment(no, title, type, marks); | ||
res.json({ res: `added user ${newAssignment.id}` }); | ||
} catch (error) { | ||
logger.error("Error while inserting", error); | ||
res.status(500); | ||
res.json({ err: "Error while inserting in DB" }); | ||
} | ||
} | ||
|
||
async function updateAssignment(req, res) { | ||
const { | ||
id, ...data | ||
} = req.body; | ||
try { | ||
await updateAssignmentById(id, data); | ||
res.json({ res: `updated assignment with id ${id}` }); | ||
} catch (error) { | ||
logger.error("Error while updating", error); | ||
res.status(500); | ||
res.json({ err: "Error while updaing in DB" }); | ||
} | ||
} | ||
|
||
async function getAssignment(req, res) { | ||
const filter = req.query; | ||
const assingmentlist = await assignmentList(filter); | ||
res.json({ res: assingmentlist }); | ||
} | ||
|
||
async function deleteAssignment(req, res) { | ||
const { assignmentId } = req.params; | ||
try { | ||
await deleteAssignmentById(assignmentId); | ||
|
||
res.json({ res: `Deleted assignment with ID ${assignmentId}` }); | ||
} catch (error) { | ||
logger.error("Error while deleting", error); | ||
res.status(500).json({ error: "Error while deleting from DB" }); | ||
} | ||
} | ||
export default { | ||
addAssignment, deleteAssignment, getAssignment, updateAssignment, | ||
}; |
This file contains 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 assingmentController from "#controller/assignment"; | ||
|
||
const router = express.Router(); | ||
router.post("/add", assingmentController.addAssignment); | ||
router.get("/list", assingmentController.getAssignment); | ||
router.post("/update", assingmentController.updateAssignment); | ||
router.post("/delete", assingmentController.deleteAssignment); | ||
|
||
export default router; |
This file contains 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 Assignment from "#models/assignment"; | ||
import databaseError from "#error/database"; | ||
|
||
export async function createAssignment(no, title, type, marks) { | ||
const newAssignment = await Assignment.create({ | ||
no, title, type, marks, | ||
}); | ||
if (newAssignment.name === name) { | ||
return newAssignment; | ||
} | ||
throw new databaseError.DataEntryError("assignment"); | ||
} | ||
|
||
export async function updateAssignmentById(id, data) { | ||
const updated = await Assignment.update({ _id: id }, data); | ||
if (updated) { | ||
return updated; | ||
} | ||
throw new databaseError.DataEntryError("assignment"); | ||
} | ||
|
||
export async function assignmentList(filter) { | ||
const assignmentlist = await Assignment.read(filter, 0); | ||
return assignmentList; | ||
} | ||
|
||
export async function deleteAssignmentById(assignmentId) { | ||
const deleted = await Assignment.remove({ _id: assignmentId}); | ||
if (deleted) { | ||
return deleted; | ||
} | ||
throw new databaseError.DataDeleteError("assingment"); | ||
} |