-
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.
feat(ReadData): Read CSV Data format (#9)
Co-authored-by: AmirReza <[email protected]>
- Loading branch information
1 parent
1299b55
commit e6d408b
Showing
16 changed files
with
201 additions
and
274 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
46 changes: 46 additions & 0 deletions
46
AnalysisData/AnalysisData/Controllers/UploadDataController.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,46 @@ | ||
using AnalysisData.DataProcessService; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace AnalysisData.Controllers; | ||
|
||
[ApiController] | ||
[Authorize(Roles = "admin")] | ||
[Route("api/[controller]")] | ||
public class DataController : ControllerBase | ||
{ | ||
private readonly IDataProcessor _dataProcessor; | ||
|
||
public DataController(IDataProcessor dataProcessor) | ||
{ | ||
_dataProcessor = dataProcessor; | ||
} | ||
|
||
[HttpPost("upload/account")] | ||
public async Task<IActionResult> UploadAccountFile(IFormFile file) | ||
{ | ||
if (file == null || file.Length == 0) | ||
{ | ||
return BadRequest("File is empty or not provided."); | ||
} | ||
|
||
var stream = file.OpenReadStream(); | ||
await _dataProcessor.ProcessDataAsync(stream, "account"); | ||
|
||
return Ok("Account data processed successfully."); | ||
} | ||
|
||
[HttpPost("upload/transaction")] | ||
public async Task<IActionResult> UploadTransactionFile(IFormFile file) | ||
{ | ||
if (file == null || file.Length == 0) | ||
{ | ||
return BadRequest("File is empty or not provided."); | ||
} | ||
|
||
var stream = file.OpenReadStream(); | ||
await _dataProcessor.ProcessDataAsync(stream, "transaction"); | ||
|
||
return Ok("Transaction data processed successfully."); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace AnalysisData.DataManage.Model; | ||
|
||
public class Account | ||
{ | ||
public string AccountID { get; set; } | ||
public string CardID { get; set; } | ||
public string IBAN { get; set; } | ||
public string AccountType { get; set; } | ||
public string BranchTelephone { get; set; } | ||
public string BranchAdress { get; set; } | ||
public string BranchName { get; set; } | ||
public string OwnerName { get; set; } | ||
public string OwnerLastName { get; set; } | ||
public string OwnerID { get; set; } | ||
} |
21 changes: 21 additions & 0 deletions
21
AnalysisData/AnalysisData/DataManage/Model/Trancsaction.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,21 @@ | ||
using AnalysisData.DataProcessService; | ||
using CsvHelper.Configuration.Attributes; | ||
|
||
namespace AnalysisData.DataManage.Model; | ||
|
||
public class Transaction | ||
{ | ||
[Name("SourceAcount")] | ||
public string SourceAccount { get; set; } | ||
|
||
[Name("DestiantionAccount")] | ||
public string DestinationAccount { get; set; } | ||
|
||
public decimal Amount { get; set; } | ||
|
||
public string Date { get; set; } | ||
|
||
public string TransactionID { get; set; } | ||
|
||
public string Type { get; set; } | ||
} |
41 changes: 41 additions & 0 deletions
41
AnalysisData/AnalysisData/DataProcessService/DataReadProcessor.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,41 @@ | ||
using CsvHelper; | ||
using System.Globalization; | ||
using AnalysisData.DataManage.Model; | ||
using AnalysisData.Repository.AccountRepository.Abstraction; | ||
using AnalysisData.Repository.TrancsactionRepository.Abstraction; | ||
using CsvHelper.Configuration; | ||
|
||
namespace AnalysisData.DataProcessService; | ||
|
||
public class DataReadProcessor : IDataProcessor | ||
{ | ||
private readonly IAccountRepository _accountRepository; | ||
private readonly ITransactionRepository _transactionRepository; | ||
|
||
public DataReadProcessor(IAccountRepository accountRepository, ITransactionRepository transactionRepository) | ||
{ | ||
_accountRepository = accountRepository; | ||
_transactionRepository = transactionRepository; | ||
} | ||
|
||
public async Task ProcessDataAsync(Stream fileStream, string fileType) | ||
{ | ||
var reader = new StreamReader(fileStream); | ||
var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)); | ||
|
||
if (fileType == "account") | ||
{ | ||
var accounts = csv.GetRecords<Account>().ToList(); | ||
await _accountRepository.AddAccountsAsync(accounts); | ||
} | ||
else if (fileType == "transaction") | ||
{ | ||
var transactions = csv.GetRecords<Transaction>().ToList(); | ||
await _transactionRepository.AddTransactionsAsync(transactions); | ||
} | ||
else | ||
{ | ||
throw new ArgumentException("Invalid file type"); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
AnalysisData/AnalysisData/DataProcessService/IDataProcessor.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,6 @@ | ||
namespace AnalysisData.DataProcessService; | ||
|
||
public interface IDataProcessor | ||
{ | ||
Task ProcessDataAsync(Stream fileStream, string fileType); | ||
} |
102 changes: 0 additions & 102 deletions
102
AnalysisData/AnalysisData/Migrations/20240815172546_InitialCreate.Designer.cs
This file was deleted.
Oops, something went wrong.
69 changes: 0 additions & 69 deletions
69
AnalysisData/AnalysisData/Migrations/20240815172546_InitialCreate.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.