add & repair: roles schema,roles service & roles router, roles controller

This commit is contained in:
Muhammad Afif
2025-10-10 17:24:32 +07:00
parent 6c7d92deae
commit c51c686cce
7 changed files with 307 additions and 262 deletions

88
services/roles.service.js Normal file
View File

@@ -0,0 +1,88 @@
const {
getAllRolesDb,
getRolesByIdDb,
createRolesDB,
updateRolesDb,
deleteRolesDb
} = require('../db/roles.db');
const { ErrorHandler } = require('../helpers/error');
class RolesService {
// Get all Roles
static async getAllRoles(param) {
try {
const results = await getAllRolesDb(param);
results.data.map(element => {
});
return results
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Get Roles by ID
static async getRolesById(id) {
try {
const result = await getRolesByIdDb(id);
if (result.length < 1) throw new ErrorHandler(404, 'Roles not found');
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Create Roles
static async createRoles(data) {
try {
if (!data || typeof data !== 'object') data = {};
const result = await createRolesDB(data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Update Roles
static async updateRoles(id, data) {
try {
if (!data || typeof data !== 'object') data = {};
const dataExist = await getRolesByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Roles not found');
}
const result = await updateRolesDb(id, data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Soft delete Roles
static async deleteRoles(id, userId) {
try {
const dataExist = await getRolesByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Roles not found');
}
const result = await deleteRolesDb(id, userId);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
}
module.exports = RolesService;