52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using ModVersionChecker.managers.interfaces;
|
|
using ModVersionChecker.repository.api.dto;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace ModVersionChecker.managers.filesystem
|
|
{
|
|
public class SourcesDefManager
|
|
{
|
|
private readonly string _filePath = Path.Combine(AppContext.BaseDirectory, "data", "sourcesDef.json");
|
|
private List<SourceResponse> _sourcesDef = new List<SourceResponse>();
|
|
|
|
public SourcesDefManager()
|
|
{
|
|
_sourcesDef = Load();
|
|
}
|
|
|
|
private List<SourceResponse> Load()
|
|
{
|
|
if (!File.Exists(_filePath))
|
|
return new List<SourceResponse>();
|
|
var json = File.ReadAllText(_filePath);
|
|
return JsonSerializer.Deserialize<List<SourceResponse>>(json) ?? new();
|
|
}
|
|
|
|
public List<SourceResponse> GetSourcesDef()
|
|
{
|
|
return _sourcesDef;
|
|
}
|
|
|
|
public void AddSourceDef(SourceResponse sourceDef)
|
|
{
|
|
_sourcesDef.Add(sourceDef);
|
|
Save(_sourcesDef);
|
|
}
|
|
|
|
public void RemoveSourceDef(string id)
|
|
{
|
|
_sourcesDef.RemoveAll(s => s.Id == id);
|
|
Save(_sourcesDef);
|
|
}
|
|
|
|
|
|
public void Save(List<SourceResponse> sourcesDef)
|
|
{
|
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
|
var json = JsonSerializer.Serialize(sourcesDef, options);
|
|
File.WriteAllText(_filePath, json);
|
|
}
|
|
}
|
|
} |