You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.7KB

  1. using Campaign_Tracker.Server.Authorization;
  2. using Campaign_Tracker.Server.Municipalities;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Mvc;
  5. namespace Campaign_Tracker.Server.Controllers;
  6. [ApiController]
  7. [Authorize(Policy = ApplicationPolicy.ClientServicesAccess)]
  8. [Route("api/municipalities/{profileId}/prior-cycle-defaults")]
  9. public sealed class MunicipalityPriorCycleDefaultsController : ControllerBase
  10. {
  11. private const string EmptyMessage = "No prior cycle defaults available.";
  12. private readonly IMunicipalityProfileRepository _profiles;
  13. private readonly IMunicipalityPriorCycleDefaultsRepository _defaults;
  14. public MunicipalityPriorCycleDefaultsController(
  15. IMunicipalityProfileRepository profiles,
  16. IMunicipalityPriorCycleDefaultsRepository defaults)
  17. {
  18. _profiles = profiles;
  19. _defaults = defaults;
  20. }
  21. [HttpGet]
  22. public async Task<ActionResult<MunicipalityPriorCycleDefaultsResponse>> Get(
  23. string profileId,
  24. CancellationToken cancellationToken)
  25. {
  26. var profile = await _profiles.GetByIdAsync(profileId, cancellationToken);
  27. if (profile is null)
  28. return NotFound();
  29. var cycles = await _defaults.GetByJCodeAsync(
  30. profile.Profile.JCode,
  31. cancellationToken);
  32. var responseCycles = cycles
  33. .Select(PriorCycleResponse.From)
  34. .ToArray();
  35. return Ok(new MunicipalityPriorCycleDefaultsResponse(
  36. ProfileId: profileId,
  37. HasPriorCycles: responseCycles.Length > 0,
  38. SelectedCycleId: responseCycles.FirstOrDefault()?.CycleId,
  39. EmptyStateMessage: responseCycles.Length == 0 ? EmptyMessage : string.Empty,
  40. Cycles: responseCycles));
  41. }
  42. }
  43. public sealed record MunicipalityPriorCycleDefaultsResponse(
  44. string ProfileId,
  45. bool HasPriorCycles,
  46. string? SelectedCycleId,
  47. string EmptyStateMessage,
  48. IReadOnlyList<PriorCycleResponse> Cycles);
  49. public sealed record PriorCycleResponse(
  50. string CycleId,
  51. string CycleName,
  52. string CompletedAt,
  53. IReadOnlyList<PriorCycleServiceResponse> Services)
  54. {
  55. public static PriorCycleResponse From(MunicipalityPriorCycleDefaults cycle) =>
  56. new(
  57. cycle.CycleId,
  58. cycle.CycleName,
  59. cycle.CompletedAt.ToString("O"),
  60. cycle.Services.Select(PriorCycleServiceResponse.From).ToArray());
  61. }
  62. public sealed record PriorCycleServiceResponse(
  63. string ServiceType,
  64. string Summary,
  65. IReadOnlyDictionary<string, string> Values)
  66. {
  67. public static PriorCycleServiceResponse From(MunicipalityPriorCycleServiceDefault service) =>
  68. new(service.ServiceType, service.Summary, service.Values);
  69. }

Powered by TurnKey Linux.