From a51a88df8e5e532e856af1cef3302dd8facd55cb Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Sun, 18 Aug 2024 20:27:06 +0330 Subject: [PATCH] feat: Add get-all-transactions api --- .../GetAllTransactionsResponse.cs | 17 +++++++++++++ .../Interfaces/ITransactionRepository.cs | 1 + .../Services/ITransactionService.cs | 2 ++ src/Application/Mappers/TransactionMapper.cs | 24 +++++++++++++++++++ .../Services/TransactionService.cs | 23 +++++++++++++++++- .../Services/TransactionRepository.cs | 6 +++++ src/Web/Controllers/TransactionController.cs | 18 +++++++++++++- .../DTOs/Transaction/GotAllTransactionsDto.cs | 13 ++++++++++ src/Web/DTOs/Transaction/TransactionDto.cs | 11 +++++++++ src/Web/Mappers/TransactionMapper.cs | 20 ++++++++++++++++ 10 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs create mode 100644 src/Application/Mappers/TransactionMapper.cs create mode 100644 src/Web/DTOs/Transaction/GotAllTransactionsDto.cs create mode 100644 src/Web/DTOs/Transaction/TransactionDto.cs create mode 100644 src/Web/Mappers/TransactionMapper.cs diff --git a/src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs b/src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs new file mode 100644 index 0000000..80f5631 --- /dev/null +++ b/src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs @@ -0,0 +1,17 @@ +namespace Web.DTOs.Transaction; + +public class GetAllTransactionsResponse +{ + public List Transactions { get; set; } = new(); + + public class TransactionDto + { + public long TransactionId { get; set; } + public long SourceAccountId { get; set; } + public long DestinationAccountId { get; set; } + public decimal Amount { get; set; } + public DateTime Date { get; set; } + public string Type { get; set; } = string.Empty; + } + +} \ No newline at end of file diff --git a/src/Application/Interfaces/ITransactionRepository.cs b/src/Application/Interfaces/ITransactionRepository.cs index 060ce17..1a96ef2 100644 --- a/src/Application/Interfaces/ITransactionRepository.cs +++ b/src/Application/Interfaces/ITransactionRepository.cs @@ -5,4 +5,5 @@ namespace Application.Interfaces; public interface ITransactionRepository { Task CreateBulkAsync(List transactions); + Task> GetAllTransactions(); } \ No newline at end of file diff --git a/src/Application/Interfaces/Services/ITransactionService.cs b/src/Application/Interfaces/Services/ITransactionService.cs index ea94d79..cb5ffaa 100644 --- a/src/Application/Interfaces/Services/ITransactionService.cs +++ b/src/Application/Interfaces/Services/ITransactionService.cs @@ -1,8 +1,10 @@ using Application.DTOs; +using Web.DTOs.Transaction; namespace Application.Interfaces.Services; public interface ITransactionService { Task AddTransactionsFromCsvAsync(string filePath); + Task> GetAllTransactions(); } \ No newline at end of file diff --git a/src/Application/Mappers/TransactionMapper.cs b/src/Application/Mappers/TransactionMapper.cs new file mode 100644 index 0000000..f9d0c22 --- /dev/null +++ b/src/Application/Mappers/TransactionMapper.cs @@ -0,0 +1,24 @@ +using Web.DTOs.Transaction; +using Domain.Entities; + +namespace Application.Mappers +{ + public static class TransactionMapper + { + public static GetAllTransactionsResponse ToGetAllTransactionsResponse(this List transactions) + { + return new GetAllTransactionsResponse + { + Transactions = transactions.Select(transaction => new GetAllTransactionsResponse.TransactionDto + { + TransactionId = transaction.TransactionId, + SourceAccountId = transaction.SourceAccountId, + DestinationAccountId = transaction.DestinationAccountId, + Amount = transaction.Amount, + Date = transaction.Date, + Type = transaction.Type + }).ToList() + }; + } + } +} \ No newline at end of file diff --git a/src/Application/Services/TransactionService.cs b/src/Application/Services/TransactionService.cs index 1d4868e..a15f87c 100644 --- a/src/Application/Services/TransactionService.cs +++ b/src/Application/Services/TransactionService.cs @@ -2,14 +2,16 @@ using Application.DTOs.TransactionCsv; using Application.Interfaces; using Application.Interfaces.Services; +using Application.Mappers; using Application.Services.SharedService; using Domain.Entities; +using Web.DTOs.Transaction; namespace Application.Services; public class TransactionService : ITransactionService { - public readonly ITransactionRepository _transactionRepository; + private readonly ITransactionRepository _transactionRepository; public TransactionService(ITransactionRepository transactionRepository) { @@ -31,4 +33,23 @@ public async Task AddTransactionsFromCsvAsync(string filePath) await _transactionRepository.CreateBulkAsync(transactions); } + + public async Task> GetAllTransactions() + { + try + { + var transactions = await _transactionRepository.GetAllTransactions(); + + if (transactions.Count == 0) + { + return Result.Fail("No transactions found"); + } + var response = transactions.ToGetAllTransactionsResponse(); + return Result.Ok(response); + } + catch (Exception ex) + { + return Result.Fail($"An error occurred: {ex.Message}"); + } + } } \ No newline at end of file diff --git a/src/Infrastructure/Services/TransactionRepository.cs b/src/Infrastructure/Services/TransactionRepository.cs index e695392..a0eedf4 100644 --- a/src/Infrastructure/Services/TransactionRepository.cs +++ b/src/Infrastructure/Services/TransactionRepository.cs @@ -1,6 +1,7 @@ using Application.Interfaces; using Domain.Entities; using Infrastructure.Data; +using Microsoft.EntityFrameworkCore; namespace Infrastructure.Services; @@ -17,4 +18,9 @@ public async Task CreateBulkAsync(List transactions) await _dbContext.Transactions.AddRangeAsync(transactions); await _dbContext.SaveChangesAsync(); } + + public async Task> GetAllTransactions() + { + return await _dbContext.Transactions.ToListAsync(); + } } \ No newline at end of file diff --git a/src/Web/Controllers/TransactionController.cs b/src/Web/Controllers/TransactionController.cs index b69e90b..4c88357 100644 --- a/src/Web/Controllers/TransactionController.cs +++ b/src/Web/Controllers/TransactionController.cs @@ -1,6 +1,8 @@ using Application.Interfaces.Services; -using Infrastructure.Data; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Web.Helper; +using Web.Mappers; namespace Web.Controllers; @@ -32,4 +34,18 @@ public async Task ImportTransactions([FromForm] IFormFile file) return Ok("Transactions imported successfully."); } + + [HttpGet] + [Authorize] + public async Task GetAllTransactions() + { + var allTransactions = await _transactionService.GetAllTransactions(); + if (!allTransactions.Succeed) + { + return BadRequest(Errors.New(nameof(GetAllTransactions), allTransactions.Message)); + } + + var response = allTransactions.Value!; + return Ok(response.ToGotAllTransactionsDto()); + } } \ No newline at end of file diff --git a/src/Web/DTOs/Transaction/GotAllTransactionsDto.cs b/src/Web/DTOs/Transaction/GotAllTransactionsDto.cs new file mode 100644 index 0000000..35460e5 --- /dev/null +++ b/src/Web/DTOs/Transaction/GotAllTransactionsDto.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace Web.DTOs.Transaction; + +public class GotAllTransactionsDto +{ + public long TransactionId { get; set; } + public long SourceAccountId { get; set; } + public long DestinationAccountId { get; set; } + public decimal Amount { get; set; } + public DateTime Date { get; set; } + public string Type { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/src/Web/DTOs/Transaction/TransactionDto.cs b/src/Web/DTOs/Transaction/TransactionDto.cs new file mode 100644 index 0000000..6118c22 --- /dev/null +++ b/src/Web/DTOs/Transaction/TransactionDto.cs @@ -0,0 +1,11 @@ +namespace Web.DTOs.Transaction; + +// public class TransactionDto +// { +// public long TransactionId { get; set; } +// public long SourceAccountId { get; set; } +// public long DestinationAccountId { get; set; } +// public decimal Amount { get; set; } +// public DateTime Date { get; set; } +// public string Type { get; set; } = string.Empty; +// } diff --git a/src/Web/Mappers/TransactionMapper.cs b/src/Web/Mappers/TransactionMapper.cs new file mode 100644 index 0000000..78cf2fa --- /dev/null +++ b/src/Web/Mappers/TransactionMapper.cs @@ -0,0 +1,20 @@ +using Web.DTOs.Transaction; + +namespace Web.Mappers +{ + public static class TransactionMapper + { + public static List ToGotAllTransactionsDto(this GetAllTransactionsResponse response) + { + return response.Transactions.Select(transaction => new GetAllTransactionsResponse.TransactionDto + { + TransactionId = transaction.TransactionId, + SourceAccountId = transaction.SourceAccountId, + DestinationAccountId = transaction.DestinationAccountId, + Amount = transaction.Amount, + Date = transaction.Date, + Type = transaction.Type + }).ToList(); + } + } +} \ No newline at end of file