Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2260a7e
refactor: migrate DepartmentController to ES module syntax and improv…
agaesh Oct 9, 2025
c3f00c8
refactor: update createDepartment function to use ES module syntax an…
agaesh Oct 9, 2025
2dec648
refactor: update updateDepartment function to use ES module syntax
agaesh Oct 9, 2025
55bdc5f
refactor: add getDepartmentByID and deleteDepartment functions with p…
agaesh Oct 9, 2025
0b51fb7
refactor: migrate InstructorController to ES module syntax and improv…
agaesh Oct 9, 2025
290bd61
refactor: update UpdateInstructor and DeleteInstructor functions to u…
agaesh Oct 9, 2025
f68e5f5
refactor: clean up CreateInstructor and UpdateInstructor functions fo…
agaesh Oct 9, 2025
5da1844
refactor: migrate ProgramCourseController to ES module syntax and imp…
agaesh Oct 9, 2025
2f7f8f3
refactor: migrate getAllPrograms and UpdateProgram to ES module syntax
agaesh Oct 9, 2025
53ea0fa
refactor: migrate DeleteProgram to ES module syntax
agaesh Oct 9, 2025
6bd7840
refactor: migrate userController to ES module syntax and improve func…
agaesh Oct 9, 2025
6c7c960
refactor: streamline LoginUser function by reorganizing cookie settin…
agaesh Oct 9, 2025
4eac57b
fix(ci): replace npm ci with npm install to remove lockfile dependency
agaesh Oct 10, 2025
a4b518d
fix(ci): replace npm ci with npm install to remove lockfile dependency
agaesh Oct 10, 2025
dfb7824
Merge branch 'fix/c-no-lock-file' of https://github.com/agaesh/Enroll…
agaesh Oct 10, 2025
9e1f62a
Merge pull request #58 from agaesh/fix/c-no-lock-file
agaesh Oct 10, 2025
525cc4f
refactor: migrate DepartmentController to ES module syntax and improv…
agaesh Oct 9, 2025
470f20d
refactor: update createDepartment function to use ES module syntax an…
agaesh Oct 9, 2025
743f797
refactor: update updateDepartment function to use ES module syntax
agaesh Oct 9, 2025
b041b48
refactor: add getDepartmentByID and deleteDepartment functions with p…
agaesh Oct 9, 2025
f0ba9ca
refactor: migrate InstructorController to ES module syntax and improv…
agaesh Oct 9, 2025
b461117
refactor: update UpdateInstructor and DeleteInstructor functions to u…
agaesh Oct 9, 2025
6e11e38
refactor: clean up CreateInstructor and UpdateInstructor functions fo…
agaesh Oct 9, 2025
95afc33
refactor: migrate ProgramCourseController to ES module syntax and imp…
agaesh Oct 9, 2025
4422ec1
refactor: migrate getAllPrograms and UpdateProgram to ES module syntax
agaesh Oct 9, 2025
f99e6c3
refactor: migrate DeleteProgram to ES module syntax
agaesh Oct 9, 2025
97feb94
refactor: migrate userController to ES module syntax and improve func…
agaesh Oct 9, 2025
17ce378
refactor: streamline LoginUser function by reorganizing cookie settin…
agaesh Oct 9, 2025
d97abdf
Merge branch 'feature/es6-Controllers' of https://github.com/agaesh/E…
agaesh Oct 10, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/enforce-supertest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
node-version: '22'

- name: Install dependencies
run: npm ci
run: npm Install

- name: Run tests
run: npm run test
94 changes: 46 additions & 48 deletions src/api/Controllers/DepartmentController.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,54 @@
const DepartmentService = require('../Services/DepartmentServices');
const path = require('path');
const base = path.resolve(__dirname, '../../../');
const sequelize = require(path.join(base, 'src', 'config', 'db.js'));
const { DataTypes } = require('sequelize');
const { UpdateProgram } = require('./ProgramCourseController');
const { error } = require('console');
const DepartmentModel = require('../Models/department')(sequelize, DataTypes)
import DepartmentService from '../Services/DepartmentServices.js';
import path from 'path';
import { fileURLToPath } from 'url';
import { DataTypes } from 'sequelize';
import { UpdateProgram } from './ProgramCourseController.js';
import sequelize from '../../../src/config/db.js';
import defineDepartmentModel from '../Models/department.js';

