diff --git a/Frontend/src/app/models/dataSet.model.ts b/Frontend/src/app/models/dataSet.model.ts index 7a3a7b6..484229b 100644 --- a/Frontend/src/app/models/dataSet.model.ts +++ b/Frontend/src/app/models/dataSet.model.ts @@ -29,6 +29,7 @@ export class DataSet extends Base { id: [null], name: ['', Validators.required], source: ['', Validators.required], + sheetId: '1G_Hu8DTP-PSPNXTaVYhc_ppnTQi6HWoA4oXSSdUmM9E', createdAt: '', modifiedAt: '', createdBy: '', @@ -91,4 +92,16 @@ export class DataSet extends Base { }) }) } + static parseGoogleSheet(sheetId: string, _http: HttpClient): Promise { + return new Promise((resolve, reject) => { + _http.get(`${environment.api.url}/datasets/parseGoogleSheet/${sheetId}`, + ).pipe(map(data => data.map(x => new DataRow().deserialize(x)))) + .subscribe({ + next: (data) => { + resolve(data); + }, + error: (e) => reject(e) + }) + }) + } } \ No newline at end of file diff --git a/Frontend/src/app/modules/data-sets/data-set-edit/data-set-edit.component.html b/Frontend/src/app/modules/data-sets/data-set-edit/data-set-edit.component.html index ae77a1a..24c61fa 100644 --- a/Frontend/src/app/modules/data-sets/data-set-edit/data-set-edit.component.html +++ b/Frontend/src/app/modules/data-sets/data-set-edit/data-set-edit.component.html @@ -1,69 +1,82 @@
-
- - - Edycja warstwy danych - - - - - - - - - Nazwa - - - Pole obowiązkowe - - - - - - - - CSV - GoogleSheet - SAP - Symfonia - - - Pole obowiązkowe - - - - + + + + Edycja warstwy danych + + + + + + + + + Nazwa + + + Pole obowiązkowe + + + - - + + + + CSV + GoogleSheet + SAP + Symfonia + + + Pole obowiązkowe + + + + + + + + + + + + + + + + + + + - - - - MPK - {{item.code}} - - - - Konto - {{item.desc1}} - + - - Wartość - {{item.value | number:'1.2-2'}} - - - - - - - - - - -
\ No newline at end of file + + MPK + {{item.code}} + + + + Konto + {{item.desc1}} + + + + Wartość + {{item.value | number:'1.2-2'}} + + + + + + + + + + + \ No newline at end of file diff --git a/Frontend/src/app/modules/data-sets/data-set-edit/data-set-edit.component.ts b/Frontend/src/app/modules/data-sets/data-set-edit/data-set-edit.component.ts index 068fc4e..d643c60 100644 --- a/Frontend/src/app/modules/data-sets/data-set-edit/data-set-edit.component.ts +++ b/Frontend/src/app/modules/data-sets/data-set-edit/data-set-edit.component.ts @@ -68,4 +68,11 @@ export class DataSetEditComponent implements OnInit { trackByUid(index: number, item: DataRow) { return item.id; } + async parseGoogleSheet() { + const id = this.form.get('sheetId')?.value; + this.document.dataRows = await DataSet.parseGoogleSheet(id, this.http$); + this.dataSource = new MatTableDataSource(this.document.dataRows); + this.dataSource.paginator = this.paginator; + this.dataSource.sort = this.sort; + } } diff --git a/WebAPI/Controllers/DataSetsController.cs b/WebAPI/Controllers/DataSetsController.cs index 10651a9..666ed17 100644 --- a/WebAPI/Controllers/DataSetsController.cs +++ b/WebAPI/Controllers/DataSetsController.cs @@ -1,5 +1,6 @@ using Google.Apis.Auth; using Google.Apis.Http; +using Google.Apis.Sheets.v4; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -17,11 +18,15 @@ namespace WebAPI.Controllers { [ApiController] [Route("api/[controller]")] - [Authorize] + // [Authorize] public class DataSetsController : Controller { private readonly AppDbContext db; - public DataSetsController(AppDbContext _db) { db = _db; } + private SpreadsheetsResource.ValuesResource googleSheetValues; + public DataSetsController(AppDbContext _db, GoogleSheetsHelper _googleSheetsHelper) { + db = _db; + googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values; + } [HttpGet] public IActionResult GetAll() @@ -64,6 +69,14 @@ namespace WebAPI.Controllers 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")] diff --git a/WebAPI/GoogleSheetsHelper.cs b/WebAPI/GoogleSheetsHelper.cs new file mode 100644 index 0000000..b9fcd34 --- /dev/null +++ b/WebAPI/GoogleSheetsHelper.cs @@ -0,0 +1,35 @@ +using Google.Apis.Auth.OAuth2; +using Google.Apis.Services; +using Google.Apis.Sheets.v4; + +namespace WebAPI +{ + public class GoogleSheetsHelper + { + public SheetsService Service { get; set; } + const string APPLICATION_NAME = "Diuna"; + static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets }; + public GoogleSheetsHelper() + { + InitializeService(); + } + private void InitializeService() + { + var credential = GetCredentialsFromFile(); + Service = new SheetsService(new BaseClientService.Initializer() + { + HttpClientInitializer = credential, + ApplicationName = APPLICATION_NAME + }); + } + private GoogleCredential GetCredentialsFromFile() + { + GoogleCredential credential; + using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) + { + credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes); + } + return credential; + } + } +} diff --git a/WebAPI/Program.cs b/WebAPI/Program.cs index 756ff03..b2580d3 100644 --- a/WebAPI/Program.cs +++ b/WebAPI/Program.cs @@ -53,6 +53,8 @@ builder.Services.AddAuthorization(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(typeof(GoogleSheetsHelper)); + var app = builder.Build(); app.Use(async (context, next) => diff --git a/WebAPI/WebAPI.csproj b/WebAPI/WebAPI.csproj index dead182..a962f28 100644 --- a/WebAPI/WebAPI.csproj +++ b/WebAPI/WebAPI.csproj @@ -9,6 +9,7 @@ + diff --git a/WebAPI/client_secrets.json b/WebAPI/client_secrets.json new file mode 100644 index 0000000..bc987f5 --- /dev/null +++ b/WebAPI/client_secrets.json @@ -0,0 +1,12 @@ +{ + "type": "service_account", + "project_id": "diuna-370117", + "private_key_id": "f48fd588724e6733b9639fe7d7933091b96be34f", + "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCenqveXpGKXA10\npsAQ4Wreeiom9GMbZywnqMAhxc0wobI7EfnbP4FPOjf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6cZ2y+SvAxunRRRnLykchq5cVJ+4xlB8bWm7/L9xPQ0LJvJyVblAiIgD/o/AqdKz\nSXV1lpn69Zh+ZRnhYqu9+jL1/HOzS7Au2+4GgpZjIHwB6R36SGup3slpAoGAZW2j\nSxsjQjh6x2XIWfWQbVqZLQXKFhjta7XrD8FI5XekcUfiAWuI0q5edghgp9D9T2JC\naH5p4GLgyt9zpMTdCSpm8RRQT93905jxw/X51JpPQddO6psRE0K/i3YTD8SN5NgG\nXLF4FpLfkozncZMuOXl23HcYKHZFZMYql/FDWkUCgYAjGQKzYV7IXA7UDAY3ejaw\nWMbsDttSPQ0E1ouuJWIX/eb4SXYr0u/gdLuX1uM7EsxqIGVFWfgtUGopoVGr604S\ng+dfOPZgUzaGAlUE2iRMVp6YoRRbrvPsYJwDrV0Xwil1k6UEzn8bgXO/IQ4fgIWj\nkxS5sDkZ6LVSCfDn5tLThg==\n-----END PRIVATE KEY-----\n", + "client_email": "diuna-backend@diuna-370117.iam.gserviceaccount.com", + "client_id": "101546901561736131820", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/diuna-backend%40diuna-370117.iam.gserviceaccount.com" +} diff --git a/WebAPI/dataParsers/googleSheet.parser.cs b/WebAPI/dataParsers/googleSheet.parser.cs new file mode 100644 index 0000000..31f98f5 --- /dev/null +++ b/WebAPI/dataParsers/googleSheet.parser.cs @@ -0,0 +1,43 @@ +using Google.Apis.Sheets.v4; +using System.Globalization; +using WebAPI.Models; + +namespace WebAPI.dataParsers +{ + public class googleSheetParser + { + private SpreadsheetsResource.ValuesResource googleSheetValues; + + public googleSheetParser(SpreadsheetsResource.ValuesResource _googleSheetValues) + { + googleSheetValues = _googleSheetValues; + } + + public List parse(string sheetId) + { + var range = "Arkusz1!A:B"; + + var request = googleSheetValues.Get(sheetId, range); + var response = request.Execute(); + var data = response.Values; + + List dataRows = new List(); + + for (int i = 1; i < data.Count; i++) + { + float value = float.Parse(data[i][1].ToString(), CultureInfo.GetCultureInfo("pl-PL")); + if (value > 0) + { + DataRow dataRow = new DataRow(); + dataRow.Id = Guid.NewGuid(); + dataRow.Code = data[i][0].ToString(); + dataRow.Value = value; + dataRow.CreatedAt = DateTime.UtcNow; + dataRow.ModifiedAt = DateTime.UtcNow; + dataRows.Add(dataRow); + } + } + return dataRows; + } + } +}