mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-10-14 10:04:52 +02:00
fixed an error where get command cant serlialize employee
This commit is contained in:
@@ -344,8 +344,25 @@ public class CommandLibrary
|
||||
throw new CommandException("Employee not found.");
|
||||
|
||||
// Return the first (and expected single) matching employee as JSON object
|
||||
var firstEmp = dataArray[0];
|
||||
return firstEmp.GetRawText();
|
||||
var firstRow = dataArray[0];
|
||||
// 5) Deserialize it into an Employee object.
|
||||
// This requires that Employee.Id is an int (matching the DB schema),
|
||||
// or you map fields manually if Id remains a string.
|
||||
Employee self;
|
||||
try
|
||||
{
|
||||
string employeeJson = firstRow.GetRawText();
|
||||
self = JsonSerializer.Deserialize<Employee>(employeeJson)
|
||||
?? throw new InvalidOperationException("Deserialized Employee was null.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Program.messageSender.Error($"[GetSelfUser] Failed to deserialize Employee: {ex.Message}");
|
||||
throw new CommandException("Internal error while parsing user data.");
|
||||
}
|
||||
|
||||
// 6) Return the Employee’s JSON via the ToJson() helper you already wrote:
|
||||
return self.ToJson();
|
||||
}
|
||||
|
||||
[Command("clockHistory")]
|
||||
@@ -396,58 +413,58 @@ public class CommandLibrary
|
||||
|
||||
[Command("fullClockHistory")]
|
||||
public static string FullClockHistoryCommand(string[] args, TcpClient? client, TcpServer? socket)
|
||||
{
|
||||
// 1) Determine maxHistoryLength (default = 50)
|
||||
int maxHistoryLength;
|
||||
if (args.Length >= 1 && int.TryParse(args[0], out var parsed))
|
||||
{
|
||||
maxHistoryLength = parsed;
|
||||
}
|
||||
else
|
||||
{
|
||||
maxHistoryLength = 50;
|
||||
}
|
||||
{
|
||||
// 1) Determine maxHistoryLength (default = 50)
|
||||
int maxHistoryLength;
|
||||
if (args.Length >= 1 && int.TryParse(args[0], out var parsed))
|
||||
{
|
||||
maxHistoryLength = parsed;
|
||||
}
|
||||
else
|
||||
{
|
||||
maxHistoryLength = 50;
|
||||
}
|
||||
|
||||
// 2) Verify client/socket
|
||||
if (client == null || socket == null)
|
||||
throw new CommandException("No client connection detected.");
|
||||
// 2) Verify client/socket
|
||||
if (client == null || socket == null)
|
||||
throw new CommandException("No client connection detected.");
|
||||
|
||||
if (!socket.LoggedInClients.TryGetValue(client, out var employeeCode))
|
||||
throw new CommandException("User not logged in.");
|
||||
if (!socket.LoggedInClients.TryGetValue(client, out var employeeCode))
|
||||
throw new CommandException("User not logged in.");
|
||||
|
||||
// 3) Look up the employee to get its Id
|
||||
var empParams = new Dictionary<string, object> { { "Code", employeeCode } };
|
||||
string empResultJson = Program.mySql.Get("employees", empParams);
|
||||
// 3) Look up the employee to get its Id
|
||||
var empParams = new Dictionary<string, object> { { "Code", employeeCode } };
|
||||
string empResultJson = Program.mySql.Get("employees", empParams);
|
||||
|
||||
using var empDoc = JsonDocument.Parse(empResultJson);
|
||||
bool empError = empDoc.RootElement.GetProperty("error").GetBoolean();
|
||||
if (empError)
|
||||
{
|
||||
string dbMsg = empDoc.RootElement.GetProperty("data").GetString() ?? "Unknown DB error";
|
||||
Program.messageSender.Error($"[FullClockHistory] DB error when looking up employee: {dbMsg}");
|
||||
throw new CommandException("Internal error while fetching user data.");
|
||||
}
|
||||
using var empDoc = JsonDocument.Parse(empResultJson);
|
||||
bool empError = empDoc.RootElement.GetProperty("error").GetBoolean();
|
||||
if (empError)
|
||||
{
|
||||
string dbMsg = empDoc.RootElement.GetProperty("data").GetString() ?? "Unknown DB error";
|
||||
Program.messageSender.Error($"[FullClockHistory] DB error when looking up employee: {dbMsg}");
|
||||
throw new CommandException("Internal error while fetching user data.");
|
||||
}
|
||||
|
||||
var empArray = empDoc.RootElement.GetProperty("data");
|
||||
if (empArray.GetArrayLength() == 0)
|
||||
throw new CommandException("Logged-in user not found in database.");
|
||||
var empArray = empDoc.RootElement.GetProperty("data");
|
||||
if (empArray.GetArrayLength() == 0)
|
||||
throw new CommandException("Logged-in user not found in database.");
|
||||
|
||||
int employeeId = empArray[0].GetProperty("Id").GetInt32();
|
||||
int employeeId = empArray[0].GetProperty("Id").GetInt32();
|
||||
|
||||
// 4) Query the history table for this employeeId, ordered by ChangeTime DESC, limited to maxHistoryLength
|
||||
string sql = @"
|
||||
SELECT `EmployeeId`, `NewState`, `ChangeTime`
|
||||
FROM `employee_state_history`
|
||||
WHERE `EmployeeId` = ?
|
||||
ORDER BY `ChangeTime` DESC
|
||||
LIMIT ?
|
||||
";
|
||||
var values = new List<object> { employeeId, maxHistoryLength };
|
||||
string historyJson = Program.mySql.Query(sql, null, values);
|
||||
// 4) Query the history table for this employeeId, ordered by ChangeTime DESC, limited to maxHistoryLength
|
||||
string sql = @"
|
||||
SELECT `EmployeeId`, `NewState`, `ChangeTime`
|
||||
FROM `employee_state_history`
|
||||
ORDER BY `ChangeTime` DESC
|
||||
LIMIT ?
|
||||
";
|
||||
var values = new List<object> {maxHistoryLength };
|
||||
// Pass an empty string ("") for 'types' so that parameters get bound correctly
|
||||
string historyJson = Program.mySql.Query(sql, /*types:*/ "", /*values:*/ values);
|
||||
|
||||
// 5) Return the JSON from Query (the client will receive {"error":…, "data":[…]} )
|
||||
return historyJson;
|
||||
}
|
||||
// 5) Return the JSON from Query (the client will receive {"error":…, "data":[…]} )
|
||||
return historyJson;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
Reference in New Issue
Block a user