exports.getAllDeparments = async(req, res)=>{
try{
// Fix __dirname in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

let {top, page, limit} = req.body
// Initialize model
const DepartmentModel = defineDepartmentModel(sequelize, DataTypes);

// βœ… Get All Departments
export const getAllDepartments = async (req, res) => {
try {
let { top, page, limit } = req.body;

// Convert string query params to numbers
top = parseInt(top);
page = parseInt(page);
limit = parseInt(limit);

let queryOptions = {
order: [['createdAt', 'DESC']] // latest first
const queryOptions = {
order: [['createdAt', 'DESC']], // latest first
};

if (top) {
// Return only top X records
queryOptions.limit = top;
}
else if (page && limit) {
// Apply pagination
const offset = (page - 1) * limit;
} else if (page && limit) {
queryOptions.limit = limit;
queryOptions.offset = offset;
queryOptions.offset = (page - 1) * limit;
}

DepartmentModel.findAll(queryOptions)
.then((departments) => {
res.status(200).json({
status: 'success',
data: departments.toJson()
});
})
.catch((error) => {
res.status(500).json({
status: 'error',
message: error.message
});
});
const departments = await DepartmentModel.findAll(queryOptions);

}catch(error){
res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' });
}
}


exports.getDepartmentByID = async (req, res) => {
res.status(200).json({
status: 'success',
data: departments, // No need for toJson()
});
} catch (error) {
res
.status(error.status || 500)
.json({ error: error.message || 'Internal Server Error' });
}
};
// βœ… Get Department by ID
export const getDepartmentByID = async (req, res) => {
try {
const { id } = req.params;
const department = await DepartmentService.getDepartmentByID(id);
Expand All @@ -68,10 +64,11 @@ exports.getDepartmentByID = async (req, res) => {
}
};

exports.createDeparment = async (req, res) => {
// βœ… Create Department
export const createDepartment = async (req, res) => {
try {
const addDeparment = await DepartmentService.CreateDeparment(req.body);
if(addDeparment){
const addDepartment = await DepartmentService.CreateDeparment(req.body);
if (addDepartment) {
res.status(201).json({
message: addDeparment.message,
data: addDeparment.data
Expand All @@ -82,7 +79,8 @@ exports.createDeparment = async (req, res) => {
}
};

exports.UpdateDepartment = async (req, res) => {
// βœ… Update Department
export const updateDepartment = async (req, res) => {
try {
const update = await DepartmentService.UpdateDepartment(req, res);
res.status(201).json({ message: update.message });
Expand All @@ -91,11 +89,11 @@ exports.UpdateDepartment = async (req, res) => {
}
};

exports.DeleteDepartment = async(req, res)=>{
try{
const deleteDepartment = await DepartmentService.DeleteDepartment(req,res)
}catch(error){
// βœ… Delete Department
export const deleteDepartment = async (req, res) => {
try {
await DepartmentService.DeleteDepartment(req, res);
} catch (error) {
res.status(500).json({ message: error.message });
}
}

};
88 changes: 50 additions & 38 deletions src/api/Controllers/InstructorController.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
const InstructorService = require('../Services/InstructorServices');
const path = require('path');
const base = path.resolve(__dirname, '../../../');
const sequelize = require(path.join(base, 'src', 'config', 'db.js'));
const { DataTypes } = require('sequelize');
const { error } = require('console');
const User = require("../Models/User")(sequelize, DataTypes)
const InstructorModel = require('../Models/instructor')(sequelize, DataTypes)
const { verifyWebToken } = require("../../Middlewares/authMiddleware");
import InstructorService from "../Services/InstructorServices.js";
import path from "path";
import { fileURLToPath } from "url";
import { DataTypes } from "sequelize";
import sequelize from "../../config/db.js";
import UserModelDefiner from "../Models/user.js";
import InstructorModelDefiner from "../Models/instructor.js";
import { verifyWebToken } from "../../Middlewares/authMiddleware.js";

exports.CreateInstructor = async (req, res) => {
try {
// Resolve __dirname (ESM compatible)
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const base = path.resolve(__dirname, "../../../");

const {id, ...data} = req.body
const User = UserModelDefiner(sequelize, DataTypes);
const Instructor = InstructorModelDefiner(sequelize, DataTypes);

// βœ… Create Instructor
export const CreateInstructor = async (req, res) => {
try {
const {id, ...data} = req.body
const findInstructor = await Instructor.findOne({ where: { user_id: id } });

if (findInstructor) {
Expand All @@ -35,29 +41,35 @@ exports.CreateInstructor = async (req, res) => {
});
}
};
exports.UpdateInstructor = async(req, res)=>{
try{
const update = await InstructorService.UpdateInstructor(req.param.id, req.body);

return res.status(201).JSON({
success: true,
message: update.message
})
}catch(error ){
throw error;
}
}
exports.DeleteInstructor = async (req, res) => {
try {
const DeleteInstructor = await InstructorService.DeleteInstructor(req.params.id);
return res.status(200).json({
success:true,
message: "Instructor deleted successfully"
});
} catch (error) {
return res.status(500).json({
message: "Error deleting instructor",
error: error.message
});
}
};
// βœ… Update Instructor
export const UpdateInstructor = async (req, res) => {
try {
const update = await InstructorService.UpdateInstructor(req.params.id, req.body);

return res.status(201).json({
success: true,
message: update.message,
});
} catch (error) {
return res.status(500).json({
message: "Error updating instructor",
error: error.message,
});
}
};
// βœ… Delete Instructor
export const DeleteInstructor = async (req, res) => {
try {
await InstructorService.DeleteInstructor(req.params.id);

return res.status(200).json({
success: true,
message: "Instructor deleted successfully",
});
} catch (error) {
return res.status(500).json({
message: "Error deleting instructor",
error: error.message,
});
}
};
47 changes: 28 additions & 19 deletions src/api/Controllers/ProgramCourseController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const ProgramService = require("../Services/ProgramCourseServices")
const path = require('path');
const base = path.resolve(__dirname, '../../../');
const sequelize = require(path.join(base, 'src', 'config', 'db.js'));
const { DataTypes } = require('sequelize');
const ProgramCourse = require('../Models/programcourse')(sequelize, DataTypes)
import ProgramService from "../Services/ProgramCourseServices.js";
import path from "path";
import { fileURLToPath } from "url";
import { DataTypes } from "sequelize";
import sequelize from "../../../src/config/db.js";
import defineProgramCourse from "../Models/programcourse.js";

exports.createProgram = async (req, res) => {
const ProgramCourse = defineProgramCourse(sequelize, DataTypes);
const __dirname = path.dirname(fileURLToPath(import.meta.url));

// βœ… Create Program
export const createProgram = async (req, res) => {
try {
const addProgram = await ProgramService.createProgram(req.body);
if(addProgram){
Expand All @@ -19,7 +23,8 @@ exports.createProgram = async (req, res) => {
}
};

exports.getAllPrograms = async (req, res) => {
// βœ… Get All Programs
export const getAllPrograms = async (req, res) => {
try {
let { top, page, limit } = req.body;

Expand All @@ -28,8 +33,8 @@ exports.getAllPrograms = async (req, res) => {
page = parseInt(page);
limit = parseInt(limit);

let queryOptions = {
order: [['createdAt', 'DESC']] // latest first
const queryOptions = {
order: [["createdAt", "DESC"]],
};

if (top) {
Expand All @@ -46,18 +51,20 @@ exports.getAllPrograms = async (req, res) => {
const programs = await ProgramCourse.findAll(queryOptions);

res.status(200).json({
status: 'success',
data: programs
status: "success",
data: programs,
});

} catch (error) {
res.status(500).json({
status: 'error',
message: error.message
res.status(500).json({
status: "error",
message: error.message,
});
}
};
exports.UpdateProgram = async (req, res) => {

// βœ… Update Program
export const UpdateProgram = async (req, res) => {
try {
const result = await ProgramService.UpdateProgram(req.body);

Expand All @@ -82,6 +89,8 @@ exports.UpdateProgram = async (req, res) => {
});
}
};
exports.DeleteProgram = async(id)=>{
return await ProgramService.DeleteProgram(id)
}

// βœ… Delete Program
export const DeleteProgram = async (id) => {
return await ProgramService.DeleteProgram(id);
};
36 changes: 19 additions & 17 deletions src/api/Controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const userService = require('../Services/UserService');
import userService from "../Services/UserService.js";

exports.registerUser = async (req, res) => {
// βœ… Register User
export const registerUser = async (req, res) => {
try {
const newUser = await userService.registerUser(req.body);
if(newUser){
Expand All @@ -14,24 +15,25 @@ exports.registerUser = async (req, res) => {
}
};

exports.LoginUser = async(req,res)=>{
try{
const isLogin = await userService.LoginUser(req.body)
// βœ… Login User
export const LoginUser = async (req, res) => {
try {
const isLogin = await userService.LoginUser(req.body);

res.cookie('token', isLogin.token, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict',
maxAge: 60 * 60 * 1000
});

if(isLogin) {
return res.status(200).json({
message: 'Login successful',
if(isLogin) {
res.cookie("token", isLogin.token, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict',
maxAge: 60 * 60 * 1000
});
}

return res.status(200).json({
message: 'Login successful',
});
}
}catch(error){
res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' });
}
}
};
Loading