From 56ca8d4ec287d4b0449072d91145d515f0f5581d Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 29 Dec 2020 16:39:49 +0100 Subject: [PATCH] Add geting meals by date --- src/controllers/meal.controller.js | 6 ++++++ src/routes/v1/meal.route.js | 4 ++++ src/services/meal.service.js | 14 +++++++++++++- src/validations/meal.validation.js | 5 +---- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/controllers/meal.controller.js b/src/controllers/meal.controller.js index cca61cc..37525f1 100644 --- a/src/controllers/meal.controller.js +++ b/src/controllers/meal.controller.js @@ -11,6 +11,11 @@ const createMeal = catchAsync(async (req, res) => { }); const getMeals = catchAsync(async (req, res) => { + const result = await mealService.getMealsByDate(req.user._id, req.query.date); + res.send(result); +}); + +const getAllMeals = catchAsync(async (req, res) => { const filter = pick(req.query, ['label']); const options = pick(req.query, ['sortBy', 'limit', 'page']); const result = await mealService.queryMeals(filter, options); @@ -43,6 +48,7 @@ const deleteMeal = catchAsync(async (req, res) => { module.exports = { createMeal, getMeals, + getAllMeals, getMeal, updateMeal, updateMealProducts, diff --git a/src/routes/v1/meal.route.js b/src/routes/v1/meal.route.js index e460bbd..dfbba47 100644 --- a/src/routes/v1/meal.route.js +++ b/src/routes/v1/meal.route.js @@ -11,6 +11,10 @@ router .post(auth(), validate(mealValidation.createMeal), mealController.createMeal) .get(auth(), validate(mealValidation.getMeals), mealController.getMeals); +router + .route('/all') + .get(mealController.getAllMeals) + router .route('/:mealId') .get(auth(), validate(mealValidation.getMeal), mealController.getMeal) diff --git a/src/services/meal.service.js b/src/services/meal.service.js index 83fc177..e031187 100644 --- a/src/services/meal.service.js +++ b/src/services/meal.service.js @@ -12,12 +12,23 @@ const queryMeals = async (filter, options) => { return meals; }; +const getMealsByDate = async (userId, date) => { + const meals = Meal.find({ + user: userId, + date: { + $gte: new Date(new Date(date).setHours(0, 0, 0)), + $lt: new Date(new Date(date).setHours(23, 59, 59)), + }, + }); + return meals; +}; + const getMealById = async (id) => { return Meal.findById(id); }; const getMealByUserId = async (userId) => { - return Meal.findOne({ userId }); + return Meal.findOne({ user: userId }); }; const updateMealById = async (mealId, updateBody) => { @@ -53,6 +64,7 @@ module.exports = { createMeal, queryMeals, getMealById, + getMealsByDate, getMealByUserId, updateMealById, updateMealProductsById, diff --git a/src/validations/meal.validation.js b/src/validations/meal.validation.js index 0819926..8102894 100644 --- a/src/validations/meal.validation.js +++ b/src/validations/meal.validation.js @@ -17,10 +17,7 @@ const createMeal = { const getMeals = { query: Joi.object().keys({ - label: Joi.string().valid('breakfast', 'lunch', 'dinner', 'supper', 'snack I', 'snack II', 'snack III'), - sortBy: Joi.string(), - limit: Joi.number().integer(), - page: Joi.number().integer(), + date: Joi.date().required(), }), };