91 lines
2.7 KiB
JavaScript
91 lines
2.7 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 with nested error codes and solutions
|
|
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.createBrandWithFullData(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;
|
|
|
|
// Debug logging untuk lihat request body
|
|
console.log('🔍 BE Raw Request Body:', req.body);
|
|
console.log('🔍 BE Request Headers:', req.headers);
|
|
console.log('🔍 BE Request Method:', req.method);
|
|
|
|
const { error, value } = await checkValidate(updateBrandSchema, req);
|
|
|
|
if (error) {
|
|
console.log('❌ BE Validation Error:', {
|
|
error,
|
|
details: error.details?.map(d => ({
|
|
field: d.path.join('.'),
|
|
message: d.message,
|
|
value: d.context?.value
|
|
})),
|
|
requestBody: req.body
|
|
});
|
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
|
}
|
|
|
|
value.updated_by = req.user?.user_id || null;
|
|
|
|
const results = await BrandService.updateBrandWithFullData(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; |