123 lines
3.4 KiB
JavaScript
123 lines
3.4 KiB
JavaScript
// Brand operations
|
|
const {
|
|
getAllBrandsDb,
|
|
getBrandByIdDb,
|
|
createBrandDb,
|
|
updateBrandDb,
|
|
deleteBrandDb,
|
|
checkBrandNameExistsDb,
|
|
} = require("../db/brand.db");
|
|
const { ErrorHandler } = require("../helpers/error");
|
|
|
|
class BrandService {
|
|
// Get all brands
|
|
static async getAllBrands(param) {
|
|
try {
|
|
const results = await getAllBrandsDb(param);
|
|
|
|
return {
|
|
...results,
|
|
data: results.data
|
|
};
|
|
} catch (error) {
|
|
throw new ErrorHandler(error.statusCode, error.message);
|
|
}
|
|
}
|
|
|
|
// Get brand by ID (without error codes)
|
|
static async getBrandById(id) {
|
|
try {
|
|
const brand = await getBrandByIdDb(id);
|
|
if (!brand) throw new ErrorHandler(404, "Brand not found");
|
|
|
|
return brand;
|
|
} catch (error) {
|
|
throw new ErrorHandler(error.statusCode, error.message);
|
|
}
|
|
}
|
|
|
|
|
|
// Create brand
|
|
static async createBrand(data) {
|
|
try {
|
|
if (data.brand_name) {
|
|
const brandExists = await checkBrandNameExistsDb(data.brand_name);
|
|
if (brandExists) {
|
|
throw new ErrorHandler(400, "Brand name already exists");
|
|
}
|
|
}
|
|
|
|
const brandData = {
|
|
brand_name: data.brand_name,
|
|
brand_type: data.brand_type,
|
|
brand_manufacture: data.brand_manufacture,
|
|
brand_model: data.brand_model,
|
|
is_active: data.is_active !== undefined ? data.is_active : true,
|
|
created_by: data.created_by,
|
|
};
|
|
|
|
const createdBrand = await createBrandDb(brandData);
|
|
if (!createdBrand) {
|
|
throw new ErrorHandler(500, "Failed to create brand");
|
|
}
|
|
|
|
return createdBrand;
|
|
} catch (error) {
|
|
throw new ErrorHandler(error.statusCode || 500, error.message);
|
|
}
|
|
}
|
|
|
|
// Soft delete brand by ID
|
|
static async deleteBrand(id, userId) {
|
|
try {
|
|
const brandExist = await getBrandByIdDb(id);
|
|
|
|
if (!brandExist) {
|
|
throw new ErrorHandler(404, "Brand not found");
|
|
}
|
|
|
|
const result = await deleteBrandDb(id, userId);
|
|
|
|
return result;
|
|
} catch (error) {
|
|
throw new ErrorHandler(error.statusCode, error.message);
|
|
}
|
|
}
|
|
|
|
|
|
// Update brand
|
|
static async updateBrand(id, data) {
|
|
try {
|
|
const existingBrand = await getBrandByIdDb(id);
|
|
if (!existingBrand) throw new ErrorHandler(404, "Brand not found");
|
|
|
|
if (data.brand_name && data.brand_name !== existingBrand.brand_name) {
|
|
const brandExists = await checkBrandNameExistsDb(data.brand_name, id);
|
|
if (brandExists) {
|
|
throw new ErrorHandler(400, "Brand name already exists");
|
|
}
|
|
}
|
|
|
|
const brandData = {
|
|
brand_name: data.brand_name || existingBrand.brand_name,
|
|
brand_type: data.brand_type !== undefined ? data.brand_type : existingBrand.brand_type,
|
|
brand_manufacture: data.brand_manufacture !== undefined ? data.brand_manufacture : existingBrand.brand_manufacture,
|
|
brand_model: data.brand_model !== undefined ? data.brand_model : existingBrand.brand_model,
|
|
is_active: data.is_active !== undefined ? data.is_active : existingBrand.is_active,
|
|
updated_by: data.updated_by,
|
|
};
|
|
|
|
const updatedBrand = await updateBrandDb(existingBrand.brand_name, brandData);
|
|
if (!updatedBrand) {
|
|
throw new ErrorHandler(500, "Failed to update brand");
|
|
}
|
|
|
|
return updatedBrand;
|
|
} catch (error) {
|
|
throw new ErrorHandler(error.statusCode || 500, error.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = BrandService;
|