example for template master device

This commit is contained in:
2025-10-10 14:53:43 +07:00
parent 6eed13bc4f
commit ab3b38eb49
9 changed files with 266 additions and 226 deletions

View File

@@ -1,101 +1,70 @@
const DeviceService = require('../services/device.service');
const { deviceSchema, deviceUpdateSchema } = require('../helpers/validation');
const { setResponse } = require('../helpers/utils');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const { insertDeviceSchema, updateDeviceSchema } = require('../validate/device.schema');
class DeviceController {
// Get all devices
static async getAll(req, res) {
try {
const { search } = req.query;
const devices = await DeviceService.getAllDevices(search || '');
return res.status(200).json(setResponse(devices, 'Devices retrieved successfully', 200));
} catch (err) {
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Failed to retrieve devices', err.statusCode || 500)
);
}
static async getAll(req, res) {
const queryParams = req.query;
const results = await DeviceService.getAllDevices(queryParams);
const response = await setResponsePaging(queryParams, results, 'Device found')
res.status(response.statusCode).json(response);
}
// Get device by ID
static async getById(req, res) {
try {
const { id } = req.params;
const device = await DeviceService.getDeviceById(id);
return res.status(200).json(setResponse(device, 'Device retrieved successfully', 200));
} catch (err) {
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Device not found', err.statusCode || 500)
);
}
const { id } = req.params;
const results = await DeviceService.getDeviceById(id);
const response = await setResponse(results, 'Device found')
res.status(response.statusCode).json(response);
}
// Create device
static async create(req, res) {
try {
const { error, value } = deviceSchema.validate(req.body || {}, { abortEarly: false });
if (error) {
const errors = error.details.reduce((acc, cur) => {
const field = Array.isArray(cur.path) ? cur.path.join('.') : String(cur.path);
if (!acc[field]) acc[field] = [];
acc[field].push(cur.message);
return acc;
}, {});
return res.status(400).json(setResponse(errors, 'Validation failed', 400));
}
const { error, value } = await checkValidate(insertDeviceSchema, req)
const newDevice = await DeviceService.createDevice(value, req.user.user_id);
return res.status(201).json(
setResponse(newDevice, 'Device created successfully', 201)
);
} catch (err) {
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Failed to create device', err.statusCode || 500)
);
if (error) {
return res.status(400).json(setResponse(error, 'Validation failed', 400));
}
value.userId = req.user.user_id
const results = await DeviceService.createDevice(value);
const response = await setResponse(results, 'Device created successfully')
return res.status(response.statusCode).json(response);
}
// Update device
static async update(req, res) {
try {
const { id } = req.params;
const { error, value } = deviceUpdateSchema.validate(req.body || {}, { abortEarly: false });
if (error) {
const errors = error.details.reduce((acc, cur) => {
const field = Array.isArray(cur.path) ? cur.path.join('.') : String(cur.path);
if (!acc[field]) acc[field] = [];
acc[field].push(cur.message);
return acc;
}, {});
return res.status(400).json(setResponse(errors, 'Validation failed', 400));
}
const { id } = req.params;
const updatedDevice = await DeviceService.updateDevice(id, value, req.user.user_Id);
const { error, value } = checkValidate(updateDeviceSchema, req)
return res.status(200).json(
setResponse(updatedDevice.data, 'Device updated successfully', 200)
);
} catch (err) {
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Failed to update device', err.statusCode || 500)
);
if (error) {
return res.status(400).json(setResponse(error, 'Validation failed', 400));
}
value.userId = req.user.user_id
const results = await DeviceService.updateDevice(id, value);
const response = await setResponse(results, 'Device updated successfully')
res.status(response.statusCode).json(response);
}
// Soft delete device
static async delete(req, res) {
try {
const { id } = req.params;
const { id } = req.params;
await DeviceService.deleteDevice(id, req.user.userId);
return res.status(200).json(
setResponse([], 'Device deleted successfully', 200)
);
} catch (err) {
return res.status(err.statusCode || 500).json(
setResponse([], err.message || 'Failed to delete device', err.statusCode || 500)
);
}
const results = await DeviceService.deleteDevice(id, req.user.user_id);
const response = await setResponse(results, 'Device deleted successfully')
res.status(response.statusCode).json(response);
}
}