Skip to content

Commit c76251a

Browse files
authored
Merge pull request #341 from tcet-opensource/315-crud-endpoint-activity
[ADDED]added crud, all endpoints, apidocs , testcases for activity.
2 parents 81132e9 + 92c9c87 commit c76251a

File tree

7 files changed

+399
-21
lines changed

7 files changed

+399
-21
lines changed

_apidoc.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,152 @@
665665
* @apiSuccess {String[]} module.cognitiveLevels Array of cognitive levels of
666666
* attainment as per Bloom's Taxanomy (L1-L6).
667667
*/
668+
669+
// ------------------------------------------------------------------------------------------
670+
// Activity.
671+
// ------------------------------------------------------------------------------------------
672+
673+
/**
674+
* @api {post} /activity/add Add Activty.
675+
* @apiName AddActivity
676+
* @apiGroup Activity
677+
*
678+
* @apiBody {Date} startTime The startTime of the activity.
679+
* @apiBody {Number} duration The duration of the activity (in minutes).
680+
* @apiBody {ObjectId} course The course of the activity (ObjectId).
681+
* @apiBody {ObjectId} faculty The faculty alloted for the activity(ObjectId).
682+
* @apiBody {String} type The type of activity.One of possible LECTURE, PRACTICAL, TUTORIAL.
683+
* @apiBody {ObjectId} task The task of the activity (ObjectId).One of possible Topic,Practical,Tutorial.
684+
* @apiBody {ObjectId} group The group of the activity (ObjectId).
685+
* @apiBody {ObjectId} students the students who gonna attend the activity(ObjectId).
686+
*
687+
* @apiSuccess {String} res Response message.
688+
*
689+
* @apiError (Error 500) DatabaseError Error while inserting in the database.
690+
*
691+
* @apiDescription Adds a new Activity to the system.
692+
*/
693+
694+
/**
695+
*
696+
* @apiSuccessExample Success-Response:
697+
* HTTP/1.1 200 OK
698+
* {
699+
* "res": "Added activity"
700+
* }
701+
*
702+
* @apiErrorExample Error-Response:
703+
* HTTP/1.1 500 Internal Server Error
704+
* {
705+
* "err": "Error while inserting in DB"
706+
* }
707+
*/
708+
709+
/**
710+
* @api {delete} /timetable/delete/:timetableId Delete Timetable
711+
* @apiName DeleteTimetable
712+
* @apiGroup Timetable
713+
*
714+
* @apiParam {String} timetableId The ID of the timetable document to delete.
715+
*
716+
* @apiSuccess {String} res Success message indicating the deletion.
717+
*
718+
* @apiError (Error 500) DatabaseError Error message if there was an error during the deletion.
719+
*/
720+
721+
/**
722+
* @api {delete} /activity/delete/:activity Delete Activity.
723+
* @apiName DeleteActivity
724+
* @apiGroup Activity
725+
*
726+
* @apiParam {String} Activity The activity document to delete.
727+
*
728+
* @apiSuccess {String} res Success message indicating the deletion.
729+
*
730+
* @apiError (Error 500) DatabaseError Error message if there was an error during the deletion.
731+
*/
732+
733+
/**
734+
* @api {post} /timetable/update Update Timetable
735+
* @apiName UpdateTimetable
736+
* @apiGroup Timetable
737+
* @apiDescription Update existing timetable data.
738+
*
739+
* @apiBody {Date} startTime The startTime of the activity.
740+
* @apiBody {Number} duration The duration of the activity (in minutes).
741+
* @apiBody {ObjectId} course The course of the activity (ObjectId).
742+
* @apiBody {ObjectId} faculty The faculty alloted for the activity(ObjectId).
743+
* @apiBody {String} type The type of activity.One of possible LECTURE, PRACTICAL, TUTORIAL.
744+
* @apiBody {ObjectId} task The task of the activity (ObjectId).One of possible Topic,Practical,Tutorial.
745+
* @apiBody {ObjectId} group The group of the activity (ObjectId).
746+
* @apiBody {ObjectId} students the students who gonna attend the activity(ObjectId).
747+
*
748+
* @apiSuccess {String} res Timetable updated.
749+
*/
750+
751+
/**
752+
* @api {post} /activity/update Update Activity.
753+
* @apiName UpdateActivity
754+
* @apiGroup Activity
755+
* @apiDescription Update existing activity data.
756+
*
757+
* @apiBody {Date} startTime The startTime of the activity.
758+
* @apiBody {Number} duration The duration of the activity (in minutes).
759+
* @apiBody {ObjectId} course The course of the activity (ObjectId).
760+
* @apiBody {ObjectId} faculty The faculty alloted for the activity(ObjectId).
761+
* @apiBody {String} type The type of activity.One of possible LECTURE, PRACTICAL, TUTORIAL.
762+
* @apiBody {ObjectId} task The task of the activity (ObjectId).One of possible Topic,Practical,Tutorial.
763+
* @apiBody {ObjectId} group The group of the activity (ObjectId).
764+
* @apiBody {ObjectId} students the students who gonna attend the activity(ObjectId).
765+
*
766+
* @apiSuccess {String} res Activity updated.
767+
* @apiError (Error 500) DatabaseError Error in updating the database.
768+
*/
769+
770+
/**
771+
* @api {get} /timetable/list Get Timetable List
772+
* @apiName GetTimetableList
773+
* @apiGroup Timetable
774+
*
775+
* @apiQuery {Date} startTime The startTime of the activity.
776+
* @apiQuery {Number} duration The duration of the activity (in minutes).
777+
* @apiQUERY {ObjectId} course The course of the activity (ObjectId).
778+
* @apiQuery {ObjectId} faculty The faculty alloted for the activity(ObjectId).
779+
* @apiQuery {String} type The type of activity.One of possible LECTURE, PRACTICAL, TUTORIAL.
780+
* @apiQuery {ObjectId} task The task of the activity (ObjectId).One of possible Topic,Practical,Tutorial.
781+
* @apiQuery {ObjectId} group The group of the activity (ObjectId).
782+
* @apiQuery {ObjectId} students the students who gonna attend the activity(ObjectId).
783+
*
784+
* @apiSuccess {Date} startTime The startTime of the activity.
785+
* @apiSuccess {Number} duration The duration of the activity (in minutes).
786+
* @apiSuccess {ObjectId} course The course of the activity (ObjectId).
787+
* @apiSuccess {ObjectId} faculty The faculty alloted for the activity(ObjectId).
788+
* @apiSuccess {String} type The type of activity.One of possible LECTURE, PRACTICAL, TUTORIAL.
789+
* @apiSuccess {ObjectId} task The task of the activity (ObjectId).One of possible Topic,Practical,Tutorial.
790+
* @apiSuccess {ObjectId} group The group of the activity (ObjectId).
791+
* @apiSucess {ObjectId} students the students who gonna attend the activity(ObjectId).
792+
*/
793+
794+
/**
795+
* @api {get} /activity/list Get Activity List
796+
* @apiName GetActivityList
797+
* @apiGroup Activity
798+
*
799+
* @apiQuery {Date} startTime The startTime of the activity.
800+
* @apiQuery {Number} duration The duration of the activity (in minutes).
801+
* @apiQUERY {ObjectId} course The course of the activity (ObjectId).
802+
* @apiQuery {ObjectId} faculty The faculty alloted for the activity(ObjectId).
803+
* @apiQuery {String} type The type of activity.One of possible LECTURE, PRACTICAL, TUTORIAL.
804+
* @apiQuery {ObjectId} task The task of the activity (ObjectId).One of possible Topic,Practical,Tutorial.
805+
* @apiQuery {ObjectId} group The group of the activity (ObjectId).
806+
* @apiQuery {ObjectId} students the students who gonna attend the activity(ObjectId).
807+
*
808+
* @apiSuccess {Date} startTime The startTime of the activity.
809+
* @apiSuccess {Number} duration The duration of the activity (in minutes).
810+
* @apiSuccess {ObjectId} course The course of the activity (ObjectId).
811+
* @apiSuccess {ObjectId} faculty The faculty alloted for the activity(ObjectId).
812+
* @apiSuccess {String} type The type of activity.One of possible LECTURE, PRACTICAL, TUTORIAL.
813+
* @apiSuccess {ObjectId} task The task of the activity (ObjectId).One of possible Topic,Practical,Tutorial.
814+
* @apiSuccess {ObjectId} group The group of the activity (ObjectId).
815+
* @apiSucess {ObjectId} students the students who gonna attend the activity(ObjectId).
816+
*/

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import tutorialRouter from "#routes/tutorial";
1717
import assignmentRouter from "#routes/assignment";
1818
import timetableRouter from "#routes/timetable";
1919
import courseworkRouter from "#routes/coursework";
20+
import activityRouter from "#routes/activity";
2021
import moduleRouter from "#routes/module";
2122
import { identifyUser } from "#middleware/identifyUser";
2223
import departmentRouter from "#routes/department";
@@ -46,6 +47,7 @@ app.use("/department", departmentRouter);
4647
app.use("/practical", practicalRouter);
4748
app.use("/organization", organizationRouter);
4849
app.use("/student", studentRouter);
50+
app.use("/activity", activityRouter);
4951
app.use("/tutorial", tutorialRouter);
5052
app.use("/assignment", assignmentRouter);
5153
app.use("/timetable", timetableRouter);

