Skip to content

Commit

Permalink
feat(ReadData): Read CSV Data format (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: AmirReza <[email protected]>
  • Loading branch information
mahdijafariii and AmirRezaZahedi authored Aug 17, 2024
1 parent 1299b55 commit e6d408b
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 274 deletions.
1 change: 1 addition & 0 deletions AnalysisData/AnalysisData/AnalysisData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="JWT" Version="10.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
Expand Down
46 changes: 46 additions & 0 deletions AnalysisData/AnalysisData/Controllers/UploadDataController.cs
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.");
}
}
6 changes: 4 additions & 2 deletions AnalysisData/AnalysisData/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using AnalysisData.DataManage.Model;
using Microsoft.EntityFrameworkCore;

namespace AnalysisData.Data;

Expand All @@ -10,5 +11,6 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }

public DbSet<Account> Accounts { get; set; }
public DbSet<Transaction> Transactions { get; set; }
}
15 changes: 15 additions & 0 deletions AnalysisData/AnalysisData/DataManage/Model/Account.cs
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 AnalysisData/AnalysisData/DataManage/Model/Trancsaction.cs
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 AnalysisData/AnalysisData/DataProcessService/DataReadProcessor.cs
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");
}
}
}
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);
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit e6d408b

Please sign in to comment.