Initial commit
This commit is contained in:
44
server/controllers/brandController.js
Normal file
44
server/controllers/brandController.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const Brand = require('../models/Brand');
|
||||
const { successResponse, errorResponse } = require('../utils/apiResponse');
|
||||
|
||||
const getBrands = async (req, res, next) => {
|
||||
try {
|
||||
const brands = await Brand.find({ isActive: true }).sort({ name: 1 });
|
||||
return successResponse(res, 'Brands retrieved', brands);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
const createBrand = async (req, res, next) => {
|
||||
try {
|
||||
const brand = new Brand(req.body);
|
||||
const saved = await brand.save();
|
||||
return successResponse(res, 'Brand created', saved, 201);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
const updateBrand = async (req, res, next) => {
|
||||
try {
|
||||
const brand = await Brand.findByIdAndUpdate(req.params.id, req.body, { new: true });
|
||||
if (!brand) return errorResponse(res, 'Brand not found', [], 404);
|
||||
return successResponse(res, 'Brand updated', brand);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
const deleteBrand = async (req, res, next) => {
|
||||
try {
|
||||
const brand = await Brand.findByIdAndDelete(req.params.id);
|
||||
if (!brand) return errorResponse(res, 'Brand not found', [], 404);
|
||||
return successResponse(res, 'Brand deleted');
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { getBrands, createBrand, updateBrand, deleteBrand };
|
||||
|
||||
Reference in New Issue
Block a user