Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4c78a87
Merge branch 'create-crud-for-students' of https://github.com/devcare…
JudySeyram Nov 14, 2022
fb72927
Update models to use UUIDs
talktonok Nov 17, 2022
8c995a5
Merge branch 'development' of https://github.com/devcareer/class-mana…
talktonok Nov 17, 2022
4323f64
create seed files
talktonok Nov 17, 2022
4c63ed7
create seed files
talktonok Nov 17, 2022
c9012f7
chore(assignment crud):add crud endpoints for assignments
JudySeyram Nov 24, 2022
5aaadf4
create seeders
talktonok Nov 28, 2022
5b03465
feat(README.md restructure):restructure the README.md file
JudySeyram Nov 28, 2022
9d18b21
resolve conflicts
bellogo Nov 30, 2022
b9a869a
Merge branch 'development' of https://github.com/devcareer/class-mana…
talktonok Nov 30, 2022
d8db09a
update class student
talktonok Nov 30, 2022
593b1f5
seed database
talktonok Dec 1, 2022
68ec1b7
edit README.md
JudySeyram Dec 1, 2022
dac235d
Merge branch 'create-assignment-CRUD' of https://github.com/devcareer…
JudySeyram Dec 1, 2022
07d64b9
initial commit
JudySeyram Dec 1, 2022
8966bf3
install passport
talktonok Dec 1, 2022
f337418
install passport
talktonok Dec 1, 2022
825e2c4
user registration and login
talktonok Dec 2, 2022
fb53e2a
user registration and login
talktonok Dec 2, 2022
944e4aa
chore(upload assignment):add route for student assignment upload
Hazeem01 Dec 5, 2022
6fcb91f
email verification
talktonok Dec 5, 2022
855538f
testing
JudySeyram Dec 7, 2022
e611867
delete score files
JudySeyram Dec 10, 2022
631fb3c
correction
JudySeyram Dec 10, 2022
8f765ca
correction
JudySeyram Dec 10, 2022
35cd0ee
Authenticate verify users
talktonok Dec 12, 2022
26cb6fe
updating branch
JudySeyram Dec 12, 2022
27adfe6
Merge branch 'feat-authentication' of https://github.com/devcareer/cl…
JudySeyram Dec 12, 2022
2111b12
update
talktonok Dec 12, 2022
d3dad2a
Merge branch 'feat-authentication' of https://github.com/devcareer/cl…
JudySeyram Dec 12, 2022
801345d
assignment update
JudySeyram Dec 12, 2022
24b77af
fix error
talktonok Dec 13, 2022
a8bce88
fix error
talktonok Dec 13, 2022
6d79dee
Merge pull
talktonok Dec 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions src/api/controllers/assignmentsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import AssignmentService from '../services/AssignmentService.js';
import Util from '../utils/Utils.js';

const util = new Util();

