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