-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CsvReader, CsvWriter, ExcelReader, ExcelWriter
- Loading branch information
1 parent
773eda9
commit 849fdb4
Showing
19 changed files
with
140 additions
and
91 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/Monolith/ClassifiedAds.Application/Products/DTOs/ExportProductsToCsv.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,9 @@ | ||
using ClassifiedAds.Domain.Entities; | ||
using System.Collections.Generic; | ||
|
||
namespace ClassifiedAds.Application.Products.DTOs; | ||
|
||
public record ExportProductsToCsv | ||
{ | ||
public List<Product> Products { get; set; } | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Monolith/ClassifiedAds.Application/Products/DTOs/ImportProductsFromCsv.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,9 @@ | ||
using ClassifiedAds.Domain.Entities; | ||
using System.Collections.Generic; | ||
|
||
namespace ClassifiedAds.Application.Products.DTOs; | ||
|
||
public record ImportProductsFromCsv | ||
{ | ||
public List<Product> Products { get; set; } | ||
} |
6 changes: 3 additions & 3 deletions
6
src/Monolith/ClassifiedAds.CrossCuttingConcerns/Csv/ICsvReader.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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.CrossCuttingConcerns.Csv; | ||
|
||
public interface ICsvReader<T> | ||
{ | ||
IEnumerable<T> Read(Stream stream); | ||
Task<T> ReadAsync(Stream stream); | ||
} |
6 changes: 3 additions & 3 deletions
6
src/Monolith/ClassifiedAds.CrossCuttingConcerns/Csv/ICsvWriter.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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.CrossCuttingConcerns.Csv; | ||
|
||
public interface ICsvWriter<T> | ||
{ | ||
void Write(IEnumerable<T> collection, Stream stream); | ||
Task WriteAsync(T data, Stream stream); | ||
} |
3 changes: 2 additions & 1 deletion
3
src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelReader.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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.CrossCuttingConcerns.Excel; | ||
|
||
public interface IExcelReader<T> | ||
{ | ||
T Read(Stream stream); | ||
Task<T> ReadAsync(Stream stream); | ||
} |
3 changes: 2 additions & 1 deletion
3
src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelWriter.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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.CrossCuttingConcerns.Excel; | ||
|
||
public interface IExcelWriter<T> | ||
{ | ||
void Write(T data, Stream stream); | ||
Task WriteAsync(T data, Stream stream); | ||
} |
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
17 changes: 0 additions & 17 deletions
17
src/Monolith/ClassifiedAds.Infrastructure/Csv/CsvReader.cs
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/Monolith/ClassifiedAds.Infrastructure/Csv/CsvWriter.cs
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/Monolith/ClassifiedAds.Infrastructure/Csv/ExportProductsToCsvHandler.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,19 @@ | ||
using ClassifiedAds.Application.Products.DTOs; | ||
using ClassifiedAds.CrossCuttingConcerns.Csv; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Infrastructure.Csv; | ||
|
||
public class ExportProductsToCsvHandler : ICsvWriter<ExportProductsToCsv> | ||
{ | ||
public Task WriteAsync(ExportProductsToCsv data, Stream stream) | ||
{ | ||
using var writer = new StreamWriter(stream); | ||
using var csv = new CsvHelper.CsvWriter(writer, CultureInfo.InvariantCulture); | ||
csv.WriteRecords(data.Products); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Monolith/ClassifiedAds.Infrastructure/Csv/ImportProductsFromCsvHandler.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,33 @@ | ||
using ClassifiedAds.Application.Products.DTOs; | ||
using ClassifiedAds.CrossCuttingConcerns.Csv; | ||
using ClassifiedAds.Domain.Entities; | ||
using CsvHelper.Configuration; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Infrastructure.Csv; | ||
|
||
public class ImportProductsFromCsvHandler : ICsvReader<ImportProductsFromCsv> | ||
{ | ||
public Task<ImportProductsFromCsv> ReadAsync(Stream stream) | ||
{ | ||
using var reader = new StreamReader(stream); | ||
|
||
var config = new CsvConfiguration(CultureInfo.InvariantCulture) | ||
{ | ||
HeaderValidated = null, | ||
MissingFieldFound = null, | ||
}; | ||
|
||
using var csv = new CsvHelper.CsvReader(reader, config); | ||
|
||
var response = new ImportProductsFromCsv | ||
{ | ||
Products = csv.GetRecords<Product>().ToList(), | ||
}; | ||
|
||
return Task.FromResult(response); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.