-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Star-Academy/feature/add-transactioncsv-api
Feature/add transactioncsv api
- Loading branch information
Showing
18 changed files
with
1,441 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Application/DTOs/TransactionCsv/TransactionCsvModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/Application/Services/SharedService/CsvReaderService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/Application/Services/SharedService/PersianDateConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
|
||
|
||
} |
Oops, something went wrong.