dynamically creating the buttons now works
This commit is contained in:
parent
7ef66a0b3d
commit
a0da2394b0
@ -11,14 +11,21 @@ public partial class AppController
|
||||
var user = new NewUser(username, hashedPassword, email);
|
||||
SQLite.SaveUser(user);
|
||||
}
|
||||
//the currently logged in user
|
||||
public static string _currentUser;
|
||||
|
||||
public static void SetCurrentUser(string currentUser)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
//save new entry - hashes the password and gives it to persistence
|
||||
public static void NewEntrySave(string name, string usernameurl, string email, string password, string note)
|
||||
public static void NewEntrySave(string name, string mailusername, string url, string password, string note)
|
||||
{
|
||||
string hashedPassword = ShaHash.HashPassword(password);
|
||||
string ownerUsername = _currentUser;
|
||||
SQLite.SaveEntry(name, hashedPassword, email, note, ownerUsername);
|
||||
//string name, string pass, string mailUsername, string? note, string ownerUsername
|
||||
SQLite.SaveEntry(name, hashedPassword, mailusername, url, note, ownerUsername);
|
||||
//string name, string pass, string mailUsername, url, string? note, string ownerUsername
|
||||
}
|
||||
|
||||
//compares the login and checks if the password is correct.
|
||||
@ -30,13 +37,8 @@ public partial class AppController
|
||||
return hashedPassword == savedPassword;
|
||||
}
|
||||
|
||||
//the currently logged in user
|
||||
private static string _currentUser;
|
||||
|
||||
public static void SetCurrentUser(string currentUser)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
namespace Project.Model;
|
||||
|
||||
public class Entry
|
||||
{
|
||||
public string? Name;
|
||||
public string? Username;
|
||||
public string? Password;
|
||||
public string? Url;
|
||||
public string? Note;
|
||||
}
|
||||
14
Project.Model/SavedEntries.cs
Normal file
14
Project.Model/SavedEntries.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace Project.Model;
|
||||
|
||||
public class SavedEntries
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Pass { get; set; }
|
||||
public string MailUsername { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string Note { get; set; }
|
||||
public string Owner { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -34,6 +34,7 @@ public partial class SQLite
|
||||
name STRING PRIMARY KEY NOT NULL,
|
||||
pass STRING NOT NULL,
|
||||
mail_username STRING NOT NULL,
|
||||
url STRING NOT NULL,
|
||||
note STRING,
|
||||
owner STRING NOT NULL,
|
||||
FOREIGN KEY(owner) REFERENCES LOGINS(loginname)
|
||||
|
||||
@ -5,7 +5,8 @@ namespace Project.Persistence;
|
||||
public partial class SQLite
|
||||
{
|
||||
//Method to save a new entry to SAVED_LOGINS
|
||||
public static void SaveEntry(string name, string pass, string mailUsername, string? note, string ownerUsername)
|
||||
public static void SaveEntry(string name, string pass, string mailUsername, string url, string? note,
|
||||
string ownerUsername)
|
||||
{
|
||||
using var connection = new SqliteConnection($"Data Source={_dbPath}");
|
||||
connection.Open();
|
||||
@ -14,15 +15,36 @@ public partial class SQLite
|
||||
command.Parameters.AddWithValue("@name", name);
|
||||
command.Parameters.AddWithValue("@pass", pass);
|
||||
command.Parameters.AddWithValue("@mail_username", mailUsername);
|
||||
command.Parameters.AddWithValue("@url", url);
|
||||
command.Parameters.AddWithValue("@note", note ?? "");
|
||||
command.Parameters.AddWithValue("@owner", ownerUsername);
|
||||
|
||||
|
||||
command.CommandText =
|
||||
@"
|
||||
INSERT INTO SAVED_LOGINS (name, pass, mail_username, note, owner)
|
||||
VALUES (@name, @pass, @mail_username, @note, @owner);";
|
||||
INSERT INTO SAVED_LOGINS (name, pass, mail_username, url, note, owner)
|
||||
VALUES (@name, @pass, @mail_username,@url, @note, @owner);";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
//get all the entries from the saved_logins table that are for the current user
|
||||
/*
|
||||
public static string? GetEntries(string username)
|
||||
{
|
||||
using var connection = new SqliteConnection($"Data Source={_dbPath}");
|
||||
connection.Open();
|
||||
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.Parameters.AddWithValue("@currentuser", username);
|
||||
command.CommandText =
|
||||
@"SELECT name, pass, mail_username,url, note, owner
|
||||
FROM SAVED_LOGINS
|
||||
WHERE owner = @currentuser
|
||||
";
|
||||
var result = command.ExecuteScalar();
|
||||
return result?.ToString();
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
39
Project.Persistence/SQLite4.cs
Normal file
39
Project.Persistence/SQLite4.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Project.Model;
|
||||
namespace Project.Persistence;
|
||||
|
||||
//here we get all the current user's entries and put them in a list
|
||||
public partial class SQLite
|
||||
{
|
||||
public static List<Model.SavedEntries> GetEntries(string username)
|
||||
{
|
||||
var entries = new List<SavedEntries>();
|
||||
using var connection = new SqliteConnection($"Data Source={_dbPath}");
|
||||
connection.Open();
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.Parameters.AddWithValue("@owner", username);
|
||||
command.CommandText = @"
|
||||
SELECT name, pass, mail_username, url, note, owner
|
||||
FROM SAVED_LOGINS
|
||||
WHERE owner = @owner;";
|
||||
|
||||
using var reader = command.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
entries.Add(new SavedEntries()
|
||||
{
|
||||
Name = reader.GetString(0),
|
||||
Pass = reader.GetString(1),
|
||||
MailUsername = reader.GetString(2),
|
||||
Url = reader.GetString(3),
|
||||
Note = reader.IsDBNull(4) ? "" : reader.GetString(4),
|
||||
Owner = reader.GetString(5)
|
||||
});
|
||||
}
|
||||
|
||||
return entries;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -3,6 +3,7 @@ using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using Project.Controller;
|
||||
using Project.View;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Project.View;
|
||||
|
||||
@ -24,12 +25,14 @@ public partial class LoginPage : UserControl
|
||||
string? loginName = LoginNameBox.Text ?? string.Empty;
|
||||
string? loginPassword = LoginPasswordBox.Text ?? string.Empty;
|
||||
bool allowLogin = AppController.CompareLogin(loginName, loginPassword);
|
||||
var newMenuView = new MenuView();
|
||||
var newPopUp = new PopUp();
|
||||
if (allowLogin)
|
||||
{
|
||||
//show menuview
|
||||
AppController.SetCurrentUser(loginName);
|
||||
//await Task.Delay(300); // 1 second delay until menu gets shown
|
||||
|
||||
var newMenuView = new MenuView();
|
||||
newMenuView.Show();
|
||||
}
|
||||
else
|
||||
|
||||
@ -13,23 +13,8 @@
|
||||
Margin="0,0,0,40"
|
||||
VerticalScrollBarVisibility="Auto">
|
||||
|
||||
<StackPanel>
|
||||
<Button
|
||||
Background="Pink"
|
||||
BorderBrush="Black"
|
||||
BorderThickness="1"
|
||||
Name="btn1"
|
||||
Width="200">
|
||||
BeispielButton
|
||||
</Button>
|
||||
<Button
|
||||
Background="Pink"
|
||||
BorderBrush="Black"
|
||||
BorderThickness="1"
|
||||
Name="btn2"
|
||||
Width="200">
|
||||
BeispielButton2
|
||||
</Button>
|
||||
<StackPanel x:Name="entriesContainer">
|
||||
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
<Button
|
||||
|
||||
@ -1,16 +1,21 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using Project.Controller;
|
||||
using Project.Persistence;
|
||||
|
||||
namespace Project.View;
|
||||
|
||||
public partial class MenuView : Window
|
||||
{
|
||||
readonly AppController? _controller;
|
||||
|
||||
public MenuView()
|
||||
{
|
||||
InitializeComponent();
|
||||
CreateEntryButtons();
|
||||
|
||||
}
|
||||
|
||||
public MenuView(AppController controller)
|
||||
@ -18,7 +23,7 @@ public partial class MenuView : Window
|
||||
InitializeComponent();
|
||||
_controller = controller;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private async void NewEntryButton_OnClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
{
|
||||
@ -26,4 +31,30 @@ public partial class MenuView : Window
|
||||
newEntryWindow.Show();
|
||||
}
|
||||
|
||||
private void CreateEntryButtons()
|
||||
{
|
||||
// dynamically creating buttons for each entry
|
||||
entriesContainer.Children.Clear(); // Clear existing buttons
|
||||
|
||||
var entries = SQLite.GetEntries(AppController._currentUser);
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
var button = new Button
|
||||
{
|
||||
Content = entry.Name, // Display the entry name on the button
|
||||
Margin = new Thickness(5),
|
||||
Padding = new Thickness(10),
|
||||
Width = 200
|
||||
};
|
||||
// Add click handler for the button
|
||||
//button.Click += (sender, args) => ShowEntryDetails(entry);
|
||||
// Add the button to your container
|
||||
entriesContainer.Children.Add(button);
|
||||
}
|
||||
}
|
||||
public void AfterSavingNewEntry()
|
||||
{
|
||||
CreateEntryButtons(); // Refresh the buttons
|
||||
}
|
||||
|
||||
}
|
||||
@ -23,8 +23,8 @@
|
||||
FontSize="20"
|
||||
HorizontalAlignment="Left"
|
||||
Margin="23"
|
||||
Text="Email:" />
|
||||
<TextBox TextWrapping="Wrap" Watermark="E-Mail" x:Name="EmailBox"/>
|
||||
Text="URL:" />
|
||||
<TextBox TextWrapping="Wrap" Watermark="E-Mail" x:Name="URLBox"/>
|
||||
<TextBlock
|
||||
FontSize="20"
|
||||
HorizontalAlignment="Left"
|
||||
|
||||
@ -25,12 +25,14 @@ public partial class NewEntry : Window
|
||||
|
||||
private void NewEntry_Save_Click(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
string name = NameBox.Text;
|
||||
string username = UsernameMailBox.Text;
|
||||
string email = EmailBox.Text;
|
||||
string pass = PassBox.Text;
|
||||
string note = NoteBox.Text;
|
||||
AppController.NewEntrySave(name, username, email, pass, note);
|
||||
string? name = NameBox.Text;
|
||||
string? username = UsernameMailBox.Text;
|
||||
string? url = URLBox.Text;
|
||||
string? pass = PassBox.Text;
|
||||
string? note = NoteBox.Text;
|
||||
AppController.NewEntrySave(name, username, url, pass, note);
|
||||
var newMenuView = new MenuView();
|
||||
newMenuView.AfterSavingNewEntry();
|
||||
Close();
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user