private void SortCosts(ref List <CostDTO> costs, CostSort sortBy = CostSort.Date, bool isAscending = true) { Func <CostDTO, object> sort; switch (sortBy) { case CostSort.Date: sort = cost => cost.Date; break; case CostSort.Name: sort = cost => cost.Name; break; case CostSort.Price: sort = cost => cost.Price; break; case CostSort.Type: sort = cost => cost.CostType; break; default: return; } costs = (isAscending ? costs.OrderBy(sort) : costs.OrderByDescending(sort)).ToList(); }
public async Task <IActionResult> ShowCosts(int unitId, CostSort sortBy = CostSort.Date, bool isAscending = true, int pageNumber = 1, DateTime fromDate = default, DateTime toDate = default) { var unit = await _unitFacade.GetUnitByIdAsync <UnitDTO>(unitId); if (!(unit != null && UserInfoManager.CanUserAccessPage(unit.OwnerId))) { return(RedirectToAction("AccessError", "Home")); } var today = DateTime.Today; if (fromDate == default) { fromDate = new DateTime(today.Year, today.Month, 1); } if (toDate == default) { toDate = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month)); } ViewBag.UnitName = unit.Specification.Name; ViewBag.SortBy = sortBy; ViewBag.IsAscending = isAscending; var costs = await _costFacade.GetCostsByUnitIdAsync <CostDTO>(unitId, fromDate, toDate); SortCosts(ref costs, sortBy, isAscending); // TODO customizable size var pageSize = 10; var costWithUnitId = new CostsWithUnitIdDTO { CostsDTO = costs.ToPagedList(pageNumber, pageSize), UnitId = unitId, FromDate = fromDate, ToDate = toDate }; return(View(costWithUnitId)); }