class AssignmentController {
static async home(req, res) {
res.send(`<h1>Welcome to the assignment Route</h1>`)
}

static async getAllAssignments(req, res) {
try {
const allAssignments = await AssignmentService.getAllAssignments();
if (allAssignments.length > 0) {
util.setSuccess(200, 'Assignments retrieved', allAssignments);
} else {
util.setSuccess(200, 'No Assignment found in the database');
}
return util.send(res);
} catch (error) {
util.setError(400, error);
return util.send(res);
}
}

static async addAssignment(req, res) {
if (!req.body.Assignment || !req.body.senderId || !req.body.receiverId) {
util.setError(400, 'Please provide complete details of the Assignment');
return util.send(res);
}
const newAssignment = req.body;
try {
const createdAssignment = await AssignmentService.addAssignment(newAssignment);
util.setSuccess(201, 'Assignment Added Successfully!', createdAssignment);
return util.send(res);
} catch (error) {
util.setError(400, error.Assignment);
return util.send(res);
}
}

static async updatedAssignment(req, res) {
const alteredAssignment = req.body;
const { id } = req.params;
if (!Number(id)) {
util.setError(400, 'Please input a valid numeric value');
return util.send(res);
}
try {
const updateAssignment = await AssignmentService.updateAssignment(id, alteredAssignment);
if (!updateAssignment) {
util.setError(404, `Cannot find a Assignment with the id: ${id}`);
} else {
util.setSuccess(200, 'Assignment updated', updateAssignment);
}
return util.send(res);
} catch (error) {
util.setError(404, error);
return util.send(res);
}
}

static async getAAssignment(req, res) {
const { id } = req.params;

if (!Number(id)) {
util.setError(400, 'Please input a valid numeric value');
return util.send(res);
}

try {
const theAssignment = await AssignmentService.getAAssignment(id);

if (!theAssignment) {
util.setError(404, `Cannot find a Assignment with the id ${id}`);
} else {
util.setSuccess(200, 'Found Assignment', theAssignment);
}
return util.send(res);
} catch (error) {
util.setError(404, error);
return util.send(res);
}
}

static async deleteAssignment(req, res) {
const { id } = req.params;

if (!Number(id)) {
util.setError(400, 'Please provide a numeric value');
return util.send(res);
}

try {
const AssignmentToDelete = await AssignmentService.deleteAssignment(id);

if (AssignmentToDelete) {
util.setSuccess(200, `Assignment with ${id} deleted`);
} else {
util.setError(404, `Assignment with the id ${id} cannot be found`);
}
return util.send(res);
} catch (error) {
util.setError(400, error);
return util.send(res);
}
}
}

export default AssignmentController;
12 changes: 12 additions & 0 deletions src/api/routers/assignmentRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from "express";
const assignmentRouter = Router();
import AssignmentController from '../controllers/assignmentsController.js';

assignmentRouter.get('/home', AssignmentController.home);
assignmentRouter.get('/', AssignmentController.getAllAssignments);
assignmentRouter.post('/', AssignmentController.addAssignment);
assignmentRouter.get('/:id', AssignmentController.getAAssignment);
assignmentRouter.put('/:id', AssignmentController.updatedAssignment);
assignmentRouter.delete('/:id', AssignmentController.deleteAssignment);

export { assignmentRouter };
68 changes: 68 additions & 0 deletions src/api/services/assignmentService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import database from '../db/models/index.js';

class AssignmentService {
static async getAllAssignments() {
try {
const result = await database.Assignment.findAll();
return result;
} catch (error) {
throw error;
}
}

static async addAssignment(newAssignment) {
try {
const result = await database.Assignment.create(newAssignment);
return result;
} catch (error) {
throw error;
}
}

static async updateAssignment(id, updateAssignment) {
try {
const AssignmentToUpdate = await database.Assignment.findOne({
where: { id: Number(id) }
});

if (AssignmentToUpdate) {
await database.Assignment.update(updateAssignment, { where: { id: Number(id) } });

return updateAssignment;
}
return null;
} catch (error) {
throw error;
}
}

static async getAAssignment(id) {
try {
const aAssignment = await database.Assignment.findOne({
where: { id: Number(id) }
});

return aAssignment;
} catch (error) {
throw error;
}
}

static async deleteAssignment(id) {
try {
const AssignmentToDelete = await database.Assignment.findOne({ where: { id: Number(id) } });

if (AssignmentToDelete) {
const deletedAssignment = await database.Assignment.destroy({
where: { id: Number(id) }
});
return deletedAssignment;
}
return null;
} catch (error) {
throw error;
}
}
}

export default AssignmentService;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import dotenv from "dotenv";
import messageRouter from "./api/routers/MessageRouter.js";
import { studentRouter } from './api/routers/studentsRoutes.js';
import { assignmentRouter } from './api/routers/assignmentRouter.js';
const app = express();
dotenv.config();

Expand All @@ -10,6 +11,7 @@ app.use(express.json());
app.use(express.urlencoded());
app.use('/api/message', messageRouter);
app.use('/api/students', studentRouter);
app.use('/api/assignment', assignmentRouter);


app.get('/', (req, res) => {
Expand Down