mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-06-20 15:53:16 +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
|
||||
try
|
||||
{
|
||||
return Task.FromResult(Employee.FromJson(commandResult));
|
||||
var employee = Employee.FromJson(commandResult);
|
||||
return Task.FromResult(employee);
|
||||
}
|
||||
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.");
|
||||
|
||||
// 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")]
|
||||
@ -438,12 +455,12 @@ public class CommandLibrary
|
||||
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);
|
||||
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;
|
||||
|
@ -36,6 +36,7 @@ public class Tester
|
||||
"clock in",
|
||||
"clock out",
|
||||
"clock break",
|
||||
"clockhistory",
|
||||
"help"
|
||||
};
|
||||
|
||||
@ -105,12 +106,46 @@ public class Tester
|
||||
var responseLines = response.Split("%break").ToList();
|
||||
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)
|
||||
{
|
||||
await messageBox.ShowAsync("Error", new List<string> { ex.Message });
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
string input = "";
|
||||
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