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

Feature/add transactioncsv api #7

Merged
merged 4 commits into from
Aug 18, 2024
Merged
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
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; }

Check warning on line 9 in src/Domain/Entities/Transaction.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable property 'SourceAccount' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public long DestinationAccountId { get; set; }
public Account DestinationAccount { get; set; }

Check warning on line 11 in src/Domain/Entities/Transaction.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable property 'DestinationAccount' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
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
Loading