Add removing products from meal list
This commit is contained in:
parent
f9cf60f530
commit
1e0c3a5bab
@ -41,9 +41,9 @@ const updateMealProducts = catchAsync(async (req, res) => {
|
||||
res.send(meal);
|
||||
});
|
||||
|
||||
const deleteMeal = catchAsync(async (req, res) => {
|
||||
await mealService.deleteMealById(req.params.mealId);
|
||||
res.status(httpStatus.NO_CONTENT).send();
|
||||
const removeProductByMeal = catchAsync(async (req, res) => {
|
||||
const meal = await mealService.removeProductByMealId(req.params.mealId, req.body.productId);
|
||||
res.send(meal);
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
@ -53,5 +53,5 @@ module.exports = {
|
||||
getMeal,
|
||||
updateMeal,
|
||||
updateMealProducts,
|
||||
deleteMeal,
|
||||
removeProductByMeal,
|
||||
};
|
||||
|
@ -19,7 +19,10 @@ router
|
||||
.route('/:mealId')
|
||||
.get(auth(), validate(mealValidation.getMeal), mealController.getMeal)
|
||||
.put(auth(), validate(mealValidation.updateMeal), mealController.updateMeal)
|
||||
.patch(auth(), validate(mealValidation.updateMealProducts), mealController.updateMealProducts)
|
||||
.delete(auth(), validate(mealValidation.deleteMeal), mealController.deleteMeal);
|
||||
.patch(auth(), validate(mealValidation.updateMealProducts), mealController.updateMealProducts);
|
||||
|
||||
router
|
||||
.route('/:mealId/products')
|
||||
.patch(auth(), validate(mealValidation.removeProductByMeal), mealController.removeProductByMeal);
|
||||
|
||||
module.exports = router;
|
||||
|
@ -54,13 +54,15 @@ const updateMealProductsById = async (mealId, updateBody) => {
|
||||
return mealWithProducts;
|
||||
};
|
||||
|
||||
const deleteMealById = async (mealId) => {
|
||||
const removeProductByMealId = async (mealId, productId) => {
|
||||
const meal = await getMealById(mealId);
|
||||
if (!meal) {
|
||||
throw new ApiError(httpStatus.NOT_FOUND, 'Meal not found');
|
||||
}
|
||||
await meal.remove();
|
||||
return meal;
|
||||
meal.products.pull({ _id: productId });
|
||||
await meal.save();
|
||||
const mealWithProducts = await meal.populate('products.product').execPopulate()
|
||||
return mealWithProducts;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
@ -71,5 +73,5 @@ module.exports = {
|
||||
getMealByUserId,
|
||||
updateMealById,
|
||||
updateMealProductsById,
|
||||
deleteMealById,
|
||||
removeProductByMealId,
|
||||
};
|
||||
|
@ -12,9 +12,9 @@ const createProfile = async (profileBody) => {
|
||||
const dailyCalories = dailyCaloricRequirement(profileBody);
|
||||
const weeksToGoal = calculateWeeksToGetGoalWeight(profileBody);
|
||||
|
||||
const dailyFats = (dailyCalories * 0.4) / 9;
|
||||
const dailyProteins = (dailyCalories * 0.3) / 4;
|
||||
const dailyCarbs = (dailyCalories * 0.4) / 4;
|
||||
const dailyFats = Math.floor((dailyCalories * 0.4) / 9);
|
||||
const dailyProteins = Math.floor((dailyCalories * 0.3) / 4);
|
||||
const dailyCarbs = Math.floor((dailyCalories * 0.4) / 4);
|
||||
|
||||
const profile = await Profile.create({
|
||||
...profileBody,
|
||||
@ -50,9 +50,9 @@ const updateProfileById = async (profileId, updateBody) => {
|
||||
const dailyCalories = dailyCaloricRequirement(updateBody);
|
||||
const weeksToGoal = calculateWeeksToGetGoalWeight(updateBody);
|
||||
|
||||
const dailyFats = (dailyCalories * 0.4) / 9;
|
||||
const dailyProteins = (dailyCalories * 0.3) / 4;
|
||||
const dailyCarbs = (dailyCalories * 0.4) / 4;
|
||||
const dailyFats = Math.floor((dailyCalories * 0.4) / 9);
|
||||
const dailyProteins = Math.floor((dailyCalories * 0.3) / 4);
|
||||
const dailyCarbs = Math.floor((dailyCalories * 0.4) / 4);
|
||||
|
||||
Object.assign(profile, {
|
||||
...updateBody,
|
||||
|
@ -52,10 +52,15 @@ const updateMealProducts = {
|
||||
.min(1),
|
||||
};
|
||||
|
||||
const deleteMeal = {
|
||||
const removeProductByMeal = {
|
||||
params: Joi.object().keys({
|
||||
mealId: Joi.string().custom(objectId).required(),
|
||||
}),
|
||||
body: Joi.object()
|
||||
.keys({
|
||||
productId: Joi.string().custom(objectId).required(),
|
||||
})
|
||||
.min(1),
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
@ -64,5 +69,5 @@ module.exports = {
|
||||
getMeal,
|
||||
updateMeal,
|
||||
updateMealProducts,
|
||||
deleteMeal,
|
||||
removeProductByMeal,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user