mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-06-21 08:13:17 +02:00
Fixed some Server bugs where the Employee is not send as full object
This commit is contained in:
parent
c0462e01b1
commit
830c94d5e4
@ -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
|
||||||
}
|
|
||||||
|
|
||||||
public string Surname
|
|
||||||
{
|
{
|
||||||
get => _surname;
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||||
set => _surname = value;
|
};
|
||||||
}
|
return JsonSerializer.Deserialize<Employee>(json, options)
|
||||||
|
?? throw new InvalidOperationException("Deserialization to Employee failed.");
|
||||||
public string Forename
|
|
||||||
{
|
|
||||||
get => _forename;
|
|
||||||
set => _forename = value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
|
@ -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.");
|
||||||
@ -90,4 +88,17 @@ public class CommandLibrary
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region DEV-TEST
|
||||||
|
|
||||||
|
private static Employee GenerateTestEmployee()
|
||||||
|
{
|
||||||
|
return new Employee(
|
||||||
|
code: "TEST",
|
||||||
|
surname: "Musermann",
|
||||||
|
forename: "Max",
|
||||||
|
email: "max.mustermann@company.de"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user