add crud m_status
This commit is contained in:
92
services/status.service.js
Normal file
92
services/status.service.js
Normal file
@@ -0,0 +1,92 @@
|
||||
const {
|
||||
getAllStatusDb,
|
||||
getStatusByIdDb,
|
||||
createStatusDb,
|
||||
updateStatusDb,
|
||||
deleteStatusDb,
|
||||
checkStatusNumberExistsDb
|
||||
} = require('../db/status.db');
|
||||
const { ErrorHandler } = require('../helpers/error');
|
||||
|
||||
class StatusService {
|
||||
// Get all status
|
||||
static async getAllStatus(param) {
|
||||
try {
|
||||
const results = await getAllStatusDb(param);
|
||||
|
||||
results.data.map(element => {
|
||||
});
|
||||
|
||||
return results;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Get status by ID
|
||||
static async getStatusById(id) {
|
||||
try {
|
||||
const result = await getStatusByIdDb(id);
|
||||
|
||||
if (result.length < 1) throw new ErrorHandler(404, 'Status not found');
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
static async createStatus(data) {
|
||||
try {
|
||||
if (!data || typeof data !== 'object') data = {};
|
||||
|
||||
if (data.status_number) {
|
||||
const exists = await checkStatusNumberExistsDb(data.status_number);
|
||||
if (exists) throw new ErrorHandler(400, 'Status number already exists');
|
||||
}
|
||||
|
||||
const result = await createStatusDb(data);
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Update status
|
||||
static async updateStatus(id, data) {
|
||||
try {
|
||||
if (!data || typeof data !== 'object') data = {};
|
||||
|
||||
const dataExist = await getStatusByIdDb(id);
|
||||
|
||||
if (dataExist.length < 1) {
|
||||
throw new ErrorHandler(404, 'Status not found');
|
||||
}
|
||||
|
||||
const result = await updateStatusDb(id, data);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Soft delete status
|
||||
static async deleteStatus(id, userId) {
|
||||
try {
|
||||
const dataExist = await getStatusByIdDb(id);
|
||||
|
||||
if (dataExist.length < 1) {
|
||||
throw new ErrorHandler(404, 'Status not found');
|
||||
}
|
||||
|
||||
const result = await deleteStatusDb(id, userId);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = StatusService;
|
||||
Reference in New Issue
Block a user