Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

56 řádky
1.7KB

  1. using System.Text.Json;
  2. namespace Campaign_Tracker.Server.LegacyData.Schema;
  3. public sealed class FileLegacySchemaCheckHistory : ILegacySchemaCheckHistory
  4. {
  5. private readonly string _filePath;
  6. private readonly object _fileLock = new();
  7. private static readonly JsonSerializerOptions JsonOptions = new()
  8. {
  9. PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  10. };
  11. public FileLegacySchemaCheckHistory(string filePath)
  12. {
  13. if (string.IsNullOrWhiteSpace(filePath))
  14. {
  15. throw new ArgumentException("History file path is required.", nameof(filePath));
  16. }
  17. _filePath = filePath;
  18. Directory.CreateDirectory(Path.GetDirectoryName(_filePath) ?? ".");
  19. }
  20. public void Record(LegacySchemaCheckResult result)
  21. {
  22. ArgumentNullException.ThrowIfNull(result);
  23. var line = JsonSerializer.Serialize(result, JsonOptions) + Environment.NewLine;
  24. lock (_fileLock)
  25. {
  26. File.AppendAllText(_filePath, line);
  27. }
  28. }
  29. public IReadOnlyList<LegacySchemaCheckResult> GetRecent(int maxCount = 50)
  30. {
  31. if (maxCount <= 0 || !File.Exists(_filePath))
  32. {
  33. return [];
  34. }
  35. lock (_fileLock)
  36. {
  37. return File.ReadLines(_filePath)
  38. .Where(line => !string.IsNullOrWhiteSpace(line))
  39. .Select(line => JsonSerializer.Deserialize<LegacySchemaCheckResult>(line, JsonOptions))
  40. .Where(result => result is not null)
  41. .Cast<LegacySchemaCheckResult>()
  42. .OrderByDescending(result => result.CheckedAt)
  43. .Take(maxCount)
  44. .ToArray();
  45. }
  46. }
  47. }

Powered by TurnKey Linux.