From 6c5579809de07a4d6599afa7ac093a1c357fc3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miskolczi=20Rich=C3=A1rd?= Date: Fri, 26 Sep 2025 17:32:32 +0200 Subject: [PATCH] First commit --- .gitignore | 3 + Feladat1/Feladat1.csproj | 24 +++++++ Feladat1/Program.cs | 30 +++++++++ Feladat1/UI/MainWindow/MainWindow.cs | 85 ++++++++++++++++++++++++ Feladat1/UI/MainWindow/MainWindow.ui.xml | 83 +++++++++++++++++++++++ Logic/IUserService.cs | 9 +++ Logic/Logic.csproj | 13 ++++ Logic/UserService.cs | 18 +++++ Models/Models.csproj | 9 +++ Models/User.cs | 20 ++++++ PiperFeladat.sln | 40 +++++++++++ Repository/IRepository.cs | 9 +++ Repository/JsonRepository.cs | 61 +++++++++++++++++ Repository/Repository.csproj | 13 ++++ 14 files changed, 417 insertions(+) create mode 100644 .gitignore create mode 100644 Feladat1/Feladat1.csproj create mode 100644 Feladat1/Program.cs create mode 100644 Feladat1/UI/MainWindow/MainWindow.cs create mode 100644 Feladat1/UI/MainWindow/MainWindow.ui.xml create mode 100644 Logic/IUserService.cs create mode 100644 Logic/Logic.csproj create mode 100644 Logic/UserService.cs create mode 100644 Models/Models.csproj create mode 100644 Models/User.cs create mode 100644 PiperFeladat.sln create mode 100644 Repository/IRepository.cs create mode 100644 Repository/JsonRepository.cs create mode 100644 Repository/Repository.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..52746af --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/bin/ +**/obj/ +**/.vscode/ diff --git a/Feladat1/Feladat1.csproj b/Feladat1/Feladat1.csproj new file mode 100644 index 0000000..9c68b6e --- /dev/null +++ b/Feladat1/Feladat1.csproj @@ -0,0 +1,24 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + + + + + + + + + + + diff --git a/Feladat1/Program.cs b/Feladat1/Program.cs new file mode 100644 index 0000000..d615d50 --- /dev/null +++ b/Feladat1/Program.cs @@ -0,0 +1,30 @@ +using Feladat1.UI.MainWindow; +using Logic; +using Microsoft.Extensions.DependencyInjection; +using Repository; + +namespace Feladat1; + +class Program +{ + static int Main() + { + var application = Adw.Application.New("org.piper.feladat1", Gio.ApplicationFlags.FlagsNone); + application.OnActivate += (sender, args) => + { + var window = new MainWindow().Window; + window.Application = (Adw.Application)sender; + window.Show(); + }; + + return application.RunWithSynchronizationContext(null); + } + + private static ServiceProvider SetupServices() + { + var services = new ServiceCollection(); + services.AddSingleton(); + services.AddSingleton(); + return services.BuildServiceProvider(); + } +} \ No newline at end of file diff --git a/Feladat1/UI/MainWindow/MainWindow.cs b/Feladat1/UI/MainWindow/MainWindow.cs new file mode 100644 index 0000000..dbb90d1 --- /dev/null +++ b/Feladat1/UI/MainWindow/MainWindow.cs @@ -0,0 +1,85 @@ +using Adw; +using Logic; + +namespace Feladat1.UI.MainWindow; + +public class MainWindow +{ + private IUserService userService; + private const string windowId = "main_window"; + + public Window Window; + + private Gtk.Button saveBtn; + private const string saveBtnId = "save_btn"; + + private EntryRow emailField; + private const string emailFieldId = "email"; + + private EntryRow userNameField; + private const string userNameFieldId = "username"; + + private PasswordEntryRow passwordField; + private const string passwordFieldId = "password"; + + public MainWindow() + { + var builder = new Gtk.Builder("Feladat1.UI.MainWindow.MainWindow.ui.xml"); + + Window = builder.GetObject(windowId) as Window; + if (Window == null) + { + throw new Exception("Failed to load embedded resource MainWindow.ui.xml"); + } + + try + { + saveBtn = builder.GetObject(saveBtnId) as Gtk.Button; + if (saveBtn == null) + { + throw new NullReferenceException(saveBtnId); + } + saveBtn.OnClicked += (sender, args) => Save(); + + emailField = builder.GetObject(emailFieldId) as EntryRow; + if (emailField == null) + { + throw new NullReferenceException(emailFieldId); + } + + userNameField = builder.GetObject(userNameFieldId) as EntryRow; + if (userNameField == null) + { + throw new NullReferenceException(userNameFieldId); + } + + passwordField = builder.GetObject(passwordFieldId) as PasswordEntryRow; + if (passwordField == null) + { + throw new NullReferenceException(passwordFieldId); + } + } + catch (NullReferenceException e) + { + Console.WriteLine($"Failed to load UI element with ID: {e.Message}"); + Window.Close(); + } + } + + private bool ValidateFields() + { + if (userNameField.Text_ != null && emailField.Text_ != null && passwordField.Text_ != null) + { + return true; + } + return false; + } + + private void Save() + { + if (ValidateFields()) + { + userService.Create(userNameField.GetText(), emailField.GetText(), passwordField.GetText()); + } + } +} diff --git a/Feladat1/UI/MainWindow/MainWindow.ui.xml b/Feladat1/UI/MainWindow/MainWindow.ui.xml new file mode 100644 index 0000000..c0d8105 --- /dev/null +++ b/Feladat1/UI/MainWindow/MainWindow.ui.xml @@ -0,0 +1,83 @@ + + + + 300 + 300 + 500 + 600 + + + + + + + stack + wide + + + + + + + True + + + save + Save + + + + + + + Email + + + + + Felhasználónév + + + + + Jelszó + + + + + + + + Exportálás + center + center + + + + + + + + + load + Load + + + Load + + + + + + + + + stack + + + + + + \ No newline at end of file diff --git a/Logic/IUserService.cs b/Logic/IUserService.cs new file mode 100644 index 0000000..0e02c6a --- /dev/null +++ b/Logic/IUserService.cs @@ -0,0 +1,9 @@ +using Models; + +namespace Logic; + +public interface IUserService +{ + public void Create(string username, string email, string password); + public User? Read(); +} diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj new file mode 100644 index 0000000..8a3982d --- /dev/null +++ b/Logic/Logic.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/Logic/UserService.cs b/Logic/UserService.cs new file mode 100644 index 0000000..ebca3ba --- /dev/null +++ b/Logic/UserService.cs @@ -0,0 +1,18 @@ +using Models; +using Repository; + +namespace Logic; + +public class UserService : IUserService +{ + private IRepository repository; + public User? Read() + { + return repository.Read(); + } + + public void Create(string username, string email, string password) + { + repository.Write(new User(username, email, password)); + } +} diff --git a/Models/Models.csproj b/Models/Models.csproj new file mode 100644 index 0000000..125f4c9 --- /dev/null +++ b/Models/Models.csproj @@ -0,0 +1,9 @@ + + + + net9.0 + enable + enable + + + diff --git a/Models/User.cs b/Models/User.cs new file mode 100644 index 0000000..d866cf3 --- /dev/null +++ b/Models/User.cs @@ -0,0 +1,20 @@ +namespace Models; + +public class User +{ + public string Username { get; set; } + public string Email { get; } + + private string password; + public string Password + { + set => password = value; + } + + public User(string username, string email, string password) + { + Username = username; + Email = email; + Password = password; + } +} diff --git a/PiperFeladat.sln b/PiperFeladat.sln new file mode 100644 index 0000000..dcfe4b9 --- /dev/null +++ b/PiperFeladat.sln @@ -0,0 +1,40 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Feladat1", "Feladat1\Feladat1.csproj", "{F143EA5A-C9F3-4838-9805-ECC8D176DADF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logic", "Logic\Logic.csproj", "{F83A4E28-94CB-4E8D-ADE7-396B3A893115}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository", "Repository\Repository.csproj", "{42A51F92-4197-4070-BF2C-0036B636A71D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Models", "Models\Models.csproj", "{21C21D56-FD79-43F3-BAF3-BFB82CD323FD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F143EA5A-C9F3-4838-9805-ECC8D176DADF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F143EA5A-C9F3-4838-9805-ECC8D176DADF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F143EA5A-C9F3-4838-9805-ECC8D176DADF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F143EA5A-C9F3-4838-9805-ECC8D176DADF}.Release|Any CPU.Build.0 = Release|Any CPU + {F83A4E28-94CB-4E8D-ADE7-396B3A893115}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F83A4E28-94CB-4E8D-ADE7-396B3A893115}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F83A4E28-94CB-4E8D-ADE7-396B3A893115}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F83A4E28-94CB-4E8D-ADE7-396B3A893115}.Release|Any CPU.Build.0 = Release|Any CPU + {42A51F92-4197-4070-BF2C-0036B636A71D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {42A51F92-4197-4070-BF2C-0036B636A71D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {42A51F92-4197-4070-BF2C-0036B636A71D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {42A51F92-4197-4070-BF2C-0036B636A71D}.Release|Any CPU.Build.0 = Release|Any CPU + {21C21D56-FD79-43F3-BAF3-BFB82CD323FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {21C21D56-FD79-43F3-BAF3-BFB82CD323FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {21C21D56-FD79-43F3-BAF3-BFB82CD323FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {21C21D56-FD79-43F3-BAF3-BFB82CD323FD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Repository/IRepository.cs b/Repository/IRepository.cs new file mode 100644 index 0000000..887a014 --- /dev/null +++ b/Repository/IRepository.cs @@ -0,0 +1,9 @@ +using Models; + +namespace Repository; + +public interface IRepository +{ + public User? Read(); + public void Write(User item); +} diff --git a/Repository/JsonRepository.cs b/Repository/JsonRepository.cs new file mode 100644 index 0000000..e06b7a2 --- /dev/null +++ b/Repository/JsonRepository.cs @@ -0,0 +1,61 @@ +using System.Text.Json; +using Models; + +namespace Repository; + +public class JsonRepository : IRepository +{ + private const string _fileName = "data.json"; + + public User? Read() + { + if (File.Exists(_fileName)) + { + try + { + string json = File.ReadAllText(_fileName); + return JsonSerializer.Deserialize(json) ?? new User("fallback", "fallback", "fallback"); + } + catch (JsonException e) + { + WriteToStdErr($"JSON error: {e.Message}"); + } + catch (IOException e) + { + WriteToStdErr($"File I/O error: {e.Message}"); + } + catch (Exception e) + { + WriteToStdErr($"Unexpected error: {e.Message}"); + } + } + return null; + } + + public void Write(User item) + { + try + { + string json = JsonSerializer.Serialize(item); + File.WriteAllText(_fileName, json); + } + catch (JsonException e) + { + WriteToStdErr($"JSON error: {e.Message}"); + } + catch (IOException e) + { + WriteToStdErr($"File I/O error: {e.Message}"); + } + catch (Exception e) + { + WriteToStdErr($"Unexpected error: {e.Message}"); + } + } + + private void WriteToStdErr(string message) + { + using var sw = new StreamWriter(Console.OpenStandardError()); + sw.WriteLine(message); + } +} \ No newline at end of file diff --git a/Repository/Repository.csproj b/Repository/Repository.csproj new file mode 100644 index 0000000..31e38e2 --- /dev/null +++ b/Repository/Repository.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + +