Skip to content

Commit

Permalink
Merge pull request #7 from Star-Academy/feature/add-transactioncsv-api
Browse files Browse the repository at this point in the history
Feature/add transactioncsv api
  • Loading branch information
amiralirahimii authored Aug 18, 2024
2 parents ee49c22 + 3bf78a5 commit dabf113
Show file tree
Hide file tree
Showing 18 changed files with 1,441 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="33.0.1" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions src/Application/DTOs/TransactionCsv/TransactionCsvModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Application.Services.SharedService;
using CsvHelper.Configuration.Attributes;

namespace Application.DTOs.TransactionCsv;

public class TransactionCsvModel
{
public long TransactionID { get; set; }
public long SourceAcount { get; set; }
public long DestiantionAccount { get; set; }
public decimal Amount { get; set; }
[TypeConverter(typeof(PersianDateConverter))]
public DateTime Date { get; set; }
public string Type { get; set; } = string.Empty;
}
8 changes: 8 additions & 0 deletions src/Application/Interfaces/ITransactionRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Domain.Entities;

namespace Application.Interfaces;

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

namespace Application.Interfaces.Services;

public interface ITransactionService
{
Task AddTransactionsFromCsvAsync(string filePath);
}
6 changes: 6 additions & 0 deletions src/Application/Interfaces/SharedService/ICsvService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Application.Interfaces.SharedService;

public interface ICsvService
{

}
22 changes: 22 additions & 0 deletions src/Application/Services/SharedService/CsvReaderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Globalization;
using Application.DTOs.TransactionCsv;
using CsvHelper;
using CsvHelper.Configuration;

namespace Application.Services.SharedService;

public static class CsvReaderService
{
public static List<T> ReadFromCsv<T>(string filePath)
{
using var reader = new StreamReader(filePath);
using var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)
{
HeaderValidated = null,
MissingFieldFound = null
});

var records = csv.GetRecords<T>().ToList();
return records;
}
}
34 changes: 34 additions & 0 deletions src/Application/Services/SharedService/PersianDateConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;

namespace Application.Services.SharedService;

public class PersianDateConverter : DateTimeConverter
{
public override object ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
{
if (string.IsNullOrWhiteSpace(text))
{
return DateTime.MinValue;
}

try
{
var persianCalendar = new PersianCalendar();
var parts = text.Split('/');

var year = int.Parse(parts[0]);
var month = int.Parse(parts[1]);
var day = int.Parse(parts[2]);

var gregorianDate = persianCalendar.ToDateTime(year, month, day, 0, 0, 0, 0);
return DateTime.SpecifyKind(gregorianDate, DateTimeKind.Utc);
}
catch (Exception ex)
{
throw new TypeConverterException(this, memberMapData, text, row.Context, ex.Message);
}
}
}
34 changes: 34 additions & 0 deletions src/Application/Services/TransactionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Application.DTOs;
using Application.DTOs.TransactionCsv;
using Application.Interfaces;
using Application.Interfaces.Services;
using Application.Services.SharedService;
using Domain.Entities;

namespace Application.Services;

public class TransactionService : ITransactionService
{
public readonly ITransactionRepository _transactionRepository;

public TransactionService(ITransactionRepository transactionRepository)
{
_transactionRepository = transactionRepository;
}

public async Task AddTransactionsFromCsvAsync(string filePath)
{
var transactionCsvModels = CsvReaderService.ReadFromCsv<TransactionCsvModel>(filePath);

var transactions = transactionCsvModels.Select(csvModel => new Transaction
{
SourceAccountId = csvModel.SourceAcount,
DestinationAccountId = csvModel.DestiantionAccount,
Amount = csvModel.Amount,
Date = csvModel.Date,
Type = csvModel.Type
}).ToList();

await _transactionRepository.CreateBulkAsync(transactions);
}
}
4 changes: 2 additions & 2 deletions src/Domain/Entities/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Domain.Entities;
[Table("Accounts")]
public class Account
{
public int AccountId { get; set; }
public int CardId { get; set; }
public long AccountId { get; set; }
public long CardId { get; set; }
public string Iban { get; set; } = String.Empty;
public string AccountType { get; set; } = String.Empty;
public string BranchTelephone { get; set; } = String.Empty;
Expand Down
14 changes: 8 additions & 6 deletions src/Domain/Entities/Transaction.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace Domain.Entities;
[Table("Accounts")]
[Table("Transactions")]
public class Transaction
{
public int TransactionId { get; set; }
public int SourceAccountId { get; set; }
public int DestinationAccountId { get; set; }
public long TransactionId { get; set; }
public long SourceAccountId { get; set; }
public Account SourceAccount { get; set; }
public long DestinationAccountId { get; set; }
public Account DestinationAccount { get; set; }
public decimal Amount { get; set; }
public DateTime Date { get; set; }
public string Type { get; set; } = String.Empty;
public Account SourceAccount { get; set; }
public Account DestinationAccount { get; set; }


}
Loading

0 comments on commit dabf113

Please sign in to comment.