Files
Jose Conde 5e16f781b4 phase 1
2025-09-29 16:02:00 +02:00

42 lines
1.1 KiB
C#

using ModVersionChecker.managers.interfaces;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace ModVersionChecker.managers.filesystem
{
public class AppsManager
{
private readonly string FilePath = Path.Combine(AppContext.BaseDirectory, "data", "apps.json");
public List<App> Load()
{
if (!File.Exists(FilePath))
return new List<App>();
var json = File.ReadAllText(FilePath);
return JsonSerializer.Deserialize<List<App>>(json) ?? new();
}
public void Save(List<App> apps)
{
var options = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(apps, options);
File.WriteAllText(FilePath, json);
}
public void Upsert(App app)
{
var apps = Load();
var index = apps.FindIndex(a => a.Id == app.Id);
if (index >= 0)
{
apps[index] = app;
}
else
{
apps.Add(app);
}
Save(apps);
}
}
}