173 lines
5.6 KiB
C#
173 lines
5.6 KiB
C#
using Google.Apis.Auth;
|
|
using Google.Apis.Http;
|
|
using Google.Apis.Sheets.v4;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
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;
|
|
using System.Text;
|
|
using WebAPI.dataParsers;
|
|
using WebAPI.Exports;
|
|
using WebAPI.Models;
|
|
|
|
namespace WebAPI.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[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,
|
|
IConfiguration _configuration)
|
|
{
|
|
db = _db;
|
|
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
|
googleDriveHelper = _googleDriveHelper;
|
|
configuration = _configuration;
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult GetAll()
|
|
{
|
|
try
|
|
{
|
|
return Ok(db.DataSets.Where(x => !x.IsDeleted).ToList());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return BadRequest(e.ToString());
|
|
}
|
|
}
|
|
[HttpPost]
|
|
public IActionResult Save(DataSet input)
|
|
{
|
|
try
|
|
{
|
|
Request.Headers.TryGetValue("userId", out var value);
|
|
Guid currentUserId = new Guid(value);
|
|
return Ok(AddDataSet(input, currentUserId).Id);
|
|
} catch (Exception e)
|
|
{
|
|
return BadRequest(e.ToString());
|
|
}
|
|
}
|
|
[HttpGet]
|
|
[Route("{id}")]
|
|
public IActionResult Get(Guid id)
|
|
{
|
|
try
|
|
{
|
|
return Ok(db.DataSets
|
|
.Include(x => x.CreatedBy)
|
|
.Include(x => x.DataRows)
|
|
.Where(x => x.Id == id && !x.IsDeleted).First());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return BadRequest(e.ToString());
|
|
}
|
|
}
|
|
[HttpGet]
|
|
[Route("parseGoogleSheet/{sheetId}")]
|
|
public IActionResult ParseGoogleSheet(string sheetId)
|
|
{
|
|
|
|
var parser = new googleSheetParser(googleSheetValues);
|
|
return Ok(parser.parse(sheetId));
|
|
}
|
|
[HttpPost]
|
|
[DisableRequestSizeLimit]
|
|
[Route("parseFile")]
|
|
public IActionResult ParseFile()
|
|
{
|
|
var parser = new csvParser();
|
|
return Ok(parser.parse(Request.Form.Files[0]));
|
|
}
|
|
[HttpGet]
|
|
[Route("exportToGoogleSheet/{id}")]
|
|
public IActionResult ExportToGoogleSheet(Guid id)
|
|
{
|
|
DataSet dataSet = db.DataSets
|
|
.Include(x => x.DataRows)
|
|
.Where(x => x.Id == id && !x.IsDeleted).First();
|
|
|
|
var export = new googleSheetExport(googleDriveHelper, googleSheetValues);
|
|
export.export(dataSet);
|
|
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)
|
|
{
|
|
input.Number = db.DataSets.Count() + 1;
|
|
input.CreatedById = currentUserId;
|
|
input.ModifiedById = currentUserId;
|
|
input.CreatedAt = DateTime.UtcNow;
|
|
input.ModifiedAt = DateTime.UtcNow;
|
|
|
|
db.DataSets.Add(input);
|
|
SaveDataRows(input.Id, input.DataRows, currentUserId);
|
|
db.SaveChanges();
|
|
return input;
|
|
}
|
|
|
|
private void SaveDataRows(Guid id, ICollection<Models.DataRow> dataRows, Guid currentUserId)
|
|
{
|
|
try
|
|
{
|
|
List<Guid> ids = new List<Guid>();
|
|
foreach (Models.DataRow dataRow in dataRows)
|
|
{
|
|
dataRow.CreatedById = currentUserId;
|
|
dataRow.CreatedAt = DateTime.UtcNow;
|
|
dataRow.ModifiedById = currentUserId;
|
|
dataRow.ModifiedAt = DateTime.UtcNow;
|
|
dataRow.DataSetId= id;
|
|
|
|
db.DataRows.Add(dataRow);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
} |