Fixed some Server bugs where the Employee is not send as full object

This commit is contained in:
Tim G. | SnapixLP 2025-06-06 00:00:46 +02:00
parent c0462e01b1
commit 830c94d5e4
3 changed files with 64 additions and 44 deletions

View File

@ -1,45 +1,57 @@
namespace Library; using System.Text.Json;
using System.Text.Json.Serialization;
namespace Library;
public class Employee public class Employee
{ {
private string _id; public string Id { get; set; }
private string _code; public string Code { get; set; }
private string _surname; public string Surname { get; set; }
private string _forename; public string Forename { get; set; }
private string _email; public string Email { get; set; }
private string _phone; public string Phone { get; set; }
private string _street; public string Street { get; set; }
private string _city; public string City { get; set; }
private string _postcode; public string Postcode { get; set; }
private string _country; public string Country { get; set; }
private string _department; public string Department { get; set; }
private string _position; public string Position { get; set; }
private EmployeeState _employeeState; public EmployeeState EmployeeState { get; set; }
public Employee(string code) 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 /// <summary>
/// Serialisiert dieses Employee-Objekt in einen JSON-String (nur öffentliche Eigenschaften).
/// </summary>
public string ToJson()
{ {
get => _id; var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
return JsonSerializer.Serialize(this, options);
} }
public string Code /// <summary>
/// Deserialisiert aus dem JSON-String ein neues Employee-Objekt (öffentliche Eigenschaften).
/// </summary>
public static Employee FromJson(string json)
{ {
get => _code; var options = new JsonSerializerOptions
} {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
public string Surname };
{ return JsonSerializer.Deserialize<Employee>(json, options)
get => _surname; ?? throw new InvalidOperationException("Deserialization to Employee failed.");
set => _surname = value;
}
public string Forename
{
get => _forename;
set => _forename = value;
} }
} }

View File

@ -117,7 +117,7 @@ public class Server
public string Response { get; set; } public string Response { get; set; }
} }
public Task<Employee?> GetEmployee(int? id = null, string code = null) public Task<Employee> GetEmployee(int? id = null, string code = null)
{ {
if (id == null && 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."); throw new ArgumentException("Only one of id or code should be provided.");
} }
Employee? employee = null;
string commandResult; string commandResult;
// Implement logic to get a worker by ID or code // 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 // Deserialize the command result to an Employee object
try try
{ {
employee = JsonSerializer.Deserialize<Employee>(commandResult); return Task.FromResult(Employee.FromJson(commandResult));
return Task.FromResult(employee);
} }
catch (JsonException) catch (JsonException)
{ {
Console.WriteLine("Failed to deserialize the command result."); Console.WriteLine("Failed to deserialize the command result.");
} }
return Task.FromException<Employee?>(new Exception("Failed to get worker.")); return Task.FromException<Employee>(new Exception("Failed to get worker."));
} }
public Task<string> Login(string username, string password) public Task<string> Login(string username, string password)
@ -182,7 +179,7 @@ public class Server
var commandResult = ExecuteCommandAsync("getSelfUser").Result; var commandResult = ExecuteCommandAsync("getSelfUser").Result;
try try
{ {
var employee = JsonSerializer.Deserialize<Employee>(commandResult); var employee = Employee.FromJson(commandResult);
if (employee != null) if (employee != null)
{ {
return Task.FromResult(employee); return Task.FromResult(employee);

View File

@ -72,9 +72,7 @@ public class CommandLibrary
if (socket.LoggedInClients.TryGetValue(client, out var username)) if (socket.LoggedInClients.TryGetValue(client, out var username))
{ {
Employee returnEmployee = new Employee(username); return GenerateTestEmployee().ToJson();
string jsonEmployee = JsonSerializer.Serialize(returnEmployee);
return jsonEmployee;
} }
throw new CommandException("User not logged in."); throw new CommandException("User not logged in.");
@ -89,5 +87,18 @@ public class CommandLibrary
$"not implemented yet, args: {string.Join(", ", args)}"; $"not implemented yet, args: {string.Join(", ", args)}";
#endregion #endregion
#region DEV-TEST
private static Employee GenerateTestEmployee()
{
return new Employee(
code: "TEST",
surname: "Musermann",
forename: "Max",
email: "max.mustermann@company.de"
);
}
#endregion
} }