??
This commit is contained in:
@@ -1,182 +1,182 @@
|
||||
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 LayersController : Controller
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private GoogleDriveHelper googleDriveHelper;
|
||||
private readonly IConfiguration configuration;
|
||||
public LayersController(
|
||||
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.Layers.Where(x => !x.IsDeleted).ToList());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public IActionResult Save(Layer input)
|
||||
{
|
||||
try
|
||||
{
|
||||
Request.Headers.TryGetValue("userId", out var value);
|
||||
Guid currentUserId = new Guid(value!);
|
||||
return Ok(AddLayer(input, currentUserId).Id);
|
||||
} catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("{id}")]
|
||||
public IActionResult Get(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(db.Layers
|
||||
.Include(x => x.CreatedBy)
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.Id == id && !x.IsDeleted).First());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("parseGoogleSheet/{sheetId}")]
|
||||
public IActionResult ParseGoogleSheet(string sheetId)
|
||||
{
|
||||
|
||||
string sheetName = "KOSZTY";
|
||||
|
||||
Layer layer = new Layer();
|
||||
layer.Source = "GoogleSheet";
|
||||
layer.Number = db.Layers.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
layer.Records = parsedSheet.records;
|
||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
|
||||
return Ok(layer);
|
||||
}
|
||||
[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)
|
||||
{
|
||||
Layer layer = db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.Id == id && !x.IsDeleted).First();
|
||||
|
||||
var export = new googleSheetExport(googleDriveHelper, googleSheetValues);
|
||||
export.export(layer);
|
||||
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";
|
||||
|
||||
Layer layer = new Layer();
|
||||
layer.Source = "GoogleSheet";
|
||||
layer.Number = db.Layers.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
layer.Records = parsedSheet.records;
|
||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
|
||||
return Ok("OK");
|
||||
}
|
||||
|
||||
//
|
||||
private Layer AddLayer(Layer input, Guid currentUserId)
|
||||
{
|
||||
input.Number = db.Layers.Count() + 1;
|
||||
input.CreatedById = currentUserId;
|
||||
input.ModifiedById = currentUserId;
|
||||
input.CreatedAt = DateTime.UtcNow;
|
||||
input.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
db.Layers.Add(input);
|
||||
SaveRecords(input.Id, input.Records!, currentUserId);
|
||||
db.SaveChanges();
|
||||
return input;
|
||||
}
|
||||
|
||||
private void SaveRecords(Guid id, ICollection<Models.Record> records, Guid currentUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Guid> ids = new List<Guid>();
|
||||
foreach (Record record in records)
|
||||
{
|
||||
record.CreatedById = currentUserId;
|
||||
record.CreatedAt = DateTime.UtcNow;
|
||||
record.ModifiedById = currentUserId;
|
||||
record.ModifiedAt = DateTime.UtcNow;
|
||||
record.LayerId= id;
|
||||
|
||||
db.Records.Add(record);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
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 LayersController : Controller
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private GoogleDriveHelper googleDriveHelper;
|
||||
private readonly IConfiguration configuration;
|
||||
public LayersController(
|
||||
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.Layers.Where(x => !x.IsDeleted).ToList());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public IActionResult Save(Layer input)
|
||||
{
|
||||
try
|
||||
{
|
||||
Request.Headers.TryGetValue("userId", out var value);
|
||||
Guid currentUserId = new Guid(value!);
|
||||
return Ok(AddLayer(input, currentUserId).Id);
|
||||
} catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("{id}")]
|
||||
public IActionResult Get(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(db.Layers
|
||||
.Include(x => x.CreatedBy)
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.Id == id && !x.IsDeleted).First());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("parseGoogleSheet/{sheetId}")]
|
||||
public IActionResult ParseGoogleSheet(string sheetId)
|
||||
{
|
||||
|
||||
string sheetName = "KOSZTY";
|
||||
|
||||
Layer layer = new Layer();
|
||||
layer.Source = "GoogleSheet";
|
||||
layer.Number = db.Layers.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
layer.Records = parsedSheet.records;
|
||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
|
||||
return Ok(layer);
|
||||
}
|
||||
[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)
|
||||
{
|
||||
Layer layer = db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.Id == id && !x.IsDeleted).First();
|
||||
|
||||
var export = new googleSheetExport(googleDriveHelper, googleSheetValues);
|
||||
export.export(layer);
|
||||
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";
|
||||
|
||||
Layer layer = new Layer();
|
||||
layer.Source = "GoogleSheet";
|
||||
layer.Number = db.Layers.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
layer.Records = parsedSheet.records;
|
||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
|
||||
return Ok("OK");
|
||||
}
|
||||
|
||||
//
|
||||
private Layer AddLayer(Layer input, Guid currentUserId)
|
||||
{
|
||||
input.Number = db.Layers.Count() + 1;
|
||||
input.CreatedById = currentUserId;
|
||||
input.ModifiedById = currentUserId;
|
||||
input.CreatedAt = DateTime.UtcNow;
|
||||
input.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
db.Layers.Add(input);
|
||||
SaveRecords(input.Id, input.Records!, currentUserId);
|
||||
db.SaveChanges();
|
||||
return input;
|
||||
}
|
||||
|
||||
private void SaveRecords(Guid id, ICollection<Models.Record> records, Guid currentUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Guid> ids = new List<Guid>();
|
||||
foreach (Record record in records)
|
||||
{
|
||||
record.CreatedById = currentUserId;
|
||||
record.CreatedAt = DateTime.UtcNow;
|
||||
record.ModifiedById = currentUserId;
|
||||
record.ModifiedAt = DateTime.UtcNow;
|
||||
record.LayerId= id;
|
||||
|
||||
db.Records.Add(record);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user