add: crud unit
This commit is contained in:
71
controllers/unit.controller.js
Normal file
71
controllers/unit.controller.js
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
const UnitService = require('../services/unit.service');
|
||||||
|
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
|
||||||
|
const { insertUnitSchema, updateUnitSchema } = require('../validate/unit.schema');
|
||||||
|
|
||||||
|
class UnitController {
|
||||||
|
// Get all units
|
||||||
|
static async getAll(req, res) {
|
||||||
|
const queryParams = req.query;
|
||||||
|
|
||||||
|
const results = await UnitService.getAllUnits(queryParams);
|
||||||
|
const response = await setResponsePaging(queryParams, results, 'Unit found');
|
||||||
|
|
||||||
|
res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get unit by ID
|
||||||
|
static async getById(req, res) {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
const results = await UnitService.getUnitById(id);
|
||||||
|
const response = await setResponse(results, 'Unit found');
|
||||||
|
|
||||||
|
res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create unit
|
||||||
|
static async create(req, res) {
|
||||||
|
const { error, value } = await checkValidate(insertUnitSchema, req);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
||||||
|
}
|
||||||
|
|
||||||
|
value.created_by = req.user.user_id;
|
||||||
|
|
||||||
|
const results = await UnitService.createUnit(value);
|
||||||
|
const response = await setResponse(results, 'Unit created successfully');
|
||||||
|
|
||||||
|
return res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update unit
|
||||||
|
static async update(req, res) {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
const { error, value } = checkValidate(updateUnitSchema, req);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
||||||
|
}
|
||||||
|
|
||||||
|
value.updated_by = req.user.user_id;
|
||||||
|
|
||||||
|
const results = await UnitService.updateUnit(id, value);
|
||||||
|
const response = await setResponse(results, 'Unit updated successfully');
|
||||||
|
|
||||||
|
res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Soft delete unit
|
||||||
|
static async delete(req, res) {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
const results = await UnitService.deleteUnit(id, req.user.user_id);
|
||||||
|
const response = await setResponse(results, 'Unit deleted successfully');
|
||||||
|
|
||||||
|
res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = UnitController;
|
||||||
88
services/unit.service.js
Normal file
88
services/unit.service.js
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
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;
|
||||||
Reference in New Issue
Block a user