AutoImport GSheet
This commit is contained in:
@@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
@@ -19,19 +20,23 @@ namespace WebAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
// [Authorize]
|
||||
[Authorize]
|
||||
public class DataSetsController : Controller
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private GoogleDriveHelper googleDriveHelper;
|
||||
private readonly IConfiguration configuration;
|
||||
public DataSetsController(
|
||||
AppDbContext _db,
|
||||
GoogleSheetsHelper _googleSheetsHelper,
|
||||
GoogleDriveHelper _googleDriveHelper) {
|
||||
GoogleDriveHelper _googleDriveHelper,
|
||||
IConfiguration _configuration)
|
||||
{
|
||||
db = _db;
|
||||
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
||||
googleDriveHelper = _googleDriveHelper;
|
||||
configuration = _configuration;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -52,9 +57,9 @@ namespace WebAPI.Controllers
|
||||
try
|
||||
{
|
||||
Request.Headers.TryGetValue("userId", out var value);
|
||||
Guid currentUserId = new Guid(value);
|
||||
Guid currentUserId = new Guid(value);
|
||||
return Ok(AddDataSet(input, currentUserId).Id);
|
||||
} catch(Exception e)
|
||||
} catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
@@ -104,6 +109,30 @@ namespace WebAPI.Controllers
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("autoImport/{apiKey}")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult autoImport(string apiKey)
|
||||
{
|
||||
if (Request.Host.Value != "localhost:5400" || apiKey != configuration["apiKey"])
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
string sheetId = "1G_Hu8DTP-PSPNXTaVYhc_ppnTQi6HWoA4oXSSdUmM9E";
|
||||
string sheetName = "KOSZTY";
|
||||
|
||||
DataSet dataSet = new DataSet();
|
||||
dataSet.Source = "GoogleSheet";
|
||||
dataSet.Number = db.DataSets.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
dataSet.DataRows = parsedSheet.dataRows;
|
||||
dataSet.Name = $"W{dataSet.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
AddDataSet(dataSet, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
|
||||
return Ok("OK");
|
||||
}
|
||||
|
||||
//
|
||||
private DataSet AddDataSet(DataSet input, Guid currentUserId)
|
||||
{
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
},
|
||||
"GoogleCLientId": "107631825312-bkfe438ehr9k9ecb2h76g802tj6advma.apps.googleusercontent.com",
|
||||
"Secret": "8393AF8EAEF8478CB738D44858690F9C7E2D19F65896DD9FBAA3EB2A6F493E80",
|
||||
"apiKey": "9ecb2h76g802tF65896DD9FBAA3EB2A6F493",
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace WebAPI.dataParsers
|
||||
googleSheetValues = _googleSheetValues;
|
||||
}
|
||||
|
||||
public List<DataRow> parse(string sheetId)
|
||||
public dynamic parse(string sheetId)
|
||||
{
|
||||
var range = "Arkusz1!A:B";
|
||||
|
||||
@@ -23,6 +23,8 @@ namespace WebAPI.dataParsers
|
||||
|
||||
List<DataRow> dataRows = new List<DataRow>();
|
||||
|
||||
string date = (string)data[0][0];
|
||||
|
||||
for (int i = 1; i < data.Count; i++)
|
||||
{
|
||||
float value = float.Parse(data[i][1].ToString(), CultureInfo.GetCultureInfo("pl-PL"));
|
||||
@@ -37,7 +39,11 @@ namespace WebAPI.dataParsers
|
||||
dataRows.Add(dataRow);
|
||||
}
|
||||
}
|
||||
return dataRows;
|
||||
return new
|
||||
{
|
||||
dataRows = dataRows,
|
||||
date = date
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user