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.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; public DataSetsController( AppDbContext _db, GoogleSheetsHelper _googleSheetsHelper, GoogleDriveHelper _googleDriveHelper) { db = _db; googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values; googleDriveHelper = _googleDriveHelper; } [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); } // 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 dataRows, Guid currentUserId) { try { List ids = new List(); 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; } } } }