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

!!!PLEASE DO NOT MERGE!! 460836 Azure Blob Storage - Store file #66

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions src/EPR.Calculator.API/Controllers/CalculatorController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Azure.Messaging.ServiceBus;
using Azure.Storage.Blobs;
using EPR.Calculator.API.Data;
using EPR.Calculator.API.Data.DataModels;
using EPR.Calculator.API.Dtos;
Expand All @@ -8,6 +9,10 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Azure;
using Newtonsoft.Json;
using System;
using System.Text;
using System.IO;
using EPR.Calculator.API.Utils;

namespace EPR.Calculator.API.Controllers
{
Expand All @@ -25,6 +30,47 @@ public CalculatorController(ApplicationDBContext context, IConfiguration configu
_serviceBusClientFactory = serviceBusClientFactory;
}

[HttpGet]
[Route("storeResultFile")]
public async Task<IActionResult> StoreResultFile()
{
try
{
var calculatorRuns = _context.CalculatorRuns;

var csvContent = new StringBuilder();

var properties = typeof(CalculatorRun).GetProperties();

csvContent.AppendLine(string.Join(",", properties.Select(p => CsvSanitiser.SanitiseData(p.Name))));

foreach (var calculatorRun in calculatorRuns)
{
var values = properties.Select(p => CsvSanitiser.SanitiseData(p.GetValue(calculatorRun, null)));
csvContent.AppendLine(string.Join(",", values));
}

System.IO.File.WriteAllText("result.csv", csvContent.ToString());

var blobStorageConnectionString = this._configuration.GetSection("BlobStorage").GetSection("ConnectionString").Value;
var blobStorageContainerName = this._configuration.GetSection("BlobStorage").GetSection("ContainerName").Value;

BlobServiceClient blobServiceClient = new BlobServiceClient(blobStorageConnectionString);
BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(blobStorageContainerName);
BlobClient blobClient = blobContainerClient.GetBlobClient("result.csv");

using var fileStream = System.IO.File.OpenRead("result.csv");
await blobClient.UploadAsync(fileStream, true);
fileStream.Close();

return new OkResult();
}
catch (Exception exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, exception);
}
}

[HttpPost]
[Route("calculatorRun")]
public async Task<IActionResult> Create([FromBody] CreateCalculatorRunDto request)
Expand Down
1 change: 1 addition & 0 deletions src/EPR.Calculator.API/EPR.Calculator.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.18.2" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.2" />
<PackageReference Include="FluentValidation" Version="11.9.2" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
Expand Down
32 changes: 32 additions & 0 deletions src/EPR.Calculator.API/Utils/CsvSanitiser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Newtonsoft.Json;

namespace EPR.Calculator.API.Utils
{
public static class CsvSanitiser
{
public static string SanitiseData<T>(T value)
{
var stringToSanitise = string.Empty;

if (value == null)
{
return string.Empty;
}

if (value is object)
{
stringToSanitise = JsonConvert.SerializeObject(value);
}

stringToSanitise = value is object
? JsonConvert.SerializeObject(value)
: value.ToString();

stringToSanitise = stringToSanitise.Replace("\r", string.Empty).Replace("\n", string.Empty).Trim();

stringToSanitise = stringToSanitise.Replace(",", string.Empty);

return stringToSanitise;
}
}
}
4 changes: 4 additions & 0 deletions src/EPR.Calculator.API/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
"QueueName": "Queue1",
"PostMessageRetryCount": 3,
"PostMessageRetryPeriod": 2
},
"BlobStorage": {
"ConnectionString": "Some connection string",
"ContainerName": "Some container"
}
}