mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-06-21 00:03:18 +02:00
fixed an error where get command cant serlialize employee
This commit is contained in:
parent
8630c4c346
commit
78a7597689
@ -146,7 +146,8 @@ public class Server
|
|||||||
// Deserialize the command result to an Employee object
|
// Deserialize the command result to an Employee object
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return Task.FromResult(Employee.FromJson(commandResult));
|
var employee = Employee.FromJson(commandResult);
|
||||||
|
return Task.FromResult(employee);
|
||||||
}
|
}
|
||||||
catch (JsonException)
|
catch (JsonException)
|
||||||
{
|
{
|
||||||
@ -234,5 +235,17 @@ public class Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task<string> ClockHistory(string employeeCode)
|
||||||
|
{
|
||||||
|
var commandResult = ExecuteCommandAsync("fullClockHistory " + employeeCode).Result;
|
||||||
|
return Task.FromResult(commandResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<string> FullClockHistory(int limit = 50)
|
||||||
|
{
|
||||||
|
var commandResult = ExecuteCommandAsync("fullClockHistory " + limit).Result;
|
||||||
|
return Task.FromResult(commandResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -344,8 +344,25 @@ public class CommandLibrary
|
|||||||
throw new CommandException("Employee not found.");
|
throw new CommandException("Employee not found.");
|
||||||
|
|
||||||
// Return the first (and expected single) matching employee as JSON object
|
// Return the first (and expected single) matching employee as JSON object
|
||||||
var firstEmp = dataArray[0];
|
var firstRow = dataArray[0];
|
||||||
return firstEmp.GetRawText();
|
// 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")]
|
[Command("clockHistory")]
|
||||||
@ -396,7 +413,7 @@ public class CommandLibrary
|
|||||||
|
|
||||||
[Command("fullClockHistory")]
|
[Command("fullClockHistory")]
|
||||||
public static string FullClockHistoryCommand(string[] args, TcpClient? client, TcpServer? socket)
|
public static string FullClockHistoryCommand(string[] args, TcpClient? client, TcpServer? socket)
|
||||||
{
|
{
|
||||||
// 1) Determine maxHistoryLength (default = 50)
|
// 1) Determine maxHistoryLength (default = 50)
|
||||||
int maxHistoryLength;
|
int maxHistoryLength;
|
||||||
if (args.Length >= 1 && int.TryParse(args[0], out var parsed))
|
if (args.Length >= 1 && int.TryParse(args[0], out var parsed))
|
||||||
@ -438,16 +455,16 @@ public class CommandLibrary
|
|||||||
string sql = @"
|
string sql = @"
|
||||||
SELECT `EmployeeId`, `NewState`, `ChangeTime`
|
SELECT `EmployeeId`, `NewState`, `ChangeTime`
|
||||||
FROM `employee_state_history`
|
FROM `employee_state_history`
|
||||||
WHERE `EmployeeId` = ?
|
|
||||||
ORDER BY `ChangeTime` DESC
|
ORDER BY `ChangeTime` DESC
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
";
|
";
|
||||||
var values = new List<object> { employeeId, maxHistoryLength };
|
var values = new List<object> {maxHistoryLength };
|
||||||
string historyJson = Program.mySql.Query(sql, null, values);
|
// 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":[…]} )
|
// 5) Return the JSON from Query (the client will receive {"error":…, "data":[…]} )
|
||||||
return historyJson;
|
return historyJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -36,6 +36,7 @@ public class Tester
|
|||||||
"clock in",
|
"clock in",
|
||||||
"clock out",
|
"clock out",
|
||||||
"clock break",
|
"clock break",
|
||||||
|
"clockhistory",
|
||||||
"help"
|
"help"
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -105,12 +106,46 @@ public class Tester
|
|||||||
var responseLines = response.Split("%break").ToList();
|
var responseLines = response.Split("%break").ToList();
|
||||||
await messageBox.ShowAsync("Command Result: clock break", responseLines);
|
await messageBox.ShowAsync("Command Result: clock break", responseLines);
|
||||||
}
|
}
|
||||||
|
else if (command == "clockhistory")
|
||||||
|
{
|
||||||
|
List<string> history = new List<string>();
|
||||||
|
string historyString = server.FullClockHistory().Result;
|
||||||
|
ClockHistoryResponse historyResponse;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
historyResponse = JsonSerializer.Deserialize<ClockHistoryResponse>(historyString);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ms.Error(e.Message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Dictionary<int, Employee> employees = new Dictionary<int, Employee>();
|
||||||
|
foreach (ClockEntry clockEntry in historyResponse.data)
|
||||||
|
{
|
||||||
|
if (!employees.ContainsKey(clockEntry.EmployeeId))
|
||||||
|
{
|
||||||
|
Employee newEmployee = await server.GetEmployee(id: clockEntry.EmployeeId);
|
||||||
|
employees.Add(clockEntry.EmployeeId, newEmployee);
|
||||||
|
ms.Log(newEmployee.ToJson());
|
||||||
|
}
|
||||||
|
|
||||||
|
history.Add($"{clockEntry.EmployeeId}-{employees[clockEntry.EmployeeId].Code} - {clockEntry.NewState} - {clockEntry.ChangeTime}");
|
||||||
|
//Console.WriteLine($"{clockEntry.EmployeeId}-{employees[clockEntry.EmployeeId].Code} - {clockEntry.NewState} - {clockEntry.ChangeTime}");
|
||||||
|
}
|
||||||
|
ItemSelector selectorTemp = new ItemSelector(ms);
|
||||||
|
selectorTemp.SetTitle("Clock History");
|
||||||
|
selectorTemp.SelectItemFromList(history.ToArray());
|
||||||
|
//Console.ReadKey();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
await messageBox.ShowAsync("Error", new List<string> { ex.Message });
|
await messageBox.ShowAsync("Error", new List<string> { ex.Message });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
string input = "";
|
string input = "";
|
||||||
if (command.Contains("login"))
|
if (command.Contains("login"))
|
||||||
@ -141,3 +176,14 @@ public class Tester
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public class ClockEntry
|
||||||
|
{
|
||||||
|
public int EmployeeId { get; set; }
|
||||||
|
public int NewState { get; set; }
|
||||||
|
public DateTime ChangeTime { get; set; }
|
||||||
|
}
|
||||||
|
public class ClockHistoryResponse
|
||||||
|
{
|
||||||
|
public bool error { get; set; }
|
||||||
|
public List<ClockEntry> data { get; set; } = new();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user