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
{
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 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(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;
}
public string Surname
var options = new JsonSerializerOptions
{
get => _surname;
set => _surname = value;
}
public string Forename
{
get => _forename;
set => _forename = value;
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
return JsonSerializer.Deserialize<Employee>(json, options)
?? throw new InvalidOperationException("Deserialization to Employee failed.");
}
}

View File

@ -117,7 +117,7 @@ public class Server
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)
{
@ -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<Employee>(commandResult);
return Task.FromResult(employee);
return Task.FromResult(Employee.FromJson(commandResult));
}
catch (JsonException)
{
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)
@ -182,7 +179,7 @@ public class Server
var commandResult = ExecuteCommandAsync("getSelfUser").Result;
try
{
var employee = JsonSerializer.Deserialize<Employee>(commandResult);
var employee = Employee.FromJson(commandResult);
if (employee != null)
{
return Task.FromResult(employee);

View File

@ -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.");
@ -90,4 +88,17 @@ public class CommandLibrary
#endregion
#region DEV-TEST
private static Employee GenerateTestEmployee()
{
return new Employee(
code: "TEST",
surname: "Musermann",
forename: "Max",
email: "max.mustermann@company.de"
);
}
#endregion
}