crud: brand sparepart
This commit is contained in:
99
controllers/brand_sparepart.controller.js
Normal file
99
controllers/brand_sparepart.controller.js
Normal file
@@ -0,0 +1,99 @@
|
||||
const BrandSparepartService = require('../services/brand_sparepart.service');
|
||||
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
|
||||
const {
|
||||
insertBrandSparepartSchema,
|
||||
updateBrandSparepartSchema,
|
||||
} = require('../validate/brand_sparepart.schema');
|
||||
|
||||
class BrandSparepartController {
|
||||
static async getAll(req, res) {
|
||||
const queryParams = req.query;
|
||||
|
||||
const results = await BrandSparepartService.getAllBrandSparepart(queryParams);
|
||||
const response = await setResponsePaging(queryParams, results, 'Brand sparepart found');
|
||||
|
||||
res.status(response.statusCode).json(response);
|
||||
}
|
||||
|
||||
static async getById(req, res) {
|
||||
const { id } = req.params;
|
||||
|
||||
const results = await BrandSparepartService.getBrandSparepartById(id);
|
||||
const response = await setResponse(results, 'Brand sparepart found');
|
||||
|
||||
res.status(response.statusCode).json(response);
|
||||
}
|
||||
|
||||
static async create(req, res) {
|
||||
const { error, value } = await checkValidate(insertBrandSparepartSchema, req);
|
||||
|
||||
if (error) {
|
||||
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
||||
}
|
||||
|
||||
try {
|
||||
if (req.file) {
|
||||
const file = req.file;
|
||||
const ext = require('path').extname(file.originalname).toLowerCase();
|
||||
const typeDoc = ext === ".pdf" ? "PDF" : "IMAGE";
|
||||
const folder = typeDoc === "PDF" ? "pdf" : "images";
|
||||
const pathDocument = `${folder}/${file.filename}`;
|
||||
|
||||
value.path_foto = pathDocument;
|
||||
}
|
||||
|
||||
value.created_by = req.user?.user_id || null;
|
||||
|
||||
const results = await BrandSparepartService.createBrandSparepart(value);
|
||||
const response = await setResponse(results, 'Brand sparepart created successfully');
|
||||
|
||||
return res.status(response.statusCode).json(response);
|
||||
} catch (err) {
|
||||
const response = setResponse([], err.message, err.statusCode || 500);
|
||||
res.status(response.statusCode).json(response);
|
||||
}
|
||||
}
|
||||
|
||||
static async update(req, res) {
|
||||
const { id } = req.params;
|
||||
|
||||
const { error, value } = await checkValidate(updateBrandSparepartSchema, req);
|
||||
if (error) {
|
||||
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
||||
}
|
||||
|
||||
try {
|
||||
if (req.file) {
|
||||
const file = req.file;
|
||||
const ext = require('path').extname(file.originalname).toLowerCase();
|
||||
const typeDoc = ext === ".pdf" ? "PDF" : "IMAGE";
|
||||
const folder = typeDoc === "PDF" ? "pdf" : "images";
|
||||
const pathDocument = `${folder}/${file.filename}`;
|
||||
|
||||
value.path_foto = pathDocument;
|
||||
}
|
||||
|
||||
value.updated_by = req.user?.user_id || null;
|
||||
|
||||
const results = await BrandSparepartService.updateBrandSparepart(id, value);
|
||||
const response = await setResponse(results, 'Brand sparepart updated successfully');
|
||||
|
||||
res.status(response.statusCode).json(response);
|
||||
|
||||
} catch (err) {
|
||||
const response = setResponse([], err.message, err.statusCode || 500);
|
||||
res.status(response.statusCode).json(response);
|
||||
}
|
||||
}
|
||||
|
||||
static async delete(req, res) {
|
||||
const { id } = req.params;
|
||||
|
||||
const results = await BrandSparepartService.deleteBrandSparepart(id, req.user.user_id);
|
||||
const response = await setResponse(results, 'Brand sparepart deleted successfully');
|
||||
|
||||
res.status(response.statusCode).json(response);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BrandSparepartController;
|
||||
Reference in New Issue
Block a user