Added pagination to transactions #53
@ -17,6 +17,8 @@ export default function AccountDashboard() {
|
||||
const search = useLocation().search;
|
||||
const id = new URLSearchParams(search).get('id')
|
||||
|
||||
const [page, setPage] = useState(1);
|
||||
|
||||
useEffect(() => {
|
||||
getData(EndPoints.ACCOUNTS + "/" + id).then(
|
||||
(result) => {
|
||||
@ -36,7 +38,7 @@ export default function AccountDashboard() {
|
||||
}
|
||||
);
|
||||
|
||||
getAllTransactionsData();
|
||||
getAllTransactionsData(page);
|
||||
getAllAccountsData();
|
||||
getAccountEnvelopeData();
|
||||
getAccountAutoclassRulesData(id);
|
||||
@ -80,8 +82,24 @@ export default function AccountDashboard() {
|
||||
navigate(Routes.TRANSACTIONS_DELETE + query);
|
||||
}
|
||||
|
||||
function getAllTransactionsData() {
|
||||
getData(EndPoints.TRANSACTIONS + "?accountId=" + id).then(
|
||||
function previousPage()
|
||||
{
|
||||
setLoading(true);
|
||||
setPage(page > 1 ? page - 1 : 1)
|
||||
getAllTransactionsData(page)
|
||||
}
|
||||
|
||||
function nextPage()
|
||||
{
|
||||
setLoading(true);
|
||||
setPage(page + 1);
|
||||
getAllTransactionsData(page)
|
||||
}
|
||||
|
||||
function getAllTransactionsData(dataPage) {
|
||||
let pageSize = 50;
|
||||
let query = "?PageNumber=" + dataPage + "&PageSize=" + pageSize + "&accountId=" + id
|
||||
getData(EndPoints.TRANSACTIONS + query).then(
|
||||
(result) => {
|
||||
if (result) {
|
||||
console.log()
|
||||
@ -406,6 +424,26 @@ export default function AccountDashboard() {
|
||||
</h3>
|
||||
</div>
|
||||
</div>
|
||||
<div className="row">
|
||||
<div className="col-md-4">
|
||||
</div>
|
||||
<div className="col-md-1">
|
||||
<button onClick={() => previousPage()} className="btn btn-primary">Previous Page</button>
|
||||
</div>
|
||||
<div className="col-md-2">
|
||||
|
||||
<div className="text-center">
|
||||
<h3>
|
||||
{page}
|
||||
</h3>
|
||||
</div>
|
||||
</div>
|
||||
<div className="col-md-1">
|
||||
<button onClick={() => nextPage()} className="btn btn-primary">Next Page</button>
|
||||
</div>
|
||||
<div className="col-md-4">
|
||||
</div>
|
||||
</div>
|
||||
{transactionsContent}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -23,17 +23,32 @@ export default function Transactions() {
|
||||
const [accounts, setAccounts] = useState([]);
|
||||
const [envelopes, setEnvelopes] = useState([]);
|
||||
|
||||
const [page, setPage] = useState(1);
|
||||
|
||||
function onTransactionCreate() {
|
||||
navigate(Routes.TRANSACTIONS_CREATE);
|
||||
}
|
||||
|
||||
function previousPage()
|
||||
{
|
||||
setPage(page > 1 ? page - 1 : 1)
|
||||
getAllTransactionsData()
|
||||
}
|
||||
|
||||
function nextPage()
|
||||
{
|
||||
setPage(page + 1);
|
||||
getAllTransactionsData()
|
||||
}
|
||||
|
||||
function getAllTransactionsData() {
|
||||
//let url = EndPoints.TRANSACTIONS + '/';
|
||||
/*if (accountId) {
|
||||
url += '?accountId=' + accountId;
|
||||
}*/
|
||||
|
||||
getData(EndPoints.TRANSACTIONS).then(
|
||||
let pageSize = 50;
|
||||
let query = "?PageNumber=" + page + "&PageSize=" + pageSize
|
||||
getData(EndPoints.TRANSACTIONS + query).then(
|
||||
(result) => {
|
||||
if (result) {
|
||||
setTransactions(result);
|
||||
@ -93,6 +108,14 @@ export default function Transactions() {
|
||||
<h3>List of Transactions</h3>
|
||||
<button onClick={() => onTransactionCreate()} className="btn btn-primary">Create new transaction</button>
|
||||
<hr />
|
||||
|
||||
<button onClick={() => previousPage()} className="btn btn-primary">Previous Page</button>
|
||||
|
||||
<p>{page}</p>
|
||||
|
||||
<button onClick={() => nextPage()} className="btn btn-primary">Next Page</button>
|
||||
<br />
|
||||
<br />
|
||||
{content}
|
||||
</div>
|
||||
);
|
||||
|
@ -11,6 +11,8 @@ using ActiveAllocator.API.Entities;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using ActiveAllocator.API.Models.Transactions.VendorSpecific;
|
||||
using ActiveAllocator.API.Helpers;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
@ -20,23 +22,28 @@ public class TransactionsController : ControllerBase
|
||||
private ITransactionService _transactionService;
|
||||
private IMapper _mapper;
|
||||
private readonly AppSettings _appSettings;
|
||||
private readonly ILogger<TransactionsController> _logger;
|
||||
|
||||
public TransactionsController(
|
||||
ITransactionService transactionService,
|
||||
IMapper mapper,
|
||||
IOptions<AppSettings> appSettings)
|
||||
IOptions<AppSettings> appSettings,
|
||||
ILogger<TransactionsController> logger)
|
||||
{
|
||||
_transactionService = transactionService;
|
||||
_mapper = mapper;
|
||||
_appSettings = appSettings.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetAll(int? accountId = null)
|
||||
public IActionResult GetAll([FromQuery]PaginationParameters paginationParameters, int? accountId = null)
|
||||
{
|
||||
List<TransactionDto> transactionDtos = new List<TransactionDto>();
|
||||
|
||||
foreach (Transaction tran in _transactionService.GetAll())
|
||||
PagedList<Transaction> transactions = _transactionService.GetTransactions(paginationParameters, accountId);
|
||||
|
||||
foreach (Transaction tran in transactions)
|
||||
{
|
||||
if (accountId.HasValue
|
||||
&& (tran.DebitAccount == null || tran.DebitAccount.Id != accountId)
|
||||
@ -47,8 +54,21 @@ public class TransactionsController : ControllerBase
|
||||
}
|
||||
|
||||
// Sort by Date
|
||||
transactionDtos.Sort((t1, t2) => t2.Date.CompareTo(t1.Date));
|
||||
//transactionDtos.Sort((t1, t2) => t2.Date.CompareTo(t1.Date));
|
||||
|
||||
var metadata = new
|
||||
{
|
||||
transactions.TotalCount,
|
||||
transactions.PageSize,
|
||||
transactions.CurrentPage,
|
||||
transactions.TotalPages,
|
||||
transactions.HasNext,
|
||||
transactions.HasPrevious
|
||||
};
|
||||
|
||||
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata));
|
||||
|
||||
_logger.LogInformation($"Returned {transactions.PageSize} transactions from database.");
|
||||
return Ok(transactionDtos);
|
||||
}
|
||||
|
||||
|
27
ActiveAllocator.API/Helpers/PagedList.cs
Normal file
27
ActiveAllocator.API/Helpers/PagedList.cs
Normal file
@ -0,0 +1,27 @@
|
||||
namespace ActiveAllocator.API.Helpers;
|
||||
|
||||
public class PagedList<T> : List<T>
|
||||
{
|
||||
public int CurrentPage { get; private set; }
|
||||
public int TotalPages { get; private set; }
|
||||
public int PageSize { get; private set; }
|
||||
public int TotalCount { get; private set; }
|
||||
public bool HasPrevious => CurrentPage > 1;
|
||||
public bool HasNext => CurrentPage < TotalPages;
|
||||
|
||||
public PagedList(List<T> items, int count, int pageNumber, int pageSize)
|
||||
{
|
||||
TotalCount = count;
|
||||
PageSize = pageSize;
|
||||
CurrentPage = pageNumber;
|
||||
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
|
||||
AddRange(items);
|
||||
}
|
||||
|
||||
public static PagedList<T> ToPagedList(IQueryable<T> source, int pageNumber, int pageSize)
|
||||
{
|
||||
var count = source.Count();
|
||||
var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
|
||||
return new PagedList<T>(items, count, pageNumber, pageSize);
|
||||
}
|
||||
}
|
7
ActiveAllocator.API/Helpers/PaginationParameters.cs
Normal file
7
ActiveAllocator.API/Helpers/PaginationParameters.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace ActiveAllocator.API.Helpers;
|
||||
|
||||
public class PaginationParameters
|
||||
{
|
||||
public int PageNumber { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
}
|
@ -12,10 +12,11 @@ using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Internal;
|
||||
using System.Collections.Immutable;
|
||||
using ActiveAllocator.API.Helpers;
|
||||
|
||||
public interface ITransactionService
|
||||
{
|
||||
IEnumerable<Transaction> GetAll();
|
||||
PagedList<Transaction> GetTransactions(PaginationParameters paginationParameters, int? accountId);
|
||||
Transaction GetById(int id);
|
||||
IEnumerable<Transaction> BulkCreate(List<TransactionCreate> model);
|
||||
Transaction Create(TransactionCreate model, bool errorOnFail = true);
|
||||
@ -42,15 +43,17 @@ public class TransactionService : ITransactionService
|
||||
_autoclassService = autoclassService;
|
||||
}
|
||||
|
||||
public IEnumerable<Transaction> GetAll()
|
||||
public PagedList<Transaction> GetTransactions(PaginationParameters paginationParameters, int? accountId)
|
||||
{
|
||||
return _context.Transactions
|
||||
.Include(t => t.CurrencyType)
|
||||
.Include(t => t.DebitAccount)
|
||||
.Include(t => t.CreditAccount)
|
||||
.Include(t => t.DebitEnvelope)
|
||||
.Include(t => t.CreditEnvelope)
|
||||
.ToList();
|
||||
IQueryable<Transaction> queryable = FindAllTransactions();
|
||||
|
||||
if (accountId.HasValue)
|
||||
queryable = queryable.Where(t => (t.DebitAccount != null && t.DebitAccount.Id == accountId.Value) || (t.CreditAccount != null && t.CreditAccount.Id == accountId.Value));
|
||||
|
||||
return PagedList<Transaction>.ToPagedList(
|
||||
queryable.OrderByDescending(t => t.Date),
|
||||
paginationParameters.PageNumber,
|
||||
paginationParameters.PageSize);
|
||||
}
|
||||
|
||||
public Transaction GetById(int id)
|
||||
@ -353,4 +356,14 @@ public class TransactionService : ITransactionService
|
||||
|
||||
return transaction;
|
||||
}
|
||||
|
||||
private IQueryable<Transaction> FindAllTransactions()
|
||||
{
|
||||
return _context.Transactions
|
||||
.Include(t => t.CurrencyType)
|
||||
.Include(t => t.DebitAccount)
|
||||
.Include(t => t.CreditAccount)
|
||||
.Include(t => t.DebitEnvelope)
|
||||
.Include(t => t.CreditEnvelope);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user