77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
const BrandService = require('../services/brand.service');
|
|
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
|
|
const {
|
|
insertBrandSchema,
|
|
updateBrandSchema,
|
|
} = require('../validate/brand.schema');
|
|
|
|
class BrandController {
|
|
// Get all brands
|
|
static async getAll(req, res) {
|
|
const queryParams = req.query;
|
|
|
|
const results = await BrandService.getAllBrands(queryParams);
|
|
const response = await setResponsePaging(queryParams, results, 'Brand found');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Get brand by ID
|
|
static async getById(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const results = await BrandService.getBrandById(id);
|
|
|
|
// console.log('Brand response structure:', JSON.stringify(results, null, 2));
|
|
|
|
const response = await setResponse(results, 'Brand found');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Create brand
|
|
static async create(req, res) {
|
|
const { error, value } = await checkValidate(insertBrandSchema, req);
|
|
|
|
if (error) {
|
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
|
}
|
|
|
|
value.created_by = req.user?.user_id || null;
|
|
|
|
const results = await BrandService.createBrand(value);
|
|
const response = await setResponse(results, 'Brand created successfully');
|
|
|
|
return res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Update brand
|
|
static async update(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const { error, value } = await checkValidate(updateBrandSchema, req);
|
|
|
|
if (error) {
|
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
|
}
|
|
|
|
value.updated_by = req.user?.user_id || null;
|
|
|
|
const results = await BrandService.updateBrand(id, value);
|
|
const response = await setResponse(results, 'Brand updated successfully');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Soft delete brand by ID
|
|
static async delete(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const results = await BrandService.deleteBrand(id, req.user.user_id);
|
|
const response = await setResponse(results, 'Brand deleted successfully');
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
}
|
|
|
|
module.exports = BrandController; |