feat: file metadata

This commit is contained in:
2026-03-12 11:11:34 +01:00
parent d0f54655b8
commit adaa6256cd
8 changed files with 330 additions and 60 deletions

View File

@@ -178,22 +178,7 @@ namespace CarManagerV3
try
{
string[] parts = csv.Split(';');
// is part 7 a valid int? if not set it to 0 and log a warning.
if (parts.Length == 7)
{
Console.Error.WriteLine($"Warning: CSV string has only 7 fields, expected 8. Setting Order to 0. CSV: {csv}");
if (!StateManager.askForMigration())
{
throw new Exception("User declined migration. Cannot parse CSV string with missing Order field.");
}
Array.Resize(ref parts, 8);
parts[7] = "0";
}
else if (parts.Length != 8)
{
throw new FormatException($"CSV string has {parts.Length} fields, expected 8. CSV: {csv}");
}
System.Diagnostics.Debug.WriteLine(csv);
Car temp = new Car(parts[0], parts[1], parts[2], int.Parse(parts[3]), parts[4], int.Parse(parts[5]), decimal.Parse(parts[6]), int.Parse(parts[7]));
return temp;
}

View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarManagerV3.Manager;
namespace CarManagerV3.Classes
{
internal class FileMeta
{
private string version;
private DateTime lastModified;
public string Version { get { return version; } set { version = value; } }
public DateTime LastModified { get { return lastModified; } }
public FileMeta(string version)
{
this.version = version;
this.lastModified = DateTime.Now;
}
public static FileMeta FromCSV(string csv)
{
string[] parts = csv.Split(';');
if (parts.Length < 3)
{
throw new FormatException("CSV format is incorrect. Expected format: product;version;lastModified");
}
return new FileMeta(parts[1]) { lastModified = DateTime.Parse(parts[2]) };
}
public static FileMeta Generate()
{
string version = Updater.GetCurrentVersion();
return new FileMeta(version);
}
public static bool IsFileMeta(string csv)
{
return csv.StartsWith("CarManager3;");
}
public static bool IsOlderVersion(FileMeta meta)
{
string version = Updater.GetCurrentVersion();
return Updater.IsNewerVersion(version, meta.Version);
}
public static bool NeedsUpdate(FileMeta meta)
{
string version = Updater.GetCurrentVersion();
return Updater.IsNewerVersion(version, meta.Version) && FileUpdate.GetRequiredUpdates(meta.Version).Count > 0;
}
public void Modify()
{
lastModified = DateTime.Now;
}
public string ToCSV()
{
return $"CarManager3;{version};{lastModified}";
}
public override string ToString()
{
return $"File Version: {version}, Last Modified: {lastModified}";
}
}
}

View File

@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarManagerV3.Manager;
namespace CarManagerV3.Classes
{
internal class FileUpdate
{
public string version;
public bool needUpdate;
public Func<string, string> updateAction;
public static List<FileUpdate> GetUpdates()
{
return new List<FileUpdate>
{
new FileUpdate {
version = "1.4.3.0",
needUpdate = true,
updateAction = (fileContent) =>
{
// Implement the logic to update the file content for version
List<string> parts = fileContent.Split(";").ToList();
parts[4] = "Black";
return string.Join(";", parts);
}
},
new FileUpdate {
version = "1.4.4.0",
needUpdate = false
}
// Add more FileUpdate instances for other versions as needed
};
}
public static List<FileUpdate> GetRequiredUpdates(string fromVersion)
{
string currentVersion = Updater.GetCurrentVersion();
// Get all updates whos version is greater than fromVersion
return GetUpdates().Where(update => string.Compare(update.version, fromVersion) > 0 && string.Compare(update.version, currentVersion) <= 0 && update.needUpdate && update.updateAction != null).ToList();
}
public static string ApplyUpdates(string fileContent, string fileVersion)
{
List<FileUpdate> updates = GetRequiredUpdates(fileVersion);
foreach (var update in updates)
{
if (update.needUpdate && update.updateAction != null)
{
fileContent = update.updateAction(fileContent);
}
}
return fileContent;
}
public static List<string> ApplyUpdatesToALl(List<string> fileContents, string fileVersion)
{
List<FileUpdate> updates = GetRequiredUpdates(fileVersion);
List<string> updatedContents = new List<string>();
foreach (var content in fileContents)
{
string updatedContent = content;
foreach (var update in updates)
{
if (update.needUpdate && update.updateAction != null)
{
updatedContent = update.updateAction(updatedContent);
}
}
updatedContents.Add(updatedContent);
}
return updatedContents;
}
public static string PerformOldLegacyUpdate(string fileContent)
{
string[] parts = fileContent.Split(';');
// is part 7 a valid int? if not set it to 0 and log a warning.
if (parts.Length == 7)
{
Console.Error.WriteLine($"Warning: CSV string has only 7 fields, expected 8. Setting Order to 0. CSV: {fileContent}");
if (!StateManager.askForMigration())
{
throw new Exception("User declined migration. Cannot parse CSV string with missing Order field.");
}
Array.Resize(ref parts, 8);
parts[7] = "0";
return string.Join(";", parts);
}
else if (parts.Length != 8)
{
throw new FormatException($"CSV string has {parts.Length} fields, expected 8. CSV: {fileContent}");
}
return fileContent;
}
}
}