From ab5ab399b088b278905cc5b005831602405a0ff5 Mon Sep 17 00:00:00 2001 From: Sascha Date: Thu, 23 Feb 2023 23:38:25 +0100 Subject: [PATCH] try to save to db --- .vscode/launch.json | 69 ++++++++----------- .vscode/tasks.json | 24 +++++++ .../Data/EntityClasses/Account.cs | 16 ++--- Gremlin_BlazorServer/Pages/Accounts.razor | 9 +-- .../Services/GenericController.cs | 33 +++++++-- .../Services/GenericImporter.cs | 18 +++-- 6 files changed, 104 insertions(+), 65 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 42fe90e..1bd76d6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,41 +1,30 @@ { - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": ".NET Core Launch (console)", - "type": "coreclr", - "request": "launch", - "WARNING01": "*********************************************************************************", - "WARNING02": "The C# extension was unable to automatically decode projects in the current", - "WARNING03": "workspace to create a runnable launch.json file. A template launch.json file has", - "WARNING04": "been created as a placeholder.", - "WARNING05": "", - "WARNING06": "If OmniSharp is currently unable to load your project, you can attempt to resolve", - "WARNING07": "this by restoring any missing project dependencies (example: run 'dotnet restore')", - "WARNING08": "and by fixing any reported errors from building the projects in your workspace.", - "WARNING09": "If this allows OmniSharp to now load your project then --", - "WARNING10": " * Delete this file", - "WARNING11": " * Open the Visual Studio Code command palette (View->Command Palette)", - "WARNING12": " * run the command: '.NET: Generate Assets for Build and Debug'.", - "WARNING13": "", - "WARNING14": "If your project requires a more complex launch configuration, you may wish to delete", - "WARNING15": "this configuration and pick a different template using the 'Add Configuration...'", - "WARNING16": "button at the bottom of this file.", - "WARNING17": "*********************************************************************************", - "preLaunchTask": "build", - "program": "${workspaceFolder}/bin/Debug//.dll", - "args": [], - "cwd": "${workspaceFolder}", - "console": "internalConsole", - "stopAtEntry": false - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach" - } - ] -} \ No newline at end of file + // Verwendet IntelliSense zum Ermitteln möglicher Attribute. + // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. + // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (web)", + "type": "coreclr", + "preLaunchTask": "build", + "request": "launch", + "program": "${workspaceFolder}/bin/Debug/net7.0/Gremlin_BlazorServer", + "args": [], + "cwd": "${workspaceFolder}", + "stopAtEntry": false, + "requireExactSource": false, + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_URLS": "https://localhost:5000" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + } + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..92eac3b --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,24 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "shell", + "args": [ + "build", + // Ask dotnet build to generate full paths for file names. + "/property:GenerateFullPaths=true", + // Do not generate summary otherwise it leads to duplicate errors in Problems panel + "/consoleloggerparameters:NoSummary" + ], + "group": "build", + "presentation": { + "reveal": "silent" + }, + "problemMatcher": "$msCompile" + } + ] +} diff --git a/Gremlin_BlazorServer/Data/EntityClasses/Account.cs b/Gremlin_BlazorServer/Data/EntityClasses/Account.cs index 32efc7c..bf93a09 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/Account.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/Account.cs @@ -10,24 +10,24 @@ public class Account : IMetadata //foreign keys: public IList? Contacts { get; set; } - public uint ParentAccountId { get; set; } + public uint ParentAccountId { get; set; } = 0; public AccountType? AccountType { get; set; } public SubMarket? SubMarket { get; set; } public IList? CustomDescriptions { get; set; } //class properties: [Required] public string? AccountName { get; set; } - public string? Notes { get; set; } + public string Notes { get; set; } = string.Empty; [Required] public string? Street { get; set; } [Required] public uint Zip { get; set; } [Required] public string? City { get; set; } - public string? FloorOrBuilding { get; set; } + public string FloorOrBuilding { get; set; } = string.Empty; public float Longitude { get; set; } public float Latitude { get; set; } - public string? PhoneNumber { get; set; } - public string? FaxNumber { get; set; } - public string? Webpage { get; set; } - public string? EMail { get; set; } + public string PhoneNumber { get; set; } = string.Empty; + public string FaxNumber { get; set; } = string.Empty; + public string Webpage { get; set; } = string.Empty; + public string EMail { get; set; } = string.Empty; //Agilent-specific Properties: public uint SapAccountNumber { get; set; } @@ -40,7 +40,7 @@ public class Account : IMetadata public DateTime DataValidUntil { get; set; } = DateTime.MaxValue; public string DataModificationByUser { get; set; } = "Gremlin_BlazorServer"; public uint DataVersionNumber { get; set; } - public string? DataVersionComment { get; set; } + public string? DataVersionComment { get; set; } = string.Empty; public string DataStatus { get; set; } = "Active"; //IBase diff --git a/Gremlin_BlazorServer/Pages/Accounts.razor b/Gremlin_BlazorServer/Pages/Accounts.razor index ebecdc7..1aa928e 100644 --- a/Gremlin_BlazorServer/Pages/Accounts.razor +++ b/Gremlin_BlazorServer/Pages/Accounts.razor @@ -121,12 +121,9 @@ { Console.WriteLine(exception.Message); } - finally - { - Console.WriteLine("Account import successfull"); - isImportingAccounts = false; - StateHasChanged(); - } + Console.WriteLine("Account import successfull"); + isImportingAccounts = false; + StateHasChanged(); return; } diff --git a/Gremlin_BlazorServer/Services/GenericController.cs b/Gremlin_BlazorServer/Services/GenericController.cs index bc44b7a..c304fad 100644 --- a/Gremlin_BlazorServer/Services/GenericController.cs +++ b/Gremlin_BlazorServer/Services/GenericController.cs @@ -37,14 +37,37 @@ public class GenericController { return gremlinDb.Set().AsEnumerable().Last(); } - public bool Insert(T entity) where T : class, IMetadata { - gremlinDb.Set().Add(entity); - return gremlinDb.SaveChanges() > 0; + public bool Insert(T entity) where T : class, IMetadata + { + try + { + gremlinDb.Set().Add(entity); + if (typeof(T) == typeof(Account)) + { + Account account = entity as Account; + System.Console.WriteLine($"Added {account.SapAccountNumber}:{account.AccountName} to database, try to save..."); + } + return gremlinDb.SaveChanges() > 0; + } + catch (Exception exception) + { + Console.WriteLine(exception.InnerException); + return false; + } } + public bool Insert(List entities) where T : class, IMetadata { - gremlinDb.Set().AddRange(entities); - return gremlinDb.SaveChanges() > 0; + try + { + gremlinDb.Set().AddRange(entities); + return gremlinDb.SaveChanges() > 0; + } + catch (Exception exception) + { + Console.WriteLine(exception.InnerException); + return false; + } } public bool IsExisting(Predicate search) where T : class, IMetadata diff --git a/Gremlin_BlazorServer/Services/GenericImporter.cs b/Gremlin_BlazorServer/Services/GenericImporter.cs index f3e583d..53e4139 100644 --- a/Gremlin_BlazorServer/Services/GenericImporter.cs +++ b/Gremlin_BlazorServer/Services/GenericImporter.cs @@ -12,7 +12,13 @@ namespace Gremlin_BlazorServer.Services if (typeof(T) == typeof(Account)) { ListlistOfAccounts = await Task.Run(() => ParseListToAccounts(splitLines)); - return genericController.Insert(listOfAccounts); + // return genericController.Insert(listOfAccounts); + foreach (Account account in listOfAccounts) + { + Console.Write($"Try to add {account.SapAccountNumber}:{account.AccountName} to database?"); + string readLine = Console.ReadLine(); + if (readLine == "y") genericController.Insert(account); + } } return false; @@ -28,7 +34,6 @@ namespace Gremlin_BlazorServer.Services select clipboardLine.Split(';') ).ToList(); - return fileList; } @@ -52,19 +57,20 @@ namespace Gremlin_BlazorServer.Services Street = line[2], City = line[3], Zip = zip, - //TODO: AccountTypeCode AccountType = genericController.Get(aT => aT.AccountTypeCode == line[6]), - //TODO: SubMarketCode - SubMarket = genericController.Get(sM => sM.SubMarketCode == line[7]) + SubMarket = genericController.Get(sM => sM.SubMarketCode == line[7]), + ParentAccountId = 0, + DataModificationByUser = "Gremlin Generic Importer" }; if (AccountExists(account)) { //TODO: Check for changes + // Console.WriteLine($"Account {account.SapAccountNumber}:{account.AccountName} already exists..."); } else { - Console.WriteLine($"Found new account {account.AccountName}"); + // Console.WriteLine($"Found new account {account.SapAccountNumber}:{account.AccountName}!"); listOfAccounts.Add(account); } }