From 6fb3ffb680233b5d814224705a2e5f03ea306b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miskolczi=20Rich=C3=A1rd?= Date: Sat, 27 Sep 2025 23:01:24 +0200 Subject: [PATCH] Task done --- Feladat1/Program.cs | 4 +- Feladat1/UI/MainWindow/MainWindow.cs | 51 ++++++++++++++++++++++-- Feladat1/UI/MainWindow/MainWindow.ui.xml | 32 +++++++++++++-- Logic/IUserService.cs | 2 +- Logic/UserService.cs | 10 ++++- Models/User.cs | 7 +--- Repository/IRepository.cs | 2 +- Repository/JsonRepository.cs | 6 ++- 8 files changed, 95 insertions(+), 19 deletions(-) diff --git a/Feladat1/Program.cs b/Feladat1/Program.cs index d615d50..a7a5cd0 100644 --- a/Feladat1/Program.cs +++ b/Feladat1/Program.cs @@ -9,10 +9,12 @@ class Program { static int Main() { + var serviceProvider = SetupServices(); var application = Adw.Application.New("org.piper.feladat1", Gio.ApplicationFlags.FlagsNone); application.OnActivate += (sender, args) => { - var window = new MainWindow().Window; + var userService = serviceProvider.GetService(); + var window = new MainWindow(userService).Window; window.Application = (Adw.Application)sender; window.Show(); }; diff --git a/Feladat1/UI/MainWindow/MainWindow.cs b/Feladat1/UI/MainWindow/MainWindow.cs index dbb90d1..826de12 100644 --- a/Feladat1/UI/MainWindow/MainWindow.cs +++ b/Feladat1/UI/MainWindow/MainWindow.cs @@ -1,5 +1,7 @@ using Adw; +using GLib; using Logic; +using Models; namespace Feladat1.UI.MainWindow; @@ -22,8 +24,18 @@ public class MainWindow private PasswordEntryRow passwordField; private const string passwordFieldId = "password"; - public MainWindow() + private EntryRow emailLabel; + private const string emailLabelId = "emailLabel"; + + private EntryRow usernameLabel; + private const string usernameLabelId = "usernameLabel"; + + private PasswordEntryRow passwordLabel; + private const string passwordLabelId = "passwordLabel"; + + public MainWindow(IUserService userService) { + this.userService = userService; var builder = new Gtk.Builder("Feladat1.UI.MainWindow.MainWindow.ui.xml"); Window = builder.GetObject(windowId) as Window; @@ -58,6 +70,24 @@ public class MainWindow { throw new NullReferenceException(passwordFieldId); } + + emailLabel = builder.GetObject(emailLabelId) as EntryRow; + if (emailLabel == null) + { + throw new NullReferenceException(emailLabelId); + } + + usernameLabel = builder.GetObject(usernameLabelId) as EntryRow; + if (usernameLabel == null) + { + throw new NullReferenceException(usernameLabelId); + } + + passwordLabel = builder.GetObject(passwordLabelId) as PasswordEntryRow; + if (passwordLabel == null) + { + throw new NullReferenceException(passwordLabelId); + } } catch (NullReferenceException e) { @@ -68,7 +98,7 @@ public class MainWindow private bool ValidateFields() { - if (userNameField.Text_ != null && emailField.Text_ != null && passwordField.Text_ != null) + if (userNameField.Text_ != string.Empty && emailField.Text_ != string.Empty && passwordField.Text_ != string.Empty) { return true; } @@ -79,7 +109,22 @@ public class MainWindow { if (ValidateFields()) { - userService.Create(userNameField.GetText(), emailField.GetText(), passwordField.GetText()); + int returnCode = userService.Create(userNameField.GetText(), emailField.GetText(), passwordField.GetText()); + if (returnCode == 0) + { + ToastOverlay overlay = new ToastOverlay(); + overlay.SetParent(Window); + + Toast toast = new Toast(); + toast.SetButtonLabel("New user exported successfully"); + + overlay.AddToast(toast); + + User user = userService.Read(); + emailLabel.Text_ = user.Email; + usernameLabel.Text_ = user.Username; + passwordLabel.Text_ = user.Password; + } } } } diff --git a/Feladat1/UI/MainWindow/MainWindow.ui.xml b/Feladat1/UI/MainWindow/MainWindow.ui.xml index c0d8105..19a24f0 100644 --- a/Feladat1/UI/MainWindow/MainWindow.ui.xml +++ b/Feladat1/UI/MainWindow/MainWindow.ui.xml @@ -3,8 +3,8 @@ 300 300 - 500 - 600 + 400 + 400 @@ -23,6 +23,7 @@ save + document-save-as-symbolic Save @@ -63,9 +64,32 @@ load Load + document-open-symbolic - - Load + + + + + + Email + False + + + + + Felhasználónév + False + False + + + + + Jelszó + False + + + + diff --git a/Logic/IUserService.cs b/Logic/IUserService.cs index 0e02c6a..705fd0d 100644 --- a/Logic/IUserService.cs +++ b/Logic/IUserService.cs @@ -4,6 +4,6 @@ namespace Logic; public interface IUserService { - public void Create(string username, string email, string password); + public int Create(string username, string email, string password); public User? Read(); } diff --git a/Logic/UserService.cs b/Logic/UserService.cs index ebca3ba..9e6f48a 100644 --- a/Logic/UserService.cs +++ b/Logic/UserService.cs @@ -6,13 +6,19 @@ namespace Logic; public class UserService : IUserService { private IRepository repository; + + public UserService(IRepository repository) + { + this.repository = repository; + } + public User? Read() { return repository.Read(); } - public void Create(string username, string email, string password) + public int Create(string username, string email, string password) { - repository.Write(new User(username, email, password)); + return repository.Write(new User(username, email, password)); } } diff --git a/Models/User.cs b/Models/User.cs index d866cf3..0b3841c 100644 --- a/Models/User.cs +++ b/Models/User.cs @@ -4,12 +4,7 @@ public class User { public string Username { get; set; } public string Email { get; } - - private string password; - public string Password - { - set => password = value; - } + public string Password { get; set; } public User(string username, string email, string password) { diff --git a/Repository/IRepository.cs b/Repository/IRepository.cs index 887a014..80ae19d 100644 --- a/Repository/IRepository.cs +++ b/Repository/IRepository.cs @@ -5,5 +5,5 @@ namespace Repository; public interface IRepository { public User? Read(); - public void Write(User item); + public int Write(User item); } diff --git a/Repository/JsonRepository.cs b/Repository/JsonRepository.cs index e06b7a2..46c7b32 100644 --- a/Repository/JsonRepository.cs +++ b/Repository/JsonRepository.cs @@ -32,24 +32,28 @@ public class JsonRepository : IRepository return null; } - public void Write(User item) + public int Write(User item) { try { string json = JsonSerializer.Serialize(item); File.WriteAllText(_fileName, json); + return 0; } catch (JsonException e) { WriteToStdErr($"JSON error: {e.Message}"); + return e.GetHashCode(); } catch (IOException e) { WriteToStdErr($"File I/O error: {e.Message}"); + return e.GetHashCode(); } catch (Exception e) { WriteToStdErr($"Unexpected error: {e.Message}"); + return e.GetHashCode(); } }