Add project files.
This commit is contained in:
42
ModVersionChecker/managers/filesystem/AppsManager.cs
Normal file
42
ModVersionChecker/managers/filesystem/AppsManager.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
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<AppConfig> Load()
|
||||
{
|
||||
if (!File.Exists(FilePath))
|
||||
return new List<AppConfig>();
|
||||
var json = File.ReadAllText(FilePath);
|
||||
return JsonSerializer.Deserialize<List<AppConfig>>(json) ?? new();
|
||||
}
|
||||
|
||||
public void Save(List<AppConfig> apps)
|
||||
{
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
var json = JsonSerializer.Serialize(apps, options);
|
||||
File.WriteAllText(FilePath, json);
|
||||
}
|
||||
|
||||
public void Upsert(AppConfig 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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user