public async Task <ActionResult <AccountLedgerListDTO> > GetAccountLedgers([FromQuery] int page = 1, [FromQuery] int itemsPerPage = 20) { try { if (page < 1 || itemsPerPage < 1) { return(BadRequest("Request contained one or more invalid paging values.")); } var accountLedgers = await _accountLedgerRepo.GetAccountLedgers() .Skip((page - 1) * itemsPerPage) .Take(itemsPerPage) .ToListAsync(); var accountLedgerCount = await _accountLedgerRepo.GetAccountLedgers().CountAsync(); var accountLedgerList = _mapper.Map <List <AccountLedger>, List <AccountLedgerDTO> >(accountLedgers); var dto = new AccountLedgerListDTO { AccountLedgers = accountLedgerList, TotalItems = accountLedgerCount, TotalPages = decimal.ToInt32(Math.Ceiling((decimal)accountLedgerCount / (decimal)itemsPerPage)), CurrentPage = page, ItemsPerPage = itemsPerPage }; return(dto); } catch (Exception e) { _logger.LogError("Exception occurred while attempting to retrieve Account Ledgers.\nError: " + e.Message); return(BadRequest()); } }
public async Task <ActionResult <AccountLedgerListDTO> > LookupAccountLedger( [FromQuery] int?fiscalYearId, [FromQuery] int?accountId, [FromQuery] int?ledgerNo) { try { if (fiscalYearId.HasValue && (fiscalYearId.Value < byte.MinValue || fiscalYearId.Value > byte.MaxValue)) { return(BadRequest($"Fiscal Year ID must be between {byte.MinValue} and {byte.MaxValue}.")); } var accountLedgerQuery = _accountLedgerRepo.GetAccountLedgers(); if (fiscalYearId.HasValue) { accountLedgerQuery = accountLedgerQuery.Where(l => l.FiscalYearId == fiscalYearId.Value); } if (accountId.HasValue) { accountLedgerQuery = accountLedgerQuery.Where(l => l.AccountId == accountId.Value); } if (ledgerNo.HasValue) { accountLedgerQuery = accountLedgerQuery.Where(l => l.LedgerNo == ledgerNo.Value); } var accountLedgers = await accountLedgerQuery .OrderBy(l => l.FiscalYearId) .ThenBy(l => l.AccountId) .ThenBy(l => l.LedgerNo) .ToListAsync(); var accountLedgerList = _mapper.Map <List <AccountLedger>, List <AccountLedgerDTO> >(accountLedgers); var accountLedgerListCount = accountLedgerList.Count; var dto = new AccountLedgerListDTO { AccountLedgers = accountLedgerList, TotalItems = accountLedgerListCount, TotalPages = 1, CurrentPage = 1, ItemsPerPage = accountLedgerListCount }; return(dto); } catch (Exception e) { _logger.LogError("Exception occurred while attempting to lookup an Account Ledger.\nError: " + e.Message); return(BadRequest()); } }