Add geting meals by date

This commit is contained in:
= 2020-12-29 16:39:49 +01:00
parent 46c531099f
commit 56ca8d4ec2
4 changed files with 24 additions and 5 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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,

View File

@ -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(),
}),
};