Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: paginate response of get-all-transactions api #27

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Application/DTOs/PaginatedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Application.DTOs;

public class PaginatedList<T>
{
public List<T> Items { get; }
public int TotalCount { get; }
public int PageNumber { get; }
public int PageSize { get; }
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);

public PaginatedList(List<T> items, int count, int pageNumber, int pageSize)
{
Items = items;
TotalCount = count;
PageNumber = pageNumber;
PageSize = pageSize;
}

public bool HasPreviousPage => PageNumber > 1;
public bool HasNextPage => PageNumber < TotalPages;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ namespace Application.Interfaces.Repositories;
public interface ITransactionRepository
{
Task CreateBulkAsync(List<Transaction> transactions);
Task<List<Transaction>> GetAllTransactions();
Task<List<Transaction>> GetAllTransactions(int skip, int take);
Task<List<Transaction>> GetBySourceAccountId(long accountId);
Task<List<Transaction>> GetByDestinationAccountId(long accountId);
Task<List<long>> GetAllIdsAsync();
Task<int> CountAllTransactionsAsync();
}
2 changes: 1 addition & 1 deletion src/Application/Interfaces/Services/ITransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Application.Interfaces.Services;
public interface ITransactionService
{
Task<Result> AddTransactionsFromCsvAsync(string filePath);
Task<Result<List<Transaction>>> GetAllTransactionsAsync();
Task<Result<PaginatedList<Transaction>>> GetAllTransactionsAsync(int pageNumber, int pageSize);
Task<Result<List<GetTransactionsByAccountIdResponse>>> GetTransactionsByAccountIdAsync(long accountId);
}
13 changes: 9 additions & 4 deletions src/Application/Services/DomainService/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,21 @@ public async Task<Result> AddTransactionsFromCsvAsync(string filePath)
}
}

public async Task<Result<List<Transaction>>> GetAllTransactionsAsync()
public async Task<Result<PaginatedList<Transaction>>> GetAllTransactionsAsync(int pageNumber, int pageSize)
{
try
{
var transactions = await _transactionRepository.GetAllTransactions();
return Result<List<Transaction>>.Ok(transactions);
var skip = (pageNumber - 1) * pageSize;
var transactions = await _transactionRepository.GetAllTransactions(skip, pageSize);
var totalTransactions = await _transactionRepository.CountAllTransactionsAsync();

var paginatedTransactions = new PaginatedList<Transaction>(transactions, totalTransactions, pageNumber, pageSize);

return Result<PaginatedList<Transaction>>.Ok(paginatedTransactions);
}
catch (Exception ex)
{
return Result<List<Transaction>>.Fail($"An error occurred: {ex.Message}");
return Result<PaginatedList<Transaction>>.Fail($"An error occurred: {ex.Message}");
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/Application/Services/SharedService/PaginatorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Application.DTOs;

namespace Application.Services.SharedService;

public static class PaginatorService
{
public static PaginatedList<T> Paginate<T>(this IEnumerable<T> source, int pageNumber, int pageSize)
{
var sourceList = source as IList<T> ?? source.ToList();

var count = sourceList.Count;
var items = sourceList.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();

return new PaginatedList<T>(items, count, pageNumber, pageSize);
}
}
13 changes: 11 additions & 2 deletions src/Infrastructure/Repositories/TransactionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ public async Task CreateBulkAsync(List<Transaction> transactions)
await _dbContext.SaveChangesAsync();
}

public async Task<List<Transaction>> GetAllTransactions()
public async Task<List<Transaction>> GetAllTransactions(int skip, int take)
{
return await _dbContext.Transactions.ToListAsync();
return await _dbContext.Transactions
.OrderBy(t => t.TransactionId)
.Skip(skip)
.Take(take)
.ToListAsync();
}

public Task<List<Transaction>> GetBySourceAccountId(long accountId)
Expand All @@ -44,4 +48,9 @@ public async Task<List<long>> GetAllIdsAsync()
.Select(a => a.TransactionId)
.ToListAsync();
}

public async Task<int> CountAllTransactionsAsync()
{
return await _dbContext.Transactions.CountAsync();
}
}
4 changes: 2 additions & 2 deletions src/Web/Controllers/TransactionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public async Task<IActionResult> UploadTransactions([FromForm] IFormFile file)
[ProducesResponseType(400)]
[ProducesResponseType(401)]
[ProducesResponseType(403)]
public async Task<IActionResult> GetAllTransactions()
public async Task<IActionResult> GetAllTransactions(int pageNumber = 1, int pageSize = 30)
{
var allTransactions = await _transactionService.GetAllTransactionsAsync();
var allTransactions = await _transactionService.GetAllTransactionsAsync(pageNumber, pageSize);
if (!allTransactions.Succeed)
{
var errorResponse = Errors.New(nameof(GetAllTransactions), allTransactions.Message);
Expand Down
10 changes: 7 additions & 3 deletions src/Web/Mappers/TransactionMapper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Application.DTOs;
using Domain.Entities;
using Web.DTOs.Transaction;

Expand All @@ -17,10 +18,10 @@ public static TransactionDto ToTransactionDto(this Transaction transaction)
Type = transaction.Type
};
}

public static List<TransactionDto> ToGotAllTransactionsDto(this List<Transaction> transactions)
public static PaginatedList<TransactionDto> ToGotAllTransactionsDto(this PaginatedList<Transaction> transactions)
{
return transactions.Select(transaction => new TransactionDto
var dtoItems = transactions.Items.Select(transaction => new TransactionDto
{
TransactionId = transaction.TransactionId,
SourceAccountId = transaction.SourceAccountId,
Expand All @@ -29,5 +30,8 @@ public static List<TransactionDto> ToGotAllTransactionsDto(this List<Transaction
Date = transaction.Date,
Type = transaction.Type
}).ToList();

return new PaginatedList<TransactionDto>(dtoItems, transactions.TotalCount, transactions.PageNumber, transactions.PageSize);
}

}
Loading