From 830c94d5e4acbae31b121d9c0bbd69586dfb8782 Mon Sep 17 00:00:00 2001 From: "Tim G. | SnapixLP" Date: Fri, 6 Jun 2025 00:00:46 +0200 Subject: [PATCH] Fixed some Server bugs where the Employee is not send as full object --- Library/Employee.cs | 78 ++++++++++++++++++------------- Library/Server.cs | 11 ++--- Server/Commands/CommandLibrary.cs | 19 ++++++-- 3 files changed, 64 insertions(+), 44 deletions(-) diff --git a/Library/Employee.cs b/Library/Employee.cs index 0965297..4f63d11 100644 --- a/Library/Employee.cs +++ b/Library/Employee.cs @@ -1,45 +1,57 @@ -namespace Library; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Library; public class Employee { - private string _id; - private string _code; - private string _surname; - private string _forename; - private string _email; - private string _phone; - private string _street; - private string _city; - private string _postcode; - private string _country; - private string _department; - private string _position; - private EmployeeState _employeeState; - - public Employee(string code) + public string Id { get; set; } + public string Code { get; set; } + public string Surname { get; set; } + public string Forename { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + public string Street { get; set; } + public string City { get; set; } + public string Postcode { get; set; } + public string Country { get; set; } + public string Department { get; set; } + public string Position { get; set; } + public EmployeeState EmployeeState { get; set; } + + public Employee() { } + + public Employee(string code, string surname, string forename, string email) { - _code = code; + Id = Guid.NewGuid().ToString(); + Code = code; + Surname = surname; + Forename = forename; + Email = email; } - public string Id + /// + /// Serialisiert dieses Employee-Objekt in einen JSON-String (nur öffentliche Eigenschaften). + /// + public string ToJson() { - get => _id; + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + return JsonSerializer.Serialize(this, options); } - public string Code + /// + /// Deserialisiert aus dem JSON-String ein neues Employee-Objekt (öffentliche Eigenschaften). + /// + public static Employee FromJson(string json) { - get => _code; - } - - public string Surname - { - get => _surname; - set => _surname = value; - } - - public string Forename - { - get => _forename; - set => _forename = value; + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + return JsonSerializer.Deserialize(json, options) + ?? throw new InvalidOperationException("Deserialization to Employee failed."); } } \ No newline at end of file diff --git a/Library/Server.cs b/Library/Server.cs index f3ca0ed..bbb6d70 100644 --- a/Library/Server.cs +++ b/Library/Server.cs @@ -117,7 +117,7 @@ public class Server public string Response { get; set; } } - public Task GetEmployee(int? id = null, string code = null) + public Task GetEmployee(int? id = null, string code = null) { if (id == null && code == null) { @@ -128,8 +128,6 @@ public class Server { throw new ArgumentException("Only one of id or code should be provided."); } - - Employee? employee = null; string commandResult; // Implement logic to get a worker by ID or code @@ -141,15 +139,14 @@ public class Server // Deserialize the command result to an Employee object try { - employee = JsonSerializer.Deserialize(commandResult); - return Task.FromResult(employee); + return Task.FromResult(Employee.FromJson(commandResult)); } catch (JsonException) { Console.WriteLine("Failed to deserialize the command result."); } - return Task.FromException(new Exception("Failed to get worker.")); + return Task.FromException(new Exception("Failed to get worker.")); } public Task Login(string username, string password) @@ -182,7 +179,7 @@ public class Server var commandResult = ExecuteCommandAsync("getSelfUser").Result; try { - var employee = JsonSerializer.Deserialize(commandResult); + var employee = Employee.FromJson(commandResult); if (employee != null) { return Task.FromResult(employee); diff --git a/Server/Commands/CommandLibrary.cs b/Server/Commands/CommandLibrary.cs index 13e6853..d358e45 100644 --- a/Server/Commands/CommandLibrary.cs +++ b/Server/Commands/CommandLibrary.cs @@ -72,9 +72,7 @@ public class CommandLibrary if (socket.LoggedInClients.TryGetValue(client, out var username)) { - Employee returnEmployee = new Employee(username); - string jsonEmployee = JsonSerializer.Serialize(returnEmployee); - return jsonEmployee; + return GenerateTestEmployee().ToJson(); } throw new CommandException("User not logged in."); @@ -89,5 +87,18 @@ public class CommandLibrary $"not implemented yet, args: {string.Join(", ", args)}"; #endregion - + + #region DEV-TEST + + private static Employee GenerateTestEmployee() + { + return new Employee( + code: "TEST", + surname: "Musermann", + forename: "Max", + email: "max.mustermann@company.de" + ); + } + + #endregion } \ No newline at end of file