-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoiceController.cs
144 lines (119 loc) · 6.04 KB
/
InvoiceController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Microsoft.AspNetCore.Mvc;
using DataInvoiceManager.API.Core.Models;
using DataInvoiceManager.API.Core.Helpers;
using DataInvoiceManager.API.Core.Interfaces;
namespace DataInvoiceManager.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class InvoiceController : Controller
{
private readonly IWebHostEnvironment _env;
private readonly IConfiguration _config;
private readonly IInvoiceRepository _invoiceRepo;
public InvoiceController(IWebHostEnvironment env, IConfiguration config, IInvoiceRepository invoiceRepo)
{
_env = env;
_config = config;
_invoiceRepo = invoiceRepo;
}
[HttpGet("getList/{supplierId}")]
public async Task<IEnumerable<Invoice>> getList(int? supplierId, [FromQuery] int? limit)
{
return await _invoiceRepo.getList(supplierId, Common.getLimit(limit, _config));
}
[HttpGet("GetSnowflake")]
public async Task<IEnumerable<Invoice>> GetSnowflake([FromQuery] string fileName, string destPrefix, string stageName, string compression)
{ Invoice invoice = new Invoice();
var requestContent = new MultipartFormDataContent();
requestContent.Add(new StringContent(fileName), "fileName");
requestContent.Add(new StreamContent(file.OpenReadStream()), "files");
requestContent.Add(new StringContent(destPrefix), "destPrefix");
requestContent.Add(new StringContent(compression), "compression");
requestContent.Add(new StringContent(stageName), "stageName");
var client = await Common.getClient(_config);
string url = Common.getConfigValue(_config, "AzureBaseURL");
url = url + "FileUpload";
var response = await client.PostAsync(url,requestContent);
var payload = JsonSerializer.Deserialize<Invoice>(response);
Stream streamToReadFrom = await response.Content.ReadAsStreamAsync();
invoice.fileContent = streamToReadFrom;
InsertToSnowflake();
return invoices;
}
[HttpGet("snowflakeInsert")]
public async Task<long> InsertToSnowflake()
{
var repo = new InvoiceRepo();
return await repo.StoreAsync();
}
[HttpGet("getInvoiceFile")]
public async Task<IActionResult> getInvoiceFile([FromQuery] string fileName, string destPrefix, string stageName, string compression)
{
var client = await Common.getClient(_config);
string url = Common.getConfigValue(_config, "AzureBaseURL");
url = url + "FileDownload";
client.BaseAddress = new Uri(url);
var requestContent = new MultipartFormDataContent();
requestContent.Add(new StringContent(destPrefix), "destPrefix");
requestContent.Add(new StringContent(fileName), "fileName");
requestContent.Add(new StringContent(compression), "compression");
requestContent.Add(new StringContent(stageName), "stageName");
try
{
var response = await client.PostAsync(url, requestContent);
if (response.IsSuccessStatusCode)
{
return Ok(await response.Content.ReadAsStreamAsync());
}
return NotFound(response.StatusCode.ToString());
}
catch (Exception ex)
{
return NotFound(ex.Message);
}
}
[HttpPost("uploadFile")]
public async Task<IActionResult> uploadFile(IFormCollection data)
{
string clientId, clientSecret;
InvoiceinputParameter invoiceData = new InvoiceinputParameter();
try
{
Stream fileData;
string compression, destPrefix, supplierId;
var file = Request.Form.Files[0];
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
// invoiceData.supplierId = data["supplierId"];
invoiceData.destPrefix = data["supplierId"];
invoiceData.compression = Common.getConfigValue(_config, "Compression");
invoiceData.stageName = Common.getConfigValue(_config, "StageName");
invoiceData.destPrefix = data["destPrefix"];
invoiceData.fileName = fileName;
invoiceData.files = file;
//string invoicePath = _env.ContentRootPath.ToString();
var client = await Common.getClient(_config);
string url = Common.getConfigValue(_config, "AzureBaseURL");
url = url + "fileUpload";
client.BaseAddress = new Uri(url);
var requestContent = new MultipartFormDataContent();
// requestContent.Add(new StringContent(invoiceData.supplierId), "supplierId");
requestContent.Add(new StreamContent(invoiceData.files.OpenReadStream()), "file");
requestContent.Add(new StringContent(invoiceData.destPrefix), "destPrefix");
requestContent.Add(new StringContent(invoiceData.fileName), "fileName");
requestContent.Add(new StringContent(invoiceData.compression), "compression");
requestContent.Add(new StringContent(invoiceData.stageName), "stageName");
var response = await client.PostAsync(url, requestContent);
if (response.IsSuccessStatusCode)
{
return Ok(await response.Content.ReadAsStreamAsync());
}
return NotFound(response.StatusCode.ToString());
}
catch (Exception ex)
{
return NotFound(ex.Message);
}
}
}
}