70 lines
1.5 KiB
JavaScript
70 lines
1.5 KiB
JavaScript
import {
|
|
createProduct,
|
|
updateProduct,
|
|
getAllProducts,
|
|
getProductByQuery,
|
|
getProductById,
|
|
deleteProduct,
|
|
} from "../models/Product/operations";
|
|
import dayjs from "dayjs";
|
|
|
|
export const createProductController = async (req, res, next) => {
|
|
try {
|
|
const { user } = res.locals;
|
|
|
|
const product = await createProduct({
|
|
...req.body,
|
|
createdBy: user.id,
|
|
createdOn: dayjs().toISOString(),
|
|
});
|
|
|
|
res.status(200).json({
|
|
result: product,
|
|
});
|
|
} catch (error) {
|
|
console.log("createProductController Error", error);
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
export const updateProductController = async (req, res, next) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const product = await updateProduct(id, {
|
|
...req.body,
|
|
});
|
|
|
|
res.status(200).json({
|
|
result: product,
|
|
});
|
|
} catch (error) {
|
|
console.log("updateProductController Error", error);
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
export const getAllProductsController = async (req, res, next) => {
|
|
try {
|
|
const products = await getAllProducts();
|
|
|
|
res.status(200).json({
|
|
result: products,
|
|
});
|
|
} catch (error) {
|
|
console.log("getAllProductsController Error", error);
|
|
next(error);
|
|
}
|
|
};
|
|
|
|
export const deleteProductController = async (req, res, next) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const product = await deleteProduct(id);
|
|
res.status(200).json({
|
|
result: product,
|
|
});
|
|
} catch (error) {
|
|
console.log("deleteProductController Error", error);
|
|
next(error);
|
|
}
|
|
}; |