public MealsListViewModel GetMealsListByPeriod(DateTime startDateTime, DateTime endDateTime, string username) { var meals = this.context.Meals.Include(m => m.FoodyUser).Include(m => m.Location) .Where(m => m.FoodyUser.UserName == username && m.TimeOfConsumption >= startDateTime && m.TimeOfConsumption <= endDateTime) .Select(m => new MealForEditingListViewModel { MealId = m.Id, Note = m.Note, TimeOfConsumption = m.TimeOfConsumption, CaloriesFromProteins = m.CaloriesFromProteins, CaloriesFromCarbohydrates = m.CaloriesFromCarbohydrates, CaloriesFromFats = m.CaloriesFromFats, TotalCalories = m.TotalCalories, Location = m.Location.Name }).OrderBy(m => m.TimeOfConsumption).ToList(); var mealsList = new MealsListViewModel { Items = meals, StartDateTime = startDateTime, EndDateTime = endDateTime }; mealsList.PaginationModel.TotalPages = this.paginationService.GetTotalPages(mealsList.Items.Count); return(mealsList); }
public IActionResult GetMealsForEditing(DateTime startDateTime, DateTime endDateTime, int currentPage = 1, string searchText = null) { MealsListViewModel model = null; if (string.IsNullOrEmpty(searchText)) { searchText = "from" + startDateTime.ToString() + "to" + endDateTime.ToString(); model = this.diaryService.GetMealsListByPeriod(startDateTime, endDateTime, this.User.Identity.Name); } else { var datesStrings = searchText.Replace("from", string.Empty).Split("to").ToArray(); var startDateFromPagination = DateTime.Parse(datesStrings[0]); var endDateFromPagination = DateTime.Parse(datesStrings[1]); model = this.diaryService.GetMealsListByPeriod(startDateFromPagination, endDateFromPagination, this.User.Identity.Name); } model = this.paginationService.GetPageModel <MealsListViewModel, MealForEditingListViewModel>(model, currentPage, searchText, this.GetType(), "GetMealsForEditing", null); return(View(model)); }