public IViewComponentResult Invoke(WeekViewPagerViewModel pagerModel) { WeekViewViewModel model = null; if (ModelState.IsValid) { try { model = new WeekViewViewModel(); model.Pager = pagerModel; UIUser usr = GetUserInfo(); var list = WeekExpensesContext.LoadUserWeekExpenses(usr, pagerModel.SelectedWeek.WeekStartTs, pagerModel.SelectedWeek.WeekEndTs).Select(x => AutoMapperFactory.WeekViewItemViewModel_UIWeekExpense.CreateMapper().Map <WeekViewItemViewModel>(new UIWeekExpense(x))).ToList(); model.WeekViewItems = list; if (list.Count() != 0) { model.WeekTotalCost = list.Select(x => x.TotalCost).Sum(); } } catch (Exception ex) { ViewData["WarningMessage"] = ex.Message; } return(View(model)); } else { model = new WeekViewViewModel(); model.WeekViewItems = new List <WeekViewItemViewModel>(); model.Pager = pagerModel; return(View(model)); } }
public IActionResult WeekView(WeekViewPagerViewModel model, int Year, int Month, int Week) { try { if (Request.Method.Equals("GET") && Year == 0) { model.Month = DateTime.Today.Month - 1; DateTime firstDayoftheWeek = DateTime.Today.AddDays(1 - (int)(DateTime.Today.DayOfWeek)); var weekDicItem = WeekGenerator.GetAllWeeksForYear(model.Year).FirstOrDefault(x => ((DateTime)x["WeekStart"]).Equals(firstDayoftheWeek)); if (weekDicItem != null) { model.SelectedWeek = new WeekViewModel() { WeekStartTs = (DateTime)weekDicItem["WeekStart"], WeekEndTs = (DateTime)weekDicItem["WeekEnd"], WeekNum = (int)weekDicItem["WeekNum"] }; } model.Week = model.SelectedWeek.WeekNum; ModelState.Clear(); } else if (Year != 0) { model.Year = Year; model.Month = Month; model.Week = Week; var weekDicItem = WeekGenerator.GetAllWeeksForYear(model.Year).FirstOrDefault(x => ((int)x["WeekNum"]).Equals(model.Week)); if (weekDicItem != null) { model.SelectedWeek = new WeekViewModel() { WeekStartTs = (DateTime)weekDicItem["WeekStart"], WeekEndTs = (DateTime)weekDicItem["WeekEnd"], WeekNum = model.Week }; } } if (ModelState.IsValid) { return(View(model)); } else { return(View(model)); } } catch (Exception ex) { model.Month = DateTime.Today.Month - 1; DateTime firstDayoftheWeek = DateTime.Today.AddDays(1 - (int)(DateTime.Today.DayOfWeek)); var weekDicItem = WeekGenerator.GetAllWeeksForYear(model.Year).FirstOrDefault(x => ((DateTime)x["WeekStart"]).Equals(firstDayoftheWeek)); if (weekDicItem != null) { model.SelectedWeek = new WeekViewModel() { WeekStartTs = (DateTime)weekDicItem["WeekStart"], WeekEndTs = (DateTime)weekDicItem["WeekEnd"], WeekNum = (int)weekDicItem["WeekNum"] }; } model.Week = model.SelectedWeek.WeekNum; ViewData["WarningMessage"] = ex.Message; return(View(model)); } }