diff --git a/Server/Controllers/ShiftController.cs b/Server/Controllers/ShiftController.cs index f4617b2..2f146b4 100644 --- a/Server/Controllers/ShiftController.cs +++ b/Server/Controllers/ShiftController.cs @@ -12,20 +12,20 @@ namespace ShiftScheduler.Server.Controllers private readonly IcsExportService _icsService; private readonly PdfExportService _pdfExportService; private readonly ITransportService _transportService; - private readonly TransportConfiguration _transportConfig; + private readonly IConfigurationService _configurationService; public ShiftController( ShiftService shiftService, IcsExportService icsService, PdfExportService pdfExportService, ITransportService transportService, - TransportConfiguration transportConfig) + IConfigurationService configurationService) { _shiftService = shiftService; _icsService = icsService; _pdfExportService = pdfExportService; _transportService = transportService; - _transportConfig = transportConfig; + _configurationService = configurationService; } [HttpGet("shifts")] @@ -43,6 +43,7 @@ namespace ShiftScheduler.Server.Controllers return NotFound($"Shift '{request.ShiftName}' not found"); } + var transportConfig = _configurationService.GetTransportConfiguration(); TransportConnection? morningTransport = null; TransportConnection? afternoonTransport = null; @@ -71,7 +72,7 @@ namespace ShiftScheduler.Server.Controllers if (morningEndTime.HasValue && afternoonStartTime.HasValue) { var breakDurationMinutes = (afternoonStartTime.Value - morningEndTime.Value).TotalMinutes; - shouldLoadAfternoonTransport = breakDurationMinutes >= _transportConfig.MinBreakMinutes; + shouldLoadAfternoonTransport = breakDurationMinutes >= transportConfig.MinBreakMinutes; } } diff --git a/Server/Program.cs b/Server/Program.cs index 90b29e2..75632d7 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -16,8 +16,6 @@ var appConfiguration = new ApplicationConfiguration // Register services builder.Services.AddSingleton(new ConfigurationService(appConfiguration)); -builder.Services.AddSingleton>(provider => provider.GetRequiredService().GetShifts()); -builder.Services.AddSingleton(provider => provider.GetRequiredService().GetTransportConfiguration()); builder.Services.AddMemoryCache(); builder.Services.AddHttpClient(); builder.Services.AddSingleton(); diff --git a/Services/ShiftService.cs b/Services/ShiftService.cs index a2cc40d..62cead8 100644 --- a/Services/ShiftService.cs +++ b/Services/ShiftService.cs @@ -5,13 +5,13 @@ namespace ShiftScheduler.Services { public class ShiftService { - private readonly List _shifts; + private readonly IConfigurationService _configurationService; - public ShiftService(List shifts) + public ShiftService(IConfigurationService configurationService) { - _shifts = shifts; + _configurationService = configurationService; } - public List GetShifts() => _shifts; + public List GetShifts() => _configurationService.GetShifts(); } } diff --git a/Services/TransportApiService.cs b/Services/TransportApiService.cs index 6b511f2..15ffd72 100644 --- a/Services/TransportApiService.cs +++ b/Services/TransportApiService.cs @@ -3,10 +3,12 @@ using ShiftScheduler.Shared; namespace ShiftScheduler.Services { - public class TransportApiService(HttpClient httpClient, TransportConfiguration config) : ITransportApiService + public class TransportApiService(HttpClient httpClient, IConfigurationService configurationService) : ITransportApiService { public async Task GetConnectionAsync(DateTime shiftStartTime) { + var config = configurationService.GetTransportConfiguration(); + // To allow connections that arrive after shift starts, we search from earlier time // and request more connections to cover the full range var searchDate = shiftStartTime.ToString("yyyy-MM-dd"); diff --git a/Services/TransportService.cs b/Services/TransportService.cs index 94647ee..91dd0f0 100644 --- a/Services/TransportService.cs +++ b/Services/TransportService.cs @@ -3,15 +3,16 @@ using ShiftScheduler.Shared; namespace ShiftScheduler.Services { - public class TransportService(ITransportApiService transportService, TransportConfiguration config, IMemoryCache cache) : ITransportService + public class TransportService(ITransportApiService transportService, IConfigurationService configurationService, IMemoryCache cache) : ITransportService { public async Task GetConnectionAsync(DateTime shiftStartTime) { + var config = configurationService.GetTransportConfiguration(); var searchDate = shiftStartTime.ToString("yyyy-MM-dd"); var searchTime = shiftStartTime.AddMinutes(config.MaxLateArrivalMinutes).ToString("HH:mm"); // Generate cache key based on request parameters - var cacheKey = GenerateCacheKey(searchDate, searchTime); + var cacheKey = GenerateCacheKey(config, searchDate, searchTime); // Try to get from cache first if (cache.TryGetValue(cacheKey, out TransportConnection? cachedConnection) && cachedConnection != null) @@ -35,7 +36,7 @@ namespace ShiftScheduler.Services return connection; } - private string GenerateCacheKey(string searchDate, string searchTime) + private string GenerateCacheKey(TransportConfiguration config, string searchDate, string searchTime) { return $"transport_{config.StartStation}_{config.EndStation}_{searchDate}_{searchTime}"; } diff --git a/ShiftScheduler.Services.Tests/TransportApiServiceTests.cs b/ShiftScheduler.Services.Tests/TransportApiServiceTests.cs index 8065a29..cd95c7a 100644 --- a/ShiftScheduler.Services.Tests/TransportApiServiceTests.cs +++ b/ShiftScheduler.Services.Tests/TransportApiServiceTests.cs @@ -11,6 +11,7 @@ namespace ShiftScheduler.Services.Tests; public class TransportApiServiceTests { private readonly Mock _httpMessageHandlerMock; + private readonly Mock _configurationServiceMock; private readonly HttpClient _httpClient; private readonly TransportApiService _transportApiService; @@ -18,6 +19,7 @@ public class TransportApiServiceTests { _httpMessageHandlerMock = new Mock(); _httpClient = new HttpClient(_httpMessageHandlerMock.Object); + _configurationServiceMock = new Mock(); var config = new TransportConfiguration { @@ -31,7 +33,11 @@ public class TransportApiServiceTests CacheDurationDays = 1 }; - _transportApiService = new TransportApiService(_httpClient, config); + _configurationServiceMock + .Setup(x => x.GetTransportConfiguration()) + .Returns(config); + + _transportApiService = new TransportApiService(_httpClient, _configurationServiceMock.Object); } [Fact] diff --git a/ShiftScheduler.Services.Tests/TransportServiceTests.cs b/ShiftScheduler.Services.Tests/TransportServiceTests.cs index fe0f265..516b1bc 100644 --- a/ShiftScheduler.Services.Tests/TransportServiceTests.cs +++ b/ShiftScheduler.Services.Tests/TransportServiceTests.cs @@ -9,6 +9,7 @@ namespace ShiftScheduler.Services.Tests; public class TransportServiceTests { private readonly Mock _transportServiceMock; + private readonly Mock _configurationServiceMock; private readonly IMemoryCache _memoryCache; private readonly TransportService _transportService; private readonly TransportConfiguration _config; @@ -16,6 +17,7 @@ public class TransportServiceTests public TransportServiceTests() { _transportServiceMock = new Mock(); + _configurationServiceMock = new Mock(); _memoryCache = new MemoryCache(new MemoryCacheOptions()); _config = new TransportConfiguration @@ -30,7 +32,11 @@ public class TransportServiceTests CacheDurationDays = 1 }; - _transportService = new TransportService(_transportServiceMock.Object, _config, _memoryCache); + _configurationServiceMock + .Setup(x => x.GetTransportConfiguration()) + .Returns(_config); + + _transportService = new TransportService(_transportServiceMock.Object, _configurationServiceMock.Object, _memoryCache); } [Fact]