Skip to content

Commit

Permalink
Merge pull request #8 from Star-Academy/feature/get-all-transactions
Browse files Browse the repository at this point in the history
Feature/get all transactions
  • Loading branch information
mobinbr authored Aug 18, 2024
2 parents 2795892 + ead3ce6 commit f93c323
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Web.DTOs.Transaction;

public class GetAllTransactionsResponse
{
public List<TransactionDto> 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;
}

}
1 change: 1 addition & 0 deletions src/Application/Interfaces/ITransactionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace Application.Interfaces;
public interface ITransactionRepository
{
Task CreateBulkAsync(List<Transaction> transactions);
Task<List<Transaction>> GetAllTransactions();
}
2 changes: 2 additions & 0 deletions src/Application/Interfaces/Services/ITransactionService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Application.DTOs;
using Web.DTOs.Transaction;

namespace Application.Interfaces.Services;

public interface ITransactionService
{
Task AddTransactionsFromCsvAsync(string filePath);
Task<Result<GetAllTransactionsResponse>> GetAllTransactions();
}
24 changes: 24 additions & 0 deletions src/Application/Mappers/TransactionMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Web.DTOs.Transaction;
using Domain.Entities;

namespace Application.Mappers
{
public static class TransactionMapper
{
public static GetAllTransactionsResponse ToGetAllTransactionsResponse(this List<Transaction> 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()
};
}
}
}
23 changes: 22 additions & 1 deletion src/Application/Services/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -32,4 +34,23 @@ public async Task AddTransactionsFromCsvAsync(string filePath)

await _transactionRepository.CreateBulkAsync(transactions);
}

public async Task<Result<GetAllTransactionsResponse>> GetAllTransactions()
{
try
{
var transactions = await _transactionRepository.GetAllTransactions();

if (transactions.Count == 0)
{
return Result<GetAllTransactionsResponse>.Fail("No transactions found");
}
var response = transactions.ToGetAllTransactionsResponse();
return Result<GetAllTransactionsResponse>.Ok(response);
}
catch (Exception ex)
{
return Result<GetAllTransactionsResponse>.Fail($"An error occurred: {ex.Message}");
}
}
}
6 changes: 6 additions & 0 deletions src/Infrastructure/Services/TransactionRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Application.Interfaces;
using Domain.Entities;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.Services;

Expand All @@ -17,4 +18,9 @@ public async Task CreateBulkAsync(List<Transaction> transactions)
await _dbContext.Transactions.AddRangeAsync(transactions);
await _dbContext.SaveChangesAsync();
}

public async Task<List<Transaction>> GetAllTransactions()
{
return await _dbContext.Transactions.ToListAsync();
}
}
18 changes: 17 additions & 1 deletion src/Web/Controllers/TransactionController.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -32,4 +34,18 @@ public async Task<IActionResult> ImportTransactions([FromForm] IFormFile file)

return Ok("Transactions imported successfully.");
}

[HttpGet]
[Authorize]
public async Task<IActionResult> 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());
}
}
13 changes: 13 additions & 0 deletions src/Web/DTOs/Transaction/GotAllTransactionsDto.cs
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 14 additions & 1 deletion src/Web/Mappers/TransactionMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Domain.Entities;
using Domain.Entities;
using Web.DTOs.Transaction;

namespace Web.Mappers;
Expand All @@ -17,4 +17,17 @@ public static TransactionDto ToTransactionDto(this Transaction transaction)
Type = transaction.Type
};
}

public static List<GetAllTransactionsResponse.TransactionDto> 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();
}
}

0 comments on commit f93c323

Please sign in to comment.