controller/activity.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
createActivity,deleteActivityById, activityList ,updateActivityById,
3+
}from "#services/activity";
4+
import {logger} from "#util" ;
5+
6+
async function addActivity(req,res) {
7+
const{
8+
activityBlueprint,
9+
startTime,
10+
duration,
11+
course,
12+
faculty,
13+
type,
14+
task,
15+
group,
16+
students,
17+
}=req.body;
18+
try{
19+
const newActivity = await createActivity(activityBlueprint,startTime,duration,course,faculty,type,task,group,students);
20+
res.json ({res: `added activity ${newActivity.id}`, id: newActivity.id});
21+
} catch (error){
22+
logger.error ("Error while inserting",error);
23+
res.status(500);
24+
res.json({err:"Error while inserting in DB"});
25+
}
26+
}
27+
28+
async function updateActivity(req,res){
29+
const { id }=req.params;
30+
const {
31+
...data
32+
}=req.body;
33+
try {
34+
await updateActivityById(id,data);
35+
res.json({res:`updated activity with id ${id}`});
36+
}catch (error){
37+
logger.error("Error while updating",error);
38+
res.status(500);
39+
res.json({err:"Error while updating in DB"});
40+
}
41+
}
42+
43+
async function getActivity(req,res){
44+
const filter = req.query;
45+
const activitylist =await activityList(filter);
46+
res.json({res:activitylist});
47+
}
48+
49+
50+
async function deleteActivity(res,req){
51+
const { id }=req.params;
52+
try{
53+
await deleteActivityById(id);
54+
55+
res.json({res:`Deleted activity 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+
addActivity, deleteActivity ,getActivity ,updateActivity,
64+
};

models/activity.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,41 @@ const activitySchema = {
2222

2323
const Activity = connector.model("Activity", activitySchema);
2424

25-
// crud
25+
///crud operation///
2626

27-
async function create(activityData) {
27+
//add a activity to the database
28+
async function create(activityData){
2829
const {
29-
activityBlueprint, startTime, duration, course, faculty, type, task, group, students,
30-
} = activityData;
31-
const activity = new Activity({
32-
activityBlueprint, startTime, duration, course, faculty, type, task, group, students,
30+
activityBlueprint, startTime,duration,course,faculty,type,task,group,students,
31+
}=activityData;
32+
const activity= new Activity({
33+
activityBlueprint, startTime,duration,course,faculty,type,task,group,students,
3334
});
34-
const activityDoc = await activity.save();
35+
const activityDoc =await activity.save();
3536
return activityDoc;
3637
}
3738

38-
async function read(filter, limit = 1) {
39-
const activityDoc = await Activity.find(filter).limit(limit);
40-
return activityDoc;
39+
//Retrieve activity based on a given filter and limit
40+
async function read(filter,limit=1){
41+
const activityDoc = await Activity.find (filter).limit(limit);
42+
return activityDoc ;
4143
}
4244

43-
async function update(filter, updateObject, options = { multi: true }) {
44-
const updateResult = await Activity.updateMany(filter, { $set: updateObject }, options);
45-
return updateResult.acknowledged;
45+
//update activity based on a given filter
46+
async function update(filter,updateObject,options={multi:true}){
47+
const updateActivity= await Activity.updateMany(filter,{$set:updateObject},options);
48+
return updateActivity.acknowledged;
4649
}
4750

48-
async function remove(filter) {
49-
const deleteResult = await Activity.deleteMany(filter);
50-
return deleteResult.acknowledged;
51+
//Delete activity based on a given filter
52+
async function remove(filter){
53+
const deleteActivity= await Activity.deleteMany(filter).exec();
54+
return deleteActivity.acknowledged
5155
}
5256

53-
export default {
54-
create,
55-
read,
56-
update,
57-
remove,
57+
//export crud functions
58+
59+
export default{
60+
create,read,update,remove,
5861
};
62+

routes/activity.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 activityController from "#controller/activity";
3+
4+
const router=express.Router();
5+
router.post("/add",activityController.addActivity);
6+
router.get("/list",activityController.getActivity);
7+
router.post("/update/:id",activityController.updateActivity);
8+
router.delete("/delete/:id",activityController.deleteActivity);
9+
10+
export default router;

services/activity.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Activity from "#models/activity" ;
2+
import databaseError from "#error/database";
3+
4+
export async function createActivity (activityBlueprint,startTime,duration,course,faculty,type,task,group,students){
5+
const newActivity = await Activity.create({
6+
activityBlueprint,startTime,duration,course,faculty,task,type,group,students,
7+
});
8+
if (newActivity){
9+
return newActivity;
10+
}
11+
throw new databaseError.DataEntryError("actvity");
12+
}
13+
14+
export async function updateActivityById(id,data){
15+
const updated= await Activity.update({_id:id},data);
16+
if (updated){
17+
return updated;
18+
}
19+
throw new databaseError.DataEntryError("activity");
20+
}
21+
22+
export async function activityList(filter){
23+
const activitylist = await Activity.read(filter,0);
24+
return activitylist;
25+
}
26+
27+
export async function deleteActivityById(id){
28+
const deleted = await Activity.remove({_id:id},data);
29+
if(deleted){
30+
return deleted;
31+
}
32+
throw new databaseError.DataDeleteError("activity");
33+
}

0 commit comments

Comments
 (0)