Skip to content

Commit f411fa0

Browse files
CopilotabeckDev
andcommitted
Address code review feedback - add null safety and improve documentation
Co-authored-by: abeckDev <8720854+abeckDev@users.noreply.github.com>
1 parent d6da128 commit f411fa0

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

AbeckDev.DbTimetable.Mcp/Services/TimeTableService.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,17 @@ private List<ConnectionInfo> FindConnectionsInTimetable(string timetableXml, str
318318
stop.Element("dp")?.Attribute("ppth")?.Value?.Split('|').LastOrDefault() ?? "";
319319

320320
// Check if the destination station or path contains our target station
321-
// This is a heuristic - in reality we'd need to check the full route
321+
// This is a heuristic - the actual route might not be fully represented
322+
// We use the first word of the station name for matching to handle complex station names
322323
var pathStations = path.Split('|').Select(s => s.Trim()).ToList();
324+
var stationBFirstWord = GetFirstWord(stationB.Name);
325+
323326
var goesToDestination = pathStations.Any(ps =>
324327
ps.Equals(stationB.Name, StringComparison.OrdinalIgnoreCase) ||
325-
ps.Contains(stationB.Name.Split(' ')[0], StringComparison.OrdinalIgnoreCase));
328+
(!string.IsNullOrEmpty(stationBFirstWord) && ps.Contains(stationBFirstWord, StringComparison.OrdinalIgnoreCase)));
326329

327-
if (!goesToDestination && !destination.Contains(stationB.Name.Split(' ')[0], StringComparison.OrdinalIgnoreCase))
330+
if (!goesToDestination &&
331+
(string.IsNullOrEmpty(stationBFirstWord) || !destination.Contains(stationBFirstWord, StringComparison.OrdinalIgnoreCase)))
328332
{
329333
continue; // Skip trains that don't go to our destination
330334
}
@@ -388,6 +392,7 @@ private List<ConnectionInfo> FindConnectionsInTimetable(string timetableXml, str
388392

389393
/// <summary>
390394
/// Parse Deutsche Bahn timetable date format (YYMMddHHmm)
395+
/// Example: "2511061430" represents 2025-11-06 14:30 (YY=25, MM=11, dd=06, HH=14, mm=30)
391396
/// </summary>
392397
private DateTime? ParseTimetableDateTime(string? dateTimeStr)
393398
{
@@ -412,6 +417,19 @@ private List<ConnectionInfo> FindConnectionsInTimetable(string timetableXml, str
412417
return null;
413418
}
414419

420+
/// <summary>
421+
/// Helper method to safely get the first word from a station name
422+
/// Returns empty string if the input is null or empty
423+
/// </summary>
424+
private string GetFirstWord(string? text)
425+
{
426+
if (string.IsNullOrWhiteSpace(text))
427+
return "";
428+
429+
var words = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
430+
return words.Length > 0 ? words[0] : "";
431+
}
432+
415433
/// <summary>
416434
/// Station information
417435
/// </summary>

0 commit comments

Comments
 (0)