Enhance staffing management in school simulation by introducing API endpoints for hiring staff and assigning subjects. Implement payroll cap validation to ensure hiring and subject assignments do not exceed the allocated budget. Update the simulation options to include a monthly payroll cap and revise related classes to support new staffing functionalities. Enhance documentation to reflect these changes and update tests to validate the new features.

This commit is contained in:
Leonid Pershin
2026-08-19 00:19:09 +03:00
parent c189578680
commit 94842ab192
24 changed files with 1444 additions and 27 deletions
+175 -6
View File
@@ -6,8 +6,8 @@ using HSchool.Simulation;
namespace HSchool.Server.Api;
/// <summary>
/// The main menu talks to these: list, create, delete. The people list reads a published roster
/// snapshot; the person card goes through the school's mailbox because needs are live.
/// The main menu talks to these: list, create, delete. People list and staffing read published
/// snapshots; the person card, hire and subject changes go through the school's mailbox.
/// </summary>
internal static class SchoolEndpoints
{
@@ -156,6 +156,86 @@ internal static class SchoolEndpoints
};
})
.WithName("GetSchoolPerson");
schools.MapGet("/{id:int}/staffing", (
int id,
string? lang,
GameLoopService loop) =>
{
var published = loop.FindPeople(id);
if (published is null)
{
return Problem(StatusCodes.Status404NotFound, "unknown-school", "That school does not exist.");
}
return Results.Ok(MapStaffing(published, loop.Options.MonthlyPayrollCap, ParseLocale(lang)));
})
.WithName("GetSchoolStaffing");
schools.MapPost("/{id:int}/staff/hire", async (
int id,
HireStaffRequest request,
string? lang,
GameCommandQueue commands,
GameLoopService loop,
CancellationToken cancellationToken) =>
{
if (!TryPersonId(request.PersonId, out var personId, out var error)
|| !TryDefName(request.Position, "position", out var position, out error))
{
return Problem(StatusCodes.Status400BadRequest, "invalid-query", error);
}
var command = new GameCommand.HireStaff(id, personId, position, NewCompletion<StaffingOutcome>());
commands.Enqueue(command);
var outcome = await command.Result.Task.WaitAsync(CommandTimeout, cancellationToken);
return StaffingResult(id, outcome, loop, ParseLocale(lang));
})
.WithName("HireSchoolStaff");
schools.MapPost("/{id:int}/staff/{personId}/subjects", async (
int id,
string personId,
AssignSubjectRequest request,
string? lang,
GameCommandQueue commands,
GameLoopService loop,
CancellationToken cancellationToken) =>
{
if (!TryPersonId(personId, out var idValue, out var error)
|| !TryDefName(request.Subject, "subject", out var subject, out error))
{
return Problem(StatusCodes.Status400BadRequest, "invalid-query", error);
}
var command = new GameCommand.AssignSubject(id, idValue, subject, NewCompletion<StaffingOutcome>());
commands.Enqueue(command);
var outcome = await command.Result.Task.WaitAsync(CommandTimeout, cancellationToken);
return StaffingResult(id, outcome, loop, ParseLocale(lang));
})
.WithName("AssignSchoolSubject");
schools.MapDelete("/{id:int}/staff/{personId}/subjects/{subject}", async (
int id,
string personId,
string subject,
string? lang,
GameCommandQueue commands,
GameLoopService loop,
CancellationToken cancellationToken) =>
{
if (!TryPersonId(personId, out var idValue, out var error)
|| !TryDefName(subject, "subject", out var subjectName, out error))
{
return Problem(StatusCodes.Status400BadRequest, "invalid-query", error);
}
var command = new GameCommand.UnassignSubject(id, idValue, subjectName, NewCompletion<StaffingOutcome>());
commands.Enqueue(command);
var outcome = await command.Result.Task.WaitAsync(CommandTimeout, cancellationToken);
return StaffingResult(id, outcome, loop, ParseLocale(lang));
})
.WithName("UnassignSchoolSubject");
}
/// <summary>The supervisor must never be blocked by a continuation of a waiting request.</summary>
@@ -279,11 +359,100 @@ internal static class SchoolEndpoints
return true;
}
private static IResult Problem(int statusCode, string code, string detail) =>
Results.Problem(detail: detail, statusCode: statusCode, title: code, extensions: new Dictionary<string, object?>
private static StaffingResponse MapStaffing(PublishedSchoolPeople published, float allocated, string locale) =>
StaffingMapper.From(
published.Roster,
published.Applicants,
published.Catalog,
published.School.GameTime,
allocated,
locale);
private static IResult StaffingResult(int schoolId, StaffingOutcome outcome, GameLoopService loop, string locale)
{
if (outcome.Error != StaffingError.None)
{
["code"] = code,
});
return StaffingProblem(outcome);
}
var published = loop.FindPeople(schoolId);
if (published is null)
{
return Problem(StatusCodes.Status404NotFound, "unknown-school", "That school does not exist.");
}
return Results.Ok(MapStaffing(published, loop.Options.MonthlyPayrollCap, locale));
}
private static IResult StaffingProblem(StaffingOutcome outcome) =>
outcome.Error switch
{
StaffingError.UnknownApplicant =>
Problem(StatusCodes.Status404NotFound, "unknown-applicant", "That person is not in the applicant pool."),
StaffingError.AlreadyHired =>
Problem(StatusCodes.Status409Conflict, "already-hired", "That person is already on staff."),
StaffingError.UnknownPosition =>
Problem(StatusCodes.Status400BadRequest, "unknown-position", "That position is not in the catalog."),
StaffingError.UnknownSubject =>
Problem(StatusCodes.Status400BadRequest, "unknown-subject", "That subject is not in the catalog."),
StaffingError.NotStaff =>
Problem(StatusCodes.Status400BadRequest, "not-staff", "That person is not on staff."),
StaffingError.NotTeacher =>
Problem(StatusCodes.Status400BadRequest, "not-teacher", "Only a teacher can be assigned a subject."),
StaffingError.AlreadyAssigned =>
Problem(StatusCodes.Status409Conflict, "already-assigned", "That subject is already assigned to this person."),
StaffingError.SubjectNotAssigned =>
Problem(StatusCodes.Status404NotFound, "unknown-assignment", "That subject is not assigned to this person."),
StaffingError.PayrollExceeded =>
Problem(
StatusCodes.Status409Conflict,
"payroll-exceeded",
$"That change would take payroll from {outcome.Payroll} to {outcome.Attempted} against an allocation of {outcome.Allocated}.",
outcome),
StaffingError.NoOpening =>
Problem(StatusCodes.Status409Conflict, "no-opening", "There is no free opening for that position."),
_ => Problem(StatusCodes.Status404NotFound, "unknown-school", "That school does not exist."),
};
private static bool TryPersonId(string? value, out string personId, out string error)
{
personId = value?.Trim() ?? string.Empty;
if (personId.Length is < 1 or > 64)
{
error = "The person id is not valid.";
return false;
}
error = string.Empty;
return true;
}
private static bool TryDefName(string? value, string field, out string name, out string error)
{
name = value?.Trim() ?? string.Empty;
if (name.Length is < 1 or > 64)
{
error = $"{field} is not valid.";
return false;
}
error = string.Empty;
return true;
}
private static IResult Problem(int statusCode, string code, string detail, StaffingOutcome? staffing = null)
{
var extensions = new Dictionary<string, object?> { ["code"] = code };
if (staffing is not null)
{
extensions["allocated"] = staffing.Allocated;
extensions["payroll"] = staffing.Payroll;
extensions["remaining"] = staffing.Remaining;
extensions["attempted"] = staffing.Attempted;
}
return Results.Problem(detail: detail, statusCode: statusCode, title: code, extensions: extensions);
}
}
/// <summary>Body of <c>POST /api/schools</c>. The start date is a game calendar date, not a real one.</summary>