Files
cod-api/services/unit.service.js
2025-10-17 11:30:55 +07:00

88 lines
1.9 KiB
JavaScript

const {
getAllUnitsDb,
getUnitByIdDb,
createUnitDb,
updateUnitDb,
deleteUnitDb
} = require('../db/unit.db');
const { ErrorHandler } = require('../helpers/error');
class UnitService {
// Get all units
static async getAllUnits(param) {
try {
const results = await getAllUnitsDb(param);
results.data.map(element => {
});
return results;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Get unit by ID
static async getUnitById(id) {
try {
const result = await getUnitByIdDb(id);
if (result.length < 1) throw new ErrorHandler(404, 'Unit not found');
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Create unit
static async createUnit(data) {
try {
if (!data || typeof data !== 'object') data = {};
const result = await createUnitDb(data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Update unit
static async updateUnit(id, data) {
try {
if (!data || typeof data !== 'object') data = {};
const dataExist = await getUnitByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Unit not found');
}
const result = await updateUnitDb(id, data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Soft delete unit
static async deleteUnit(id, userId) {
try {
const dataExist = await getUnitByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Unit not found');
}
const result = await deleteUnitDb(id, userId);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
}
module.exports = UnitService;