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