diff --git a/Project.Model/CreateLogin.cs b/Project.Model/NewUser.cs similarity index 74% rename from Project.Model/CreateLogin.cs rename to Project.Model/NewUser.cs index cd7fac3..40af6fc 100644 --- a/Project.Model/CreateLogin.cs +++ b/Project.Model/NewUser.cs @@ -1,12 +1,12 @@ namespace Project.Model; -public class CreateLogin +public class NewUser { public string NewLoginName { get; set; } public string NewMasterPassword { get; set; } public string NewEmail { get; set; } - public CreateLogin(string username, string password, string email) + public NewUser(string username, string password, string email) { NewLoginName = username; NewMasterPassword = password; diff --git a/Project.Persistence/SQLite.cs b/Project.Persistence/SQLite.cs index beed5c8..01ccb40 100644 --- a/Project.Persistence/SQLite.cs +++ b/Project.Persistence/SQLite.cs @@ -1,11 +1,16 @@ using Microsoft.Data.Sqlite; +using Project.Model; namespace Project.Persistence; public class SQLite { - private string _dbPath = "C:/Users/lowns/Desktop/logins.db"; - - public void Init() + private static string _dbPath = "C:/Users/Soi/Project_Keywi/logins.db"; + //KLASSENVARIABLEN ERSTELLEN + //private static string loginname; + + + //checks if there is a sqlite database in _dbPath on programm startup, if not -> creates the database and a table LOGINS + public static void Init() { using var connection = new SqliteConnection($"Data Source={_dbPath}"); connection.Open(); @@ -13,7 +18,7 @@ public class SQLite var command = connection.CreateCommand(); command.CommandText = @" - CREATE TABLE IF NOT EXISTS logins ( + CREATE TABLE IF NOT EXISTS LOGINS ( loginname STRING PRIMARY KEY NOT NULL, loginpass STRING NOT NULL, loginemail STRING NOT NULL @@ -21,4 +26,25 @@ public class SQLite "; command.ExecuteNonQuery(); } + + //saves a new user to the table LOGINS ----- no input sanitization yet + public static void SaveUser(NewUser user) + { + using var connection = new SqliteConnection($"Data Source={_dbPath}"); + connection.Open(); + connection.Open(); + using (var command = connection.CreateCommand()) + { + command.Parameters.AddWithValue("@loginname", user.NewLoginName); + command.Parameters.AddWithValue("@loginpass", user.NewMasterPassword); + command.Parameters.AddWithValue("@loginemail", user.NewEmail); + + command.CommandText = + @" + INSERT INTO LOGINS (loginname, loginpass, loginemail) + VALUES (@loginname, @loginpass, @loginemail);"; + command.ExecuteNonQuery(); + + } + } } \ No newline at end of file diff --git a/Project.Persistence/SaveNewLogin.cs b/Project.Persistence/SaveNewLogin.cs index 6f85ba8..1ab1566 100644 --- a/Project.Persistence/SaveNewLogin.cs +++ b/Project.Persistence/SaveNewLogin.cs @@ -1,9 +1,11 @@ using Project.Model; + +using Microsoft.Data.Sqlite; namespace Project.Persistence; public static class SaveNewLogin { - public static void SaveUser(string username, string password, string email) + public static void SaveUser(NewUser user) { } diff --git a/Project.View/App.axaml.cs b/Project.View/App.axaml.cs index dcfe882..9ce8a59 100644 --- a/Project.View/App.axaml.cs +++ b/Project.View/App.axaml.cs @@ -18,8 +18,7 @@ public partial class App : Application { desktop.MainWindow = new MainWindow(); // Initialising the database that contains all the logins - var initdb = new SQLite(); - initdb.Init(); + SQLite.Init(); } base.OnFrameworkInitializationCompleted(); diff --git a/Project.View/NewLogin/NewLogin.axaml.cs b/Project.View/NewLogin/NewLogin.axaml.cs index ccb4af5..5943c38 100644 --- a/Project.View/NewLogin/NewLogin.axaml.cs +++ b/Project.View/NewLogin/NewLogin.axaml.cs @@ -27,13 +27,8 @@ public partial class NewLogin : Window string password = NewLoginPasswordBox.Text?.Trim(); string email = NewLoginEmailBox.Text?.Trim(); - //var user = new CreateLogin(username, password, email); - //SaveNewLogin.SaveUser(user); - - - NewLoginUsernameBox.Text = ""; - NewLoginPasswordBox.Text = ""; - NewLoginEmailBox.Text = ""; + var user = new Model.NewUser(username, password, email); + SQLite.SaveUser(user); Close();