Files
DiunaBI/WebAPI/Controllers/DataSetsController.cs

173 lines
5.6 KiB
C#
Raw Normal View History

2022-12-11 23:40:16 +01:00
using Google.Apis.Auth;
using Google.Apis.Http;
2022-12-21 22:15:17 +01:00
using Google.Apis.Sheets.v4;
2022-12-11 23:40:16 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2022-12-21 19:30:24 +01:00
using Microsoft.EntityFrameworkCore;
2022-12-11 23:40:16 +01:00
using Microsoft.Extensions.Configuration;
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
using Microsoft.IdentityModel.Tokens;
2022-12-27 14:12:45 +01:00
using System;
2022-12-11 23:40:16 +01:00
using System.Configuration;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
2022-12-19 18:36:57 +01:00
using WebAPI.dataParsers;
2022-12-22 15:12:19 +01:00
using WebAPI.Exports;
2022-12-11 23:40:16 +01:00
using WebAPI.Models;
namespace WebAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
2022-12-27 14:12:45 +01:00
[Authorize]
2022-12-11 23:40:16 +01:00
public class DataSetsController : Controller
{
private readonly AppDbContext db;
2022-12-21 22:15:17 +01:00
private SpreadsheetsResource.ValuesResource googleSheetValues;
2022-12-22 15:12:19 +01:00
private GoogleDriveHelper googleDriveHelper;
2022-12-27 14:12:45 +01:00
private readonly IConfiguration configuration;
2022-12-22 15:12:19 +01:00
public DataSetsController(
AppDbContext _db,
GoogleSheetsHelper _googleSheetsHelper,
2022-12-27 14:12:45 +01:00
GoogleDriveHelper _googleDriveHelper,
IConfiguration _configuration)
{
2022-12-21 22:15:17 +01:00
db = _db;
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
2022-12-22 15:12:19 +01:00
googleDriveHelper = _googleDriveHelper;
2022-12-27 14:12:45 +01:00
configuration = _configuration;
2022-12-21 22:15:17 +01:00
}
2022-12-11 23:40:16 +01:00
[HttpGet]
public IActionResult GetAll()
{
try
{
return Ok(db.DataSets.Where(x => !x.IsDeleted).ToList());
}
catch (Exception e)
{
return BadRequest(e.ToString());
}
}
2022-12-19 18:36:57 +01:00
[HttpPost]
2022-12-21 18:35:26 +01:00
public IActionResult Save(DataSet input)
{
try
{
Request.Headers.TryGetValue("userId", out var value);
2022-12-27 14:12:45 +01:00
Guid currentUserId = new Guid(value);
2022-12-21 18:35:26 +01:00
return Ok(AddDataSet(input, currentUserId).Id);
2022-12-27 14:12:45 +01:00
} catch (Exception e)
2022-12-21 18:35:26 +01:00
{
return BadRequest(e.ToString());
}
}
2022-12-21 19:30:24 +01:00
[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());
}
}
2022-12-21 22:15:17 +01:00
[HttpGet]
[Route("parseGoogleSheet/{sheetId}")]
public IActionResult ParseGoogleSheet(string sheetId)
{
var parser = new googleSheetParser(googleSheetValues);
return Ok(parser.parse(sheetId));
}
2022-12-21 18:35:26 +01:00
[HttpPost]
2022-12-19 18:36:57 +01:00
[DisableRequestSizeLimit]
[Route("parseFile")]
public IActionResult ParseFile()
{
var parser = new csvParser();
return Ok(parser.parse(Request.Form.Files[0]));
}
2022-12-22 15:12:19 +01:00
[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);
}
2022-12-21 18:35:26 +01:00
2022-12-27 14:12:45 +01:00
[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");
}
2022-12-21 18:35:26 +01:00
//
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;
}
}
2022-12-11 23:40:16 +01:00
